public function addAction()
 {
     $this->layout('layout/apotravinyadmin');
     $form = new CrowlerForm();
     // $form->setInputFilter(new CrowlerFilter());
     $data = $this->getServiceLocator()->get('PayoutypeTable')->gettype();
     if (is_array($data) and !empty($data)) {
         $datatypetmp = $data;
     } else {
         $datatypetmp = Null;
     }
     $form->settype($datatypetmp);
     if ($this->getRequest()->isPost()) {
         $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
         $form->setData($data);
         if (!$form->isValid()) {
             return new ViewModel(array('error' => true, 'form' => $form));
         } else {
             $exchange_data = array('namepackge' => $data['namepackge'], 'price' => $data['price'], 'id_user' => $id_user, 'type' => $data['type'], 'DKCpmUni' => $data['DKCpmUni'], 'DKCpmRaw' => $data['DKCpmRaw'], 'MBCpmUni' => $data['MBCpmUni'], 'MBCpmRaw' => $data['MBCpmRaw'], 'code' => $data['code'], 'hotstring' => $data['hotstring']);
             $Crowler = new Crowler();
             $Crowler->exchangeArray($exchange_data);
             $this->getServiceLocator()->get('CrowlerTable')->save($Crowler);
             return $this->redirect()->toRoute('Crowler', array('controller' => 'product', 'action' => 'list'));
         }
     }
     return new ViewModel(array('form' => $form));
 }
 public function editAction()
 {
     $dataPayout = $this->getServiceLocator()->get('PayoutypeTable')->gettype();
     $utility = new Utility();
     $product = new Crowler();
     $view = new ViewModel();
     $dbAdapter = $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter');
     $getuser = $this->forward()->dispatch('Admin\\Controller\\Index', array('action' => 'getuser'));
     $this->layout()->getuser = $getuser;
     if ($getuser && ($getuser->group == 'admin' || $getuser->group == 'supperadmin')) {
         $this->layout('layout/apotravinyadmin');
         $form = new CrowlerForm($dbAdapter);
         //$form->setInputFilter ( new ProductFilter () );
         $catalogarr = $this->getdataAction();
         $form->setId($catalogarr);
         $tags = $this->getTagsAction();
         $form->settagsId($tags);
         // 			$manufacturer = array ();
         // 			$form->setmanufacturerId ( $manufacturer );
         $request = $this->getRequest();
         if ($request->isPost()) {
             //$form->setInputFilter ( $product->getInputFilter () ); // check validate
             $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
             //print_r($data);die;
             $form->setData($data);
             // error rat nguy hiem o day ko vaidate form
             if ($form->isValid()) {
                 // print_r($data);die;
                 if ($data['img']['name'] != '') {
                     // edit anh
                     $_array_img = $data['img'];
                     // Recyle Bin img older
                     $id = $data['id'];
                     $getcata = $this->getServiceLocator()->get('ProductTable')->get($id);
                     if ($getcata) {
                         $get_img_older = $getcata->img;
                         $_dir = UPLOAD_PATH_IMG;
                         if ($get_img_older) {
                             $utility->deleteImage($get_img_older, $_dir);
                         }
                     }
                     // upload and rename
                     $renname_file_img = $utility->uploadImageAlatca($_array_img);
                     if (!$renname_file_img) {
                         $view->check = 0;
                         return $view;
                     }
                     $product = new Product();
                     $product->dataArraySwap($data, $renname_file_img);
                     $check = $this->getServiceLocator()->get('ProductTable')->save($product);
                     if ($check != 0) {
                         $_url = WEBPATH . '/crowler/index/index';
                         $this->redirect()->toUrl($_url);
                     } else {
                         $view->check = 0;
                         return $view;
                     }
                 } else {
                     $id = $data['id'];
                     $getcata = $this->getServiceLocator()->get('ProductTable')->get($id);
                     if ($getcata) {
                         $get_img_older = $getcata->img;
                     }
                     $product = new Product();
                     $product->dataArraySwap($form->getData(), $get_img_older);
                     $check = $this->getServiceLocator()->get('ProductTable')->save($product);
                     $_url = WEBPATH . '/crowler/index/index';
                     if ($check == 0) {
                         // notupdate
                         $_url = WEBPATH . '/crowler/index/index';
                         $this->redirect()->toUrl($_url);
                     }
                     if ($check != 0) {
                         $_url = WEBPATH . '/crowler/index/index';
                         $this->redirect()->toRoute('Crowler', array('controller' => 'index', 'action' => 'index'));
                     }
                 }
             } else {
                 echo "not valid form";
             }
         }
         $form->setId($catalogarr);
         $product_id = (int) $this->params()->fromRoute('id', 0);
         $catalog_cr = $this->getServiceLocator()->get('ProductTable')->get($product_id);
         $form->settagsId($tags);
         $Tags_cr = $this->getServiceLocator()->get('ProductTable')->get($product_id);
         $form->bind($Tags_cr);
         if ($product_id == 0) {
             $form->get('submit')->setAttribute('value', 'Add Product');
         } else {
             $form->get('submit')->setAttribute('value', 'Edit Product');
             $view->setVariable('error', 1);
         }
         if ($catalog_cr and $product_id != 0) {
             $form->bind($catalog_cr);
             $_img_thumb = $catalog_cr->img;
             $_img_thumb1 = $catalog_cr->img1;
             $_img_thumb2 = $catalog_cr->img2;
             $_img_thumb3 = $catalog_cr->img3;
             $_img_thumb4 = $catalog_cr->img4;
             $_img_thumb5 = $catalog_cr->img5;
             $_img_thumb6 = $catalog_cr->img6;
             $_img_thumb7 = $catalog_cr->img7;
             $_img_thumb8 = $catalog_cr->img8;
             $_img_thumb9 = $catalog_cr->img9;
             $_img_thumb10 = $catalog_cr->img0;
         }
         $view->setVariable('img_thumb', $_img_thumb);
         $view->setVariable('img_thumb1', $_img_thumb1);
         $view->setVariable('img_thumb2', $_img_thumb2);
         $view->setVariable('img_thumb3', $_img_thumb3);
         $view->setVariable('img_thumb4', $_img_thumb4);
         $view->setVariable('img_thumb5', $_img_thumb5);
         $view->setVariable('img_thumb6', $_img_thumb6);
         $view->setVariable('img_thumb7', $_img_thumb7);
         $view->setVariable('img_thumb8', $_img_thumb8);
         $view->setVariable('img_thumb9', $_img_thumb9);
         $view->setVariable('img_thumb10', $_img_thumb10);
         $view->setVariable('Crowlerform', $form);
         $view->setVariable('id_product', $product_id);
         return $view;
     } else {
         $view->check = 2;
         $this->layout('error/admin');
     }
 }