Example #1
0
 function editAction()
 {
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $cmpId = $this->_request->getParam('id');
         if (!$cmpId) {
             $target = $this->_urlHelper->url(array('controller' => 'index', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
             $this->_redirector->gotoUrl($target);
         }
         // Get group id from campaign info.
         $cmpModel = new Default_Model_Campaigns();
         $cmp = $cmpModel->getCampaignById($cmpId)->toArray();
         $grpId = $cmp['id_grp_cmp'];
         // Only group admins get to edit campaign info.
         $grpAdminsModel = new Default_Model_GroupAdmins();
         $grpAdmins = $grpAdminsModel->getGroupAdmins($grpId);
         $userIsGroupAdmin = $this->checkIfArrayHasKeyWithValue($grpAdmins, 'id_usr', $auth->getIdentity()->user_id);
         if (!$userIsGroupAdmin) {
             $redirectUrl = $this->_urlHelper->url(array('controller' => 'campaign', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
             $this->_redirector->gotoUrl($redirectUrl);
         }
         // Get contents filenames from database
         $filesModel = new Default_Model_Files();
         $filenames = $filesModel->getFilenames($cmpId, "campaign");
         $formData['filenames'] = $filenames;
         // Create & populate the form.
         $form = new Default_Form_AddCampaignForm($this, array('mode' => 'edit', 'startdate' => $cmp['start_time_cmp'], 'fileNames' => $filenames));
         $formData = array();
         $formData['campaign_name'] = $cmp['name_cmp'];
         $formData['campaign_ingress'] = $cmp['ingress_cmp'];
         $formData['campaign_desc'] = $cmp['description_cmp'];
         $formData['campaign_start'] = $cmp['start_time_cmp'];
         if ($cmp['end_time_cmp'] != '0000-00-00') {
             $formData['campaign_end'] = $cmp['end_time_cmp'];
         }
         // Get campaign weblinks
         $campaignWeblinksModel = new Default_Model_CampaignWeblinks();
         $campaignWeblinks = $campaignWeblinksModel->getCampaignWeblinks($cmpId);
         foreach ($campaignWeblinks as $campaignWeblink) {
             $formData['weblinks_name_site' . $campaignWeblink['count_cwl']] = $campaignWeblink['name_cwl'];
             $formData['weblinks_url_site' . $campaignWeblink['count_cwl']] = $campaignWeblink['url_cwl'];
         }
         $form->populate($formData);
         $this->view->form = $form;
         $this->view->cmpName = $cmp['name_cmp'];
         // If the form is posted and valid, save the changes to db.
         $request = $this->getRequest();
         if ($request->isPost()) {
             $post = $request->getPost();
             if ($form->isValid($post)) {
                 // Change existing group info.
                 if (isset($post['campaign_start'])) {
                     $cmpModel->editCampaign($cmpId, $post['campaign_name'], $post['campaign_ingress'], $post['campaign_desc'], $post['campaign_start'], $post['campaign_end']);
                 } else {
                     $cmpModel->editCampaign($cmpId, $post['campaign_name'], $post['campaign_ingress'], $post['campaign_desc'], 'keepExisting', $post['campaign_end']);
                 }
                 // Set weblinks
                 if (isset($post['weblinks_name_site1']) && isset($post['weblinks_url_site1'])) {
                     $campaignWeblinksModel->setWeblink($cmpId, $post['weblinks_name_site1'], $post['weblinks_url_site1'], 1);
                 }
                 if (isset($post['weblinks_name_site2']) && isset($post['weblinks_url_site2'])) {
                     $campaignWeblinksModel->setWeblink($cmpId, $post['weblinks_name_site2'], $post['weblinks_url_site2'], 2);
                 }
                 if (isset($post['weblinks_name_site3']) && isset($post['weblinks_url_site3'])) {
                     $campaignWeblinksModel->setWeblink($cmpId, $post['weblinks_name_site3'], $post['weblinks_url_site3'], 3);
                 }
                 if (isset($post['weblinks_name_site4']) && isset($post['weblinks_url_site4'])) {
                     $campaignWeblinksModel->setWeblink($cmpId, $post['weblinks_name_site4'], $post['weblinks_url_site4'], 4);
                 }
                 if (isset($post['weblinks_name_site5']) && isset($post['weblinks_url_site5'])) {
                     $campaignWeblinksModel->setWeblink($cmpId, $post['weblinks_name_site5'], $post['weblinks_url_site5'], 5);
                 }
                 $filesModel = new Default_Model_Files();
                 $files = $_FILES['content_file_upload'];
                 $filesModel->newFiles($cmpId, "campaign", $files);
                 if (isset($post['uploadedFiles'])) {
                     $filesModel->deleteCertainFiles($cmpId, "campaign", $post['uploadedFiles']);
                 }
                 // Redirect back to the campaign page.
                 $target = $this->_urlHelper->url(array('cmpid' => $cmpId, 'language' => $this->view->language), 'campaign_view', true);
                 $this->_redirector->gotoUrl($target);
             }
         }
     } else {
         // Not logged in.
         $target = $this->_urlHelper->url(array('controller' => 'groupsandcampaigns', 'action' => 'index', 'language' => $this->view->language), 'lang_default', true);
         $this->_redirector->gotoUrl($target);
     }
 }