/**
  * IS: Parameter id terdeklarasi
  * FS: Mengirimkan ke viewer: material, materialLink
  * Desc: Fungsi untuk menampilkan detail promotional materials
  */
 public function detailAction()
 {
     // Param
     $id = $this->_getParam('id');
     // Model
     $materialDb = new Model_DbTable_Material();
     $materialLinkDb = new Model_DbTable_MaterialLink();
     // Data
     $material = $materialDb->getAllWithDescById($id, $this->_languageId);
     $materialLink = $materialLinkDb->getAllById($id);
     // View
     $this->view->material = $material;
     $this->view->materialLink = $materialLink;
 }
 /**
  * IS: Parameter id terdeklarasi
  * FS: Mengirimkan ke viewer: form, datamaterial, numbCtr2
  * Desc: Mengatur aksi yang dilakukan untuk halaman edit
  */
 public function editAction()
 {
     $material_id = $this->_getParam('id');
     $language_id = $this->_getParam('lang');
     $form = new Admin_Form_MaterialPromotionForm();
     $material = new Model_DbTable_Material();
     $materialdesc = new Model_DbTable_MaterialDescription();
     $materiallink = new Model_DbTable_MaterialLink();
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($_POST)) {
             $title = htmlspecialchars($_POST['materialTitle'], ENT_QUOTES);
             $desc = htmlspecialchars($_POST['materialDescription'], ENT_QUOTES);
             if ($language_id == 1) {
                 $indo = $materialdesc->checkForIndo($material_id, 1);
                 if ($indo) {
                     $data2 = array('material_id' => $material_id, 'language_id' => $language_id, 'title' => $title, 'description' => $desc);
                     $materialdesc->updateMaterial($data2, $material_id, $language_id);
                 } else {
                     $data2 = array('material_id' => $material_id, 'language_id' => $language_id, 'title' => $title, 'description' => $desc);
                     $materialdesc->insertMaterial($data2);
                 }
             } else {
                 $data = array('name' => $title);
                 $material->updateMaterial($data, $material_id);
                 $data2 = array('title' => $title, 'description' => $desc);
                 $materialdesc->updateMaterial($data2, $material_id, $language_id);
                 /** Counter jumlah material yang diupdate */
                 $numbCtr = $_POST['materialCtr'];
                 $no = 1;
                 for ($i = 1; $i <= $numbCtr; $i++) {
                     if (!empty($_POST['materialName' . $i]) and !empty($_POST['materialUrl' . $i])) {
                         $materialStack[$no]['file'] = $_POST['materialName' . $i];
                         $materialStack[$no]['link'] = $_POST['materialUrl' . $i];
                         $no++;
                     }
                 }
                 /** Get material list from database and convert to a NON ASSOC Array */
                 $listmaterial = $materiallink->getAllById($material_id);
                 if (!empty($listmaterial)) {
                     foreach ($listmaterial as $val) {
                         $oldmaterial[$val['file_number']]['file'] = $val['file'];
                         $oldmaterial[$val['file_number']]['link'] = $val['link'];
                     }
                 }
                 /**
                  * Compare new materials with old materials,
                  * if exist in new materials but not in old one, then insert
                  * if exist in old materials but not in new one, then delete
                  * if exist in both materials but different, then update
                  */
                 for ($i = 1; $i <= max(count($materialStack), count($oldmaterial)); $i++) {
                     if (!empty($materialStack[$i]) and !empty($oldmaterial[$i])) {
                         if ($materialStack[$i]['file'] != $oldmaterial[$i]['file'] or $materialStack[$i]['link'] != $oldmaterial[$i]['link']) {
                             $data3 = array('file' => $materialStack[$i]['file'], 'link' => $materialStack[$i]['link']);
                             $materiallink->updateMaterial($data3, $material_id, $i);
                         }
                     } else {
                         if (!empty($materialStack[$i]) and empty($oldmaterial[$i])) {
                             $data3 = array('material_id' => $material_id, 'file_number' => $i, 'file' => $materialStack[$i]['file'], 'link' => $materialStack[$i]['link']);
                             $materiallink->insertMaterial($data3);
                         } else {
                             if (empty($materialStack[$i]) and !empty($oldmaterial[$i])) {
                                 $materiallink->deleteMaterial($material_id, $i);
                             }
                         }
                     }
                 }
             }
             $this->loggingaction('promotionalmaterials', 'edit', $material_id);
             $this->_flash->addMessage('1\\Promotional Material Update Success!');
             $this->_redirect($this->view->rootUrl('/admin/materialpromotionindo/'));
         }
     }
     $data = $materiallink->getAllNameLinkByMaterialId($material_id);
     if ($language_id == 1) {
         $indo = $materialdesc->checkForIndo($material_id, 1);
         if ($indo) {
             $material_data = $materialdesc->getAllByIdLang($material_id);
         }
     } else {
         $material_data = $materialdesc->getAllByIdLang($material_id, 2);
     }
     $form->materialTitle->setValue($this->view->htmlDecode($material_data[0]['title']));
     $form->materialDescription->setValue($this->view->htmlDecode($material_data[0]['description']));
     /** send to view */
     $this->view->language_id = $language_id;
     $this->view->form = $form;
     $this->view->datamaterial = $data;
     $this->view->numbCtr = sizeof($data);
 }