示例#1
0
 /**
  *   addAction
  *
  *   Adds new content by content type.
  *
  */
 public function addAction()
 {
     // Get authentication
     $auth = Zend_Auth::getInstance();
     // If user has identity
     if ($auth->hasIdentity()) {
         // Get requests
         $params = $this->getRequest()->getParams();
         // Get content type
         $contentType = isset($params['contenttype']) ? $params['contenttype'] : '';
         $relatesToId = isset($params['relatestoid']) ? $params['relatestoid'] : 0;
         // Get all content types from the database
         $modelContentTypes = new Default_Model_ContentTypes();
         //$contentTypes = $modelContentTypes->getAllNamesAndIds();
         $validContentType = $modelContentTypes->contentTypeExists($contentType);
         /*
         // Setting the variable first to be true
         $invalidContentType = true;
         
         // If set content type exists in database, invalidContentType
         // is set to false
         foreach($contentTypes as $cty){
         if($contentType == $cty['key_cty']) {
         $invalidContentType = false;
         }
         }
         */
         // Variable $relatesToId is set to 0, just in case that user is not
         // adding idea and there's no relatesto_id set
         $content = new Default_Model_Content();
         if (!$validContentType) {
             $message = 'content-add-contenttype-missing-or-invalid';
             $url = $this->_urlHelper->url(array('controller' => 'msg', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
             $this->flash($message, $url);
         } elseif ($relatesToId != 0) {
             /*if($relatesToId == 0) {
             				 $message = 'content-add-relatesto-id-missing';
             				 $url = $this->_urlHelper->url(array('controller' => 'msg',
             				 'action' => 'index',
             				 'language' => $this->view->language),
             				 'lang_default', true);
             
             				 $this->flash($message, $url);
             				 } */
             // Checking if the content that idea is related to exists
             $contentExists = $content->checkIfContentExists($relatesToId);
             if (!$contentExists) {
                 $message = 'content-add-invalid-related-content';
                 $url = $this->_urlHelper->url(array('controller' => 'msg', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
                 $this->flash($message, $url);
             }
         }
         $this->view->contenttype = $this->view->translate($contentType);
         $this->view->short_contenttype = $contentType;
         $this->view->relatesToId = $relatesToId;
         if ($relatesToId != 0) {
             $relatesToTypeId = $content->getContentTypeIdByContentId($relatesToId);
             $this->view->relatesToType = $modelContentTypes->getTypeById($relatesToTypeId);
             $this->view->relatesToHeader = $content->getContentHeaderByContentId($relatesToId);
         }
         // Content type id is needed when adding content  to database
         $contentTypeId = $modelContentTypes->getIdByType($contentType);
         // Cacheing of formData
         $cache = Zend_Registry::get('cache');
         $formDataCacheTag = 'formData_' . $contentType . '_' . $this->view->language;
         if (!($formData = $cache->load($formDataCacheTag)) || $relatesToId != 0 || true) {
             // Creating array for form data
             $formData = array();
             // Adding data to formData
             $formData['content_type'] = $contentTypeId;
             $formData['content_relatesto_id'] = $relatesToId;
             // Content classifications
             $modelFutureinfoClasses = new Default_Model_FutureinfoClasses();
             $futureinfoClasses = $modelFutureinfoClasses->getAllNamesAndIds();
             $formData['FutureinfoClasses'] = array();
             $formData['FutureinfoClasses'][0] = $this->view->translate("content-add-select-finfo-classification");
             foreach ($futureinfoClasses as $fic) {
                 $formData['FutureinfoClasses'][$fic['id_fic']] = $fic['name_fic'];
             }
             // end foreach
             if (empty($formData['FutureinfoClasses'])) {
                 $formData['FutureinfoClasses'] = array(0 => '----');
             }
             $modelInnovationTypes = new Default_Model_InnovationTypes();
             $innovationTypes = $modelInnovationTypes->getAllNamesAndIds();
             $formData['InnovationTypes'] = array();
             $formData['InnovationTypes'][0] = $this->view->translate("content-add-select-innovation");
             foreach ($innovationTypes as $ivt) {
                 $formData['InnovationTypes'][$ivt['id_ivt']] = $ivt['name_ivt'];
             }
             // end foreach
             if (empty($formData['InnovationTypes'])) {
                 $formData['InnovationTypes'] = array(0 => '----');
             }
             $languages = new Default_Model_Languages();
             $idLngInd = $languages->getLangIdByLangName($this->view->language);
             $allLanguages = $languages->getAllNamesAndIds();
             $formData['languages'] = array();
             $formData['languages'][0] = $this->view->translate("content-add-select-language");
             foreach ($allLanguages as $lng) {
                 $formData['languages'][$lng['id_lng']] = $lng['name_lng'];
             }
             $modelIndustries = new Default_Model_Industries();
             $industries = $modelIndustries->getNamesAndIdsById(0, $idLngInd);
             $formData['Industries'] = array();
             $formData['Industries'][0] = $this->view->translate("content-add-select-industry");
             foreach ($industries as $ind) {
                 $formData['Industries'][$ind['id_ind']] = $ind['name_ind'];
             }
             // end foreach
             if (empty($formData['Industries'])) {
                 $formData['Industries'] = array(0 => '----');
             }
             // The id of first industry listed is needed when listing the
             // divisions for the first time
             $firstIndustryId = $modelIndustries->getIndustryId();
             $divisions = $modelIndustries->getNamesAndIdsById($firstIndustryId, $idLngInd);
             $formData['Divisions'] = array();
             $formData['Divisions'][0] = $this->view->translate("content-add-select-division-no-industry");
             $formData['Groups'] = array();
             $formData['Groups'][0] = $this->view->translate("content-add-select-group-no-division");
             $formData['Classes'] = array();
             $formData['Classes'][0] = $this->view->translate("content-add-select-class-no-group");
             $cache->save($formData, $formDataCacheTag);
         }
         $formCacheTag = 'form_' . $contentType . '_' . $this->view->language;
         // Form for content adding, cacheing if not cached.
         // Generate new form if is post because cache will save post parameters and fail
         if ($this->getRequest()->isPost()) {
             $form = new Default_Form_AddContentForm(null, $formData, $this->view->language, $contentType);
         } elseif (!($form = $cache->load($formCacheTag))) {
             $form = new Default_Form_AddContentForm(null, $formData, $this->view->language, $contentType);
             $cache->save($form, $formCacheTag);
         }
         $this->view->form = $form;
         // Get requests
         if ($this->getRequest()->isPost()) {
             // Get content data
             $data = $this->getRequest()->getPost();
             // If form data is valid, handle database insertions
             $validForm = $form->isValid($data) ? true : isset($data['content_save']) && $data['content_save'] != '';
             if ($validForm) {
                 // If form data is going to be published
                 if (isset($data['content_publish']) && $data['content_publish'] != '') {
                     $data['publish'] = 1;
                     $message_error = 'content-publish-not-successful';
                 } elseif (isset($data['content_save']) && $data['content_save'] != '') {
                     $data['publish'] = 0;
                     $message_error = 'content-save-not-successful';
                 }
                 // Content keywords
                 /* FIXED: split() is deprecated in PHP 5.3.0 -> and removed in
                  * PHP 6.0, so changed to explode(). Also trim(array) doesn't
                  * trim array values, so this is done with foreach now.
                  */
                 $keywords = array();
                 foreach (explode(',', $data['content_keywords']) as $keyword) {
                     if (trim($keyword) != "") {
                         $keywords[] = strip_tags(trim($keyword));
                     }
                 }
                 $data['content_keywords'] = array_unique($keywords);
                 // Related companies
                 $relatedCompanies = array();
                 foreach (explode(',', $data['content_related_companies']) as $relatedCompany) {
                     if (trim($relatedCompany) != "") {
                         $relatedCompanies[] = strip_tags(trim($relatedCompany));
                     }
                 }
                 $data['content_related_companies'] = array_unique($relatedCompanies);
                 // Get user id
                 $data['User']['id_usr'] = $auth->getIdentity()->user_id;
                 if ($data['content_division'] == 0) {
                     $data['content_industry_id'] = $data['content_industry'];
                 } elseif ($data['content_group'] == 0) {
                     $data['content_industry_id'] = $data['content_division'];
                 } elseif ($data['content_class'] == 0) {
                     $data['content_industry_id'] = $data['content_group'];
                 } elseif ($data['content_class'] != 0) {
                     $data['content_industry_id'] = $data['content_class'];
                 }
                 $languages = new Default_Model_Languages();
                 if ($data['content_language'] == 0) {
                     $data['content_language'] = $this->view->language;
                 } else {
                     $data['content_language'] = $languages->getLangNameByLangId($data['content_language']);
                 }
                 $data['files'] = $_FILES['content_file_upload'];
                 // Add a new content
                 $content = new Default_Model_Content();
                 $add = $content->addContent($data);
                 if (!$add) {
                     $add_successful = false;
                 } else {
                     $add_successful = true;
                 }
                 // end if
                 $url = $this->_urlHelper->url(array('controller' => 'msg', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
                 if ($add_successful) {
                     // Get cache from registry
                     $cache = Zend_Registry::get('cache');
                     // Load most popular tags from cache
                     $output = md5(time());
                     $cache->save($output, 'LatestPostHash');
                     // Force refresh to tags on front page
                     $cache->remove('IndexTags');
                     if ($data['publish'] == 1) {
                         $url = $this->_urlHelper->url(array('content_id' => $add, 'language' => $this->view->language), 'content_shortview', true);
                         $this->_redirect($url);
                     } else {
                         /*$userpage = $this->_urlHelper->url(array('controller' => 'account',
                                                                                  			 'action' => 'view', 
                                                                                  			 'user' => $auth->getIdentity()->username, 
                         			                                                         'language' =>  $this->view->language), 
                                     			                                             'lang_default', true);
                         							$savedTab = $this->_urlHelper->url(array('controller' => 'account',
                                                                            				     'action' => 'view', 
                                                                                  			 'user' => $auth->getIdentity()->username,
                                                                                  			 'type' => 'saved',
                                                                                  			 'language' =>  $this->view->language), 
                                                                                  			 'lang_default', true);
                         
                         							$message_ok = $this->view->translate('content-save-successful');
                         							$message_ok .= ' ('.$content->getContentHeaderByContentId($add).')';
                         							$message_ok .= '<br /><br />' . $this->view->translate('content-save-successful2');
                         							$message_ok .= ' <a href="'.$userpage.'">'.$this->view->translate('content-save-successful3').'</a>';
                         							$message_ok .= ' ' . $this->view->translate('content-save-successful4');
                         							$message_ok .= ' <a href="'.$savedTab.'">'.$this->view->translate('content-save-successful5').'</a>.';
                         							$this->flash($message_ok, $url);*/
                         $url = $this->_urlHelper->url(array('controller' => 'account', 'action' => 'view', 'language' => $this->view->language, 'user' => $auth->getIdentity()->username), 'lang_default', true);
                         $this->_redirect($url);
                     }
                 } else {
                     $this->flash($message_error, $url);
                 }
             }
         }
         // end if
     } else {
         // If not logged, redirecting to system message page
         $message = 'content-add-not-logged';
         $url = $this->_urlHelper->url(array('controller' => 'msg', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
         $this->flash($message, $url);
     }
     // end if
 }