00045 {
00046
00047 $articles = new Default_Model_Articles();
00048 $this->_images = new Default_Model_ArticleImages();
00049 $this->_videos = new Default_Model_ArticleVideos();
00050 $articleData = null;
00051
00052 $userData = (object) array ();
00053 if ($this->_auth->hasIdentity()){
00054 $userData = $this->_auth->getIdentity();
00055 } else {
00056 $userData->UserID = 0;
00057 $userData->Email = $this->getRequest()->getParam('email');
00058 $userData->Author = $this->getRequest()->getParam('author');
00059 }
00060
00061 if ($this->getRequest()->getParam('articleId')){
00062 $check = $articles->fetchRow($articles->getAdapter()->quoteInto('UserID = ? ', $userData->UserID) .
00063 ' AND ' . $articles->getAdapter()->quoteInto('ArticleID = ? ', $this->getRequest()->getParam('articleId')));
00064 if (!$check->ArticleID){
00065 # Tring to hack and edit someone alse article!!?
00066 $this->_redirect('index');
00067 exit;
00068 }
00069 $articleData = $articles->myFetchRow(array (
00070 'A.ArticleID' => $this->getRequest()->getParam('articleId'),
00071 ),
00072 $this->getRequest()->getParam('lang')
00073 );
00074 if (!$articleData->LanguageCode){
00075 $articleData->LanguageCode = 'bg';
00076 }
00077 $this->view->articleId = $articleData->ArticleID;
00078 $this->_articleId = $articleData->ArticleID;
00079 $this->setImages();
00080 $this->setVideos();
00081 }
00082 $this->view->form = $this->getForm($articleData);
00083 $this->view->editLang = $articleData->LanguageCode ? $articleData->LanguageCode : $this->getRequest()->getParam('lang');
00084
00085 if ($this->getRequest()->isPost()) {
00086 if ($this->view->form->isValid($_POST)){
00087 $articles->getAdapter()->beginTransaction();
00088 $trans = new Default_Model_ArticlesTranslation();
00089 $data = array (
00090 'Status' => 'N',
00091 'UserID' => $userData->UserID,
00092 'Author' => $userData->Author,
00093 'Email' => $userData->Email,
00094 'CategoryID' => $this->getRequest()->getParam('categoryId'),
00095 'RegionID' => $this->getRequest()->getParam('regionId'),
00096 'RemoteIp' => $this->getRequest()->getServer('REMOTE_ADDR'),
00097 #'DateAdded' => new Zend_Db_Expr('NOW()'),
00098 );
00099 $dataTrans = array (
00100 'Title' => $this->getRequest()->getParam('title'),
00101 'ShortDescription' => $this->getRequest()->getParam('shortDescription'),
00102 'Content' => $this->getRequest()->getParam('content'),
00103 'LanguageCode' => $this->getRequest()->getParam('language'),
00104 );
00105 if (!$this->getRequest()->getParam('articleId')){
00106 $data['DateAdded'] = new Zend_Db_Expr('NOW()');
00107 $this->_articleId = $articles->insert($data);
00108 $dataTrans['ArticleID'] = $this->_articleId;
00109 $trans->insert($dataTrans);
00110 $this->processImages();
00111 $this->processVideos();
00112 } elseif ($userData->UserID > 0) {
00113 # XXX Ensure that the article is owned by this user and update it
00114 # XXX This is done above. Can we remove it!!?
00115 $data['LastEditDate'] = new Zend_Db_Expr('NOW()');
00116 $this->_articleId = $this->getRequest()->getParam('articleId');
00117 $check = $articles->fetchRow($articles->getAdapter()->quoteInto('UserID = ? ', $userData->UserID) .
00118 ' AND ' . $articles->getAdapter()->quoteInto('ArticleID = ? ', $this->_articleId));
00119 if (!$check->ArticleID){
00120 # Tring to hack and edit someone alse article!!?
00121 $this->_redirect('index');
00122 exit;
00123 }
00124 $articles->update($data, $articles->getAdapter()->quoteInto('UserID = ? ', $userData->UserID) .
00125 ' AND ' . $articles->getAdapter()->quoteInto('ArticleID = ? ', $this->_articleId));
00126 # XXX Here we can get unexisting language code, but...
00127 $dataTrans['ArticleID'] = $this->_articleId;
00128 $check = $trans->fetchRow($articles->getAdapter()->quoteInto('ArticleID = ? ', $this->_articleId) .
00129 ' AND ' . $articles->getAdapter()->quoteInto('LanguageCode = ? ', $this->getRequest()->getParam('language')));
00130 if ($check->TranslationID){
00131 $numRows = $trans->update($dataTrans, $articles->getAdapter()->quoteInto('ArticleID = ? ',
00132 $this->_articleId) .
00133 ' AND ' . $articles->getAdapter()->quoteInto('LanguageCode = ? ',
00134 $this->getRequest()->getParam('language')));
00135 } else {
00136 $trans->insert($dataTrans);
00137 }
00138 $this->processImages();
00139 $this->processVideos();
00140 } else {
00141 $this->_redirect('login');
00142 }
00143 $articles->getAdapter()->commit();
00144 $this->_redirect('articles/view/articleId/' . $this->_articleId);
00145 }
00146 }
00147 }