/**
  * IS: Parameter id terdeklarasi
  * FS: Mengirimkan ke viewer: form, image, require_image, news_id
  * Desc: Mengatur aksi yang dilakukan untuk halaman edit
  */
 public function editAction()
 {
     $language_id = $this->_getParam('lang');
     $news_id = $this->_getParam('id');
     $form = new Admin_Form_NewsForm();
     $table_news = new Model_DbTable_News();
     $table_newsDesc = new Model_DbTable_NewsDesc();
     $table_poitonews = new Model_DbTable_PoiToNews();
     $table_related_article_news = new Model_DbTable_RelatedArticleNews();
     // untuk menampilkan list related link article yang berhubungan dengan
     // content news
     $related_news = $table_related_article_news->getByNewsId($news_id, $language_id);
     $this->view->edit = 1;
     $this->view->ralated_news = $related_news;
     $this->view->news_id = $news_id;
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($_POST)) {
             /** Preparing the date insert */
             if ($language_id == 1) {
                 // echo 'SINI';
                 // exit;
                 $indo = $table_newsDesc->checkForIndo($news_id, $language_id);
                 if ($indo) {
                     $title = preg_replace("/'/", "'", $_POST['newsTitle']);
                     $content = preg_replace("/'/", "'", $_POST['newsContent']);
                     $data = array('news_id' => $news_id, 'language_id' => $language_id, 'title' => $title, 'content' => $content);
                     $table_newsDesc->updateNewsDescription($data, $news_id, $language_id);
                 } else {
                     $title = preg_replace("/'/", "'", $_POST['newsTitle']);
                     $content = preg_replace("/'/", "'", $_POST['newsContent']);
                     $data = array('news_id' => $news_id, 'language_id' => $language_id, 'title' => $title, 'content' => $content);
                     $table_newsDesc->insertNewsDescription($data);
                 }
             } else {
                 $tUnixTime = time();
                 date_default_timezone_set('Asia/Jakarta');
                 $GMTMySqlString = date("Y-m-d H:i:s", $tUnixTime);
                 $GMTMySqlTimeString = date("H:i:s", $tUnixTime);
                 $publish_date = $_POST['newsPublishDate'] . ' ' . $GMTMySqlTimeString;
                 if ($_POST['newsImage']['name'] == '') {
                     $data = array('news_date' => $GMTMySqlString, 'publish_date' => $publish_date, 'showing' => $_POST['newsStatus']);
                     $this->_flash->addMessage('3\\Warning: Data is saved without image!');
                 } else {
                     $image = preg_replace("/'/", "'", $_POST['newsImage']);
                     $data = array('picture' => $image, 'news_date' => $GMTMySqlString, 'publish_date' => $publish_date, 'showing' => $_POST['newsStatus']);
                 }
                 $table_news->updateNews($data, $news_id);
                 $title = preg_replace("/'/", "'", $_POST['newsTitle']);
                 $content = preg_replace("/'/", "'", $_POST['newsContent']);
                 $hasIndoDesc = $table_newsDesc->checkForIndo($news_id, $language_id);
                 if (empty($hasIndoDesc)) {
                     $data = array('news_id' => $news_id, 'language_id' => $language_id, 'title' => $title, 'content' => $content);
                     $table_newsDesc->insertNewsDescription($data);
                 } else {
                     $data = array('title' => $title, 'content' => $content);
                     $table_newsDesc->updateNewsDescription($data, $news_id, $language_id);
                 }
                 $poi_count = $_POST['PoiCounter'];
                 $poi_stack = array();
                 for ($i = 0; $i < $poi_count; $i++) {
                     if (!empty($_POST['poiValue' . $i])) {
                         array_push($poi_stack, $_POST['poiValue' . $i]);
                     }
                 }
                 /**
                  * Get related poi list from the database
                  * and convert to a NON ASSOC Array
                  */
                 $poi_list = $table_poitonews->getAllPoiByNewsId($news_id, $language_id);
                 $saved_poi = array();
                 if (!empty($poi_list)) {
                     foreach ($poi_list as $temp) {
                         array_push($saved_poi, $temp['poi_id']);
                     }
                 }
                 /**
                  * Check from the post list, compare with saved poi;
                  * if not exist then insert the new poi
                  */
                 foreach ($poi_stack as $poi_id) {
                     if (!in_array($poi_id, $saved_poi)) {
                         $data = array('poi_id' => $poi_id, 'news_id' => $news_id);
                         $table_poitonews->insertPoiNews($data);
                     }
                 }
                 /**
                  * Do the complementary process,
                  * if the old poi did not exist in the new list
                  * then delete the old one
                  */
                 foreach ($saved_poi as $old_poi) {
                     if (!in_array($old_poi, $poi_stack)) {
                         $table_poitonews->deletePoiNews($news_id, $old_poi);
                     }
                 }
             }
             /* update related article */
             // this part add related
             if ($_POST['counterRelated'] > 0) {
                 $counter = $_POST['counterRelated'];
                 for ($i = 1; $i <= $counter; $i++) {
                     if (isset($_POST['label' . $i])) {
                         //cek existing data ke database
                         // jika data link dan label yang dikirim dari viwer blm ada di database, maka akan di simpan sebagai data baru
                         //cek akan bernilai true jika data blm ada didatabase
                         $cek = $table_related_article_news->cek_existing($_POST['label' . $i], $_POST['link' . $i], $language_id);
                         // print_r($cek);
                         if (!$cek or $cek == false) {
                             $data = array('news_id' => $news_id, 'label' => $_POST['label' . $i], 'link' => $_POST['link' . $i], 'language_id' => $language_id);
                             //print_r($data);
                             $table_related_article_news->insertRelated($data);
                         }
                     }
                 }
                 //exit;
             }
             // part untuk menghapus data dari database
             // bagian inin akan dijalankan apabila ada request
             // delete dari viewer
             if ($_POST['counterDel'] > 0) {
                 $counterDel = $_POST['counterDel'];
                 for ($i = 1; $i <= $counterDel; $i++) {
                     if (isset($_POST['labeldel' . $i])) {
                         // cek terlebih dahulu ke database
                         // jika data yang sesuai dengan data yg dikirmkan ada
                         // maka lakukan penghapusan
                         $cek = $table_related_article_news->cek_existing_forDel($_POST['labeldel' . $i], $_POST['linkdel' . $i], $language_id);
                     }
                 }
             }
             $this->loggingaction('news', 'edit', $news_id, $language_id);
             $this->_flash->addMessage("1\\News Update Success!");
             $this->_redirect($this->view->rootUrl('/admin/newsindo/'));
         }
     }
     $image = $table_news->getPictureById($news_id);
     if ($image == '') {
         $this->view->require_image = TRUE;
     } else {
         $this->view->image = $image;
     }
     //        di uncomment
     $setContent = true;
     if ($language_id == 1) {
         $indo = $table_newsDesc->checkForIndo($news_id, $language_id);
         if ($indo) {
             $data = $table_news->getAllWithDescById($news_id, $language_id);
             //                di uncomment
             $setContent = true;
         } else {
             $data = $table_news->getAllWithDescById($news_id, 2);
         }
     } else {
         $data = $table_news->getAllWithDescById($news_id, $language_id);
         //                        di uncomment
         if (empty($data)) {
             $setContent = true;
             //karena belum ada terjemahan dlm bahasa ini jadi form deskripsi dikosongkan
             $data = $table_news->getAllWithDescById($news_id, 1);
         }
     }
     $news_count = $table_poitonews->countPoiNewsByNewsId($news_id);
     $date = explode(' ', $data['publish_date']);
     $form->newsTitle->setValue($data['title']);
     //        if($setContent)
     if ($language_id == 2) {
         $form->newsContent->setValue($data['content']);
     } else {
         $form->newsContent->setValue('');
     }
     $form->newsImage->setValue($data['picture']);
     $form->newsPublishDate->setValue($date[0]);
     $form->PoiCounter->setValue($news_count);
     //        $this->view->language_id = $language_id;
     $this->view->form = $form;
     $this->view->news_id = $news_id;
 }
 /**
  * IS: Parameter id terdeklarasi
  * FS: Mengirimkan ke viewer: form, image, require_image, news_id
  * Desc: Mengatur aksi yang dilakukan untuk halaman edit
  */
 public function editAction()
 {
     $language_id = $this->_getParam('lang');
     $news_id = $this->_getParam('id');
     $form = new Admin_Form_NewsForm();
     $table_news = new Model_DbTable_News();
     $table_newsDesc = new Model_DbTable_NewsDesc();
     $table_poitonews = new Model_DbTable_PoiToNews();
     $table_related_article_news = new Model_DbTable_RelatedArticleNews();
     if ($this->_userInfo->canApprove) {
         $form->submit->setLabel('Update');
     } else {
         $form->submit->setLabel('Update for review');
     }
     // untuk menampilkan list related link article yang berhubungan dengan
     // content news
     $related_news = $table_related_article_news->getByNewsId($news_id, $language_id);
     $this->view->edit = 1;
     $this->view->ralated_news = $related_news;
     $this->view->news_id = $news_id;
     //echo count($related_news);
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($_POST)) {
             $status = Model_DbTable_News::DRAFT;
             if ($this->getRequest()->getPost('action') == 'Update' || $this->getRequest()->getPost('action') == 'Update for review') {
                 if ($this->_userInfo->canApprove) {
                     $status = Model_DbTable_News::PUBLISH;
                 } else {
                     $status = Model_DbTable_News::PENDING;
                 }
             }
             /** Preparing the date insert */
             if ($language_id != 1) {
                 $indo = $table_newsDesc->checkForIndo($news_id);
                 if ($indo) {
                     $title = preg_replace("/'/", "&#39;", $_POST['newsTitle']);
                     $content = preg_replace("/'/", "&#39;", $_POST['newsContent']);
                     $data = array('news_id' => $news_id, 'language_id' => $language_id, 'title' => $title, 'content' => $content);
                     $table_newsDesc->updateNewsDescription($data, $news_id, $language_id);
                 } else {
                     $title = preg_replace("/'/", "&#39;", $_POST['newsTitle']);
                     $content = preg_replace("/'/", "&#39;", $_POST['newsContent']);
                     $data = array('news_id' => $news_id, 'language_id' => $language_id, 'title' => $title, 'content' => $content);
                     $table_newsDesc->insertNewsDescription($data);
                 }
             } else {
                 $tUnixTime = time();
                 date_default_timezone_set('Asia/Jakarta');
                 $GMTMySqlString = date("Y-m-d H:i:s", $tUnixTime);
                 $GMTMySqlTimeString = date("H:i:s", $tUnixTime);
                 $publish_date = $_POST['newsPublishDate'] . ' ' . $GMTMySqlTimeString;
                 $data = array('updated_at' => $GMTMySqlString, 'updated_by' => $this->_userInfo->id, 'status' => $status);
                 if ($form->image->isUploaded()) {
                     $form->image->setDestination(UPLOAD_FOLDER . 'news/');
                     if ($form->image->receive()) {
                         $data['picture'] = $form->image->getFileName('image', false);
                     }
                 }
                 $table_news->updateNews($data, $news_id);
                 $title = preg_replace("/'/", "&#39;", $_POST['newsTitle']);
                 $content = preg_replace("/'/", "&#39;", $_POST['newsContent']);
                 $data = array('title' => $title, 'content' => $content);
                 $table_newsDesc->updateNewsDescription($data, $news_id, $language_id);
                 $poi_count = $_POST['PoiCounter'];
                 $poi_stack = array();
                 for ($i = 0; $i < $poi_count; $i++) {
                     if (!empty($_POST['poiValue' . $i])) {
                         array_push($poi_stack, $_POST['poiValue' . $i]);
                     }
                 }
                 /**
                  * Get related poi list from the database
                  * and convert to a NON ASSOC Array
                  */
                 $poi_list = $table_poitonews->getAllPoiByNewsId($news_id, $language_id);
                 $saved_poi = array();
                 if (!empty($poi_list)) {
                     foreach ($poi_list as $temp) {
                         array_push($saved_poi, $temp['poi_id']);
                     }
                 }
                 /**
                  * Check from the post list, compare with saved poi;
                  * if not exist then insert the new poi
                  */
                 foreach ($poi_stack as $poi_id) {
                     if (!in_array($poi_id, $saved_poi)) {
                         $data = array('poi_id' => $poi_id, 'news_id' => $news_id);
                         $table_poitonews->insertPoiNews($data);
                     }
                 }
                 /**
                  * Do the complementary process,
                  * if the old poi did not exist in the new list
                  * then delete the old one
                  */
                 foreach ($saved_poi as $old_poi) {
                     if (!in_array($old_poi, $poi_stack)) {
                         $table_poitonews->deletePoiNews($news_id, $old_poi);
                     }
                 }
             }
             /* update related article */
             // this part add related
             if ($_POST['counterRelated'] > 0) {
                 $counter = $_POST['counterRelated'];
                 for ($i = 1; $i <= $counter; $i++) {
                     //cek existing data ke database
                     // if jika data link dan label yang dikirim dari viwer
                     // blm ada di database, maka akan di simpan
                     if (isset($_POST['label' . $i])) {
                         $cek = $table_related_article_news->cek_existing($_POST['label' . $i], $_POST['link' . $i], $language_id);
                         //print_r($cek);
                         if (!$cek) {
                             // echo $_POST['label'.$i];
                             $data = array('news_id' => $news_id, 'label' => $_POST['label' . $i], 'link' => $_POST['link' . $i], 'language_id' => $language_id);
                             $table_related_article_news->insertRelated($data);
                         }
                     }
                 }
             }
             // part untuk menghapus data dari database
             // bagian inin akan dijalankan apabila ada request
             // delete dari viewer
             if ($_POST['counterDel'] > 0) {
                 $counterDel = $_POST['counterDel'];
                 for ($i = 1; $i <= $counterDel; $i++) {
                     if (isset($_POST['labeldel' . $i])) {
                         // cek terlebih dahulu ke database
                         // jika data yang sesuai dengan data yg dikirmkan ada
                         // maka lakukan penghapusan
                         $cek = $table_related_article_news->cek_existing_forDel($_POST['labeldel' . $i], $_POST['linkdel' . $i], $language_id);
                     }
                 }
             }
             $this->loggingaction('news', 'edit', $news_id, $language_id);
             $this->_flash->addMessage("1\\News Update Success!");
             $this->_redirect($this->view->rootUrl('/admin/news/'));
         }
     }
     //$this->_helper->layout->disableLayout(); /* disable layout */
     //$this->_helper->viewRenderer->setNoRender(true); /* supaya tidak render view */
     $image = $table_news->getPictureById($news_id);
     if ($image == '') {
         $this->view->require_image = TRUE;
     } else {
         $this->view->image = $image;
     }
     if ($language_id != 1) {
         $indo = $table_newsDesc->checkForIndo($news_id);
         if ($indo) {
             $data = $table_news->getAllWithDescById($news_id, $language_id);
         } else {
             $data = $table_news->getAllWithDescById($news_id, 1);
         }
     } else {
         $data = $table_news->getAllWithDescById($news_id, $language_id);
     }
     //print_r($data);
     //echo $data['publish_date'];
     $news_count = $table_poitonews->countPoiNewsByNewsId($news_id);
     $date = explode(' ', $data['publish_date']);
     if ($language_id == 1) {
         $form->newsTitle->setValue($data['title']);
         $form->newsContent->setValue($data['content']);
     }
     if ($language_id == 2) {
         $news = $table_news->getAllWithDescById($news_id, $language_id);
         if (isset($news)) {
             $form->newsTitle->setValue($news['title']);
             $form->newsContent->setValue($news['content']);
         }
     }
     $this->view->picture = $data['picture'];
     $form->newsPublishDate->setValue($date[0]);
     $form->PoiCounter->setValue($news_count);
     $this->view->langId = $this->_getParam('lang');
     $this->view->form = $form;
     $this->view->news_id = $news_id;
 }