icefusion

Untitled

Dec 12th, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.11 KB | None | 0 0
  1. CONTROLLER
  2.  
  3. <?php
  4.  
  5. class Admin_SkuoptionsController extends Zend_Controller_Action {
  6.  
  7.     private $messages;
  8.     private $obrigatorios;
  9.     private $nomeFront;
  10.     private $form;
  11.     private $editData;
  12.     private $session;
  13.  
  14.     public function init() {
  15.         if (!Zend_Auth::getInstance()->hasIdentity()) {
  16.             return $this->redirect('/admin/auth');
  17.         }
  18.         $this->messages = new Icefusion_View_Helper_General_Message();
  19.         $this->session = new Icefusion_View_Helper_General_Session();
  20.         $this->obrigatorios = array('sku_id' => 'Sku', 'name' => 'Nome');
  21.         $this->nomeFront = array('sku_id' => 'Sku', 'name' => 'Nome', 'color' => 'Cor',);
  22.         $this->form = new Icefusion_View_Helper_Admin_Forms_SkuOptions();
  23.         $url = new Icefusion_View_Helper_General_FullUrl();
  24.         $this->view->cadastrar = $url->FullUrl('admin/skuoptions/add');
  25.         $this->editData = new Zend_Session_Namespace('adm');
  26.     }
  27.  
  28.     public function indexAction() {
  29.         $this->redirect('admin/skuoptions/list');
  30.     }
  31.  
  32.     public function addAction() {
  33.         $this->view->form = $this->form->getFormCadastro($this->editData->data);
  34.         unset($this->editData->data);
  35.         $this->view->message = $this->messages->adminMessageSession->message;
  36.         unset($this->messages->adminMessageSession->message);
  37.         unset($this->session->adminSession->session);
  38.     }
  39.  
  40.     public function listAction() {
  41.         if ($this->_request->isPost()) {
  42.             $data = $this->_request->getPost();
  43.             $actionUrl = explode('/', $data['gridAction_id']);
  44.             $action = end($actionUrl);
  45.             switch ($action) {
  46.                 case 'edit':
  47.                     $this->edit($data, $data['gridAction_id']);
  48.                     break;
  49.                 case 'delete':
  50.                     $this->delete($data, $data['gridAction_id']);
  51.                     break;
  52.             }
  53.         } else {
  54.             $report = new Icefusion_View_Helper_Admin_Reports_SkuOptions();
  55.             $this->view->report = $report->getReportTable();
  56.         }
  57.     }
  58.  
  59.     public function saveAction() {
  60.         $form = new Icefusion_View_Helper_Admin_Forms_SkuOptions();
  61.         if ($this->_request->isPost()) {
  62.             $data = $this->_request->getPost();
  63.             $invalido = $form->isValidated($data, $this->obrigatorios);
  64.             if (!isset($invalido) || count($invalido) == 0) {
  65.                 unset($data['submit']);
  66.                 unset($data['MAX_FILE_SIZE']);
  67.                 $files = $_FILES;
  68.                 $sku = new Application_Model_DbTable_SkuOptions();
  69.                 if ($data['id'] == '') {
  70.                     unset($data['id']);
  71.                     $data['img'] = null;
  72.                     try {
  73.                         $lastId = $sku->insert($data);                        
  74.                         unset($this->messages->adminMessageSession->message);
  75.                         $this->view->message = "";
  76.                         $this->messages->successSave();
  77.                         $this->session->adminSession->lastId = $lastId;
  78.                         $this->upJpg($files['arquivojpg'], $lastId);
  79.                         ///$this->upSwf($files['arquivoswf'], $order, $swfref);
  80.                         $this->redirect("admin/skuoptions/add");
  81.                     } catch (Exception $e) {
  82.                         var_dump($e);
  83.                         exit;
  84.                     }
  85.                 } else {
  86.                     $where = 'id = ' . $data['id'];
  87.                     $lastId = $sku->update($data, $where);
  88.                     unset($this->messages->adminMessageSession->message);
  89.                     $this->view->message = "";
  90.                     $this->messages->successEdit();
  91.                     $this->session->adminSession->lastId = $lastId;
  92.                     $this->redirect("admin/skuoptions/add");
  93.                 }
  94.             } else {
  95.                 unset($this->messages->adminMessageSession->message);
  96.                 $this->view->message = "";
  97.                 $this->messages->errorSave($invalido, $this->nomeFront);
  98.                 $this->redirect("admin/skuoptions/add");
  99.             }
  100.         }
  101.     }
  102.  
  103.     private function edit($postMassIds, $retorno) {
  104.         $ids = explode(',', $postMassIds['postMassIdsid']);
  105.         if (count($ids) == 1) {
  106.             $model = new Application_Model_SkuOptions();
  107.             $data = $model->getDataById($ids[0]);
  108.             $this->editData->data = $data;
  109.             $this->_redirect('admin/skuoptions/add');
  110.         } else {
  111.             $this->_redirect($retorno);
  112.         }
  113.     }
  114.  
  115.     private function delete($data, $retorno) {
  116.         $ids = explode(',', $data['postMassIdsid']);
  117.         $categoria = new Application_Model_DbTable_SkuOptions();
  118.         foreach ($ids as $id) {
  119.             try {
  120.                 $where = 'id = ' . $id;
  121.                 $categoria->delete($where);
  122.             } catch (Exception $e) {
  123.                 print_r($e);
  124.                 die;
  125.             }
  126.         }
  127.         $this->_redirect($retorno);
  128.     }
  129.  
  130.     private function upJpg($data, $order) {                      
  131.         $jpg = '../../img/products/' . $order;    
  132.         $upload = new Icefusion_View_Helper_General_UploadImagens();
  133.         $upload->upload('jpg', $data, $order, '', $jpg);
  134.     }
  135.    
  136.     private function upSwf($data, $order, $swfref) {                              
  137.         $swf = '../../img/products/' . $swfref;
  138.         $upload = new Icefusion_View_Helper_General_UploadImagens();
  139.         $upload->upload('swf', $data, $order, '', $swf);
  140.     }
  141. }
  142.  
  143. FORM
  144. <?php
  145.  
  146. class Icefusion_View_Helper_Admin_Forms_SkuOptions extends Zend_Form{
  147.  
  148.     public function getFormCadastro($data) {
  149.         $form = new Zend_Form;
  150.  
  151.         $form->setMethod('post');
  152.         $form->setAction('/admin/skuoptions/save');    
  153.         $form->setAttrib('enctype', 'multipart/form-data');
  154.         $form->setAttrib('id', 'form-application');
  155.        
  156.         $sku = new Zend_Form_Element_Select('sku_id', array('label' => 'Sku:', 'class' => 'select2 borda-arredondada'));
  157.         $sku->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row2'))));
  158.         $skuList = new Application_Model_Sku();
  159.  
  160.         $result = $skuList->fetchAll('id > 0','name')->toArray();
  161.         $options = array();
  162.         foreach ($result as $value) {
  163.             $options[$value['id']] = $value['name'];
  164.         }
  165.  
  166.         $sku->setMultiOptions($options);
  167.         $sku->setRequired(true);
  168.         $form->addElement($sku);
  169.        
  170.         $nome = $form->createElement('text', 'name', array('label' => 'Nome:', 'class' => 'campos4 borda-arredondada'))->setRequired(true);
  171.         $nome->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row4'))));
  172.         $form->addElement($nome);
  173.        
  174.         $color = $form->createElement('text', 'color', array('label' => 'Cor:', 'class' => 'campos1 borda-arredondada'))->setRequired(true);
  175.         $color->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row1'))));
  176.         $form->addElement($color);        
  177.        
  178.         $arquivojpg = new Zend_Form_Element_File('arquivojpg');
  179.         $arquivojpg->setLabel('Enviar Perspectiva JPG:');        
  180.         $arquivojpg->addValidator('Count', false, 1);                
  181.         $arquivojpg->addValidator('Extension', false, 'jpg,png,gif');
  182.         $arquivojpg->addDecorators(array('File', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row3'))));
  183.         $form->addElement($arquivojpg);
  184.        
  185.         $arquivoswf = new Zend_Form_Element_File('arquivoswf');
  186.         $arquivoswf->setLabel('Enviar Perspectiva SWF:');        
  187.         $arquivoswf->addValidator('Count', false, 1);        
  188.         //$arquivoswf->addValidator('Extension', false, 'swf');
  189.         $arquivoswf->addDecorators(array('File', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row3'))));
  190.         $form->addElement($arquivoswf);
  191.  
  192.         $id = $form->createElement('hidden', 'id');
  193.         $form->addElement($id);
  194.  
  195.         $submit = $form->createElement('submit', 'submit', array('label' => 'Salvar', 'class' => 'btn btn-salvar'));
  196.         $submit->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row2'))));
  197.         $form->addElement($submit);
  198.  
  199.         $limpar = $form->createElement('button', 'button', array('label' => 'Limpar', 'class' => 'btn btn-limpar'));
  200.         $limpar->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row2'))));
  201.         $form->addElement($limpar);
  202.  
  203.         if ($data) {
  204.             $form->populate($data);
  205.         }
  206.  
  207.         return $form;
  208.  
  209.         //$form->addElement('hash', 'csrf');
  210.     }  
  211.  
  212.     public function isValidated($data, $obrigatorios) {
  213.         $validacoes = new Icefusion_View_Helper_General_Validacoes();
  214.         return $validacoes->validarRequeridos($data, $obrigatorios);
  215.     }
  216. }
  217.  
  218. UPLOAD
  219. class Icefusion_View_Helper_General_UploadImagens extends Zend_View_Helper_Abstract {
  220.  
  221.     public function upload($type, $data, $order, $swfref, $path) {
  222.         switch ($type) {
  223.             case 'jpg':
  224.                 $this->jpgUpload($data, $order, $path);
  225.                 break;
  226.             case 'swf':
  227.                 $this->swfUpload($data, $order, $swfref, $path);
  228.                 break;
  229.             default :
  230.                 break;
  231.         }
  232.     }
  233.  
  234.     private function jpgUpload($data, $order, $path) {
  235.         $imageAdapterJPG = new Zend_File_Transfer_Adapter_Http();        
  236.         try {
  237.             if (file_exists($path)) {
  238.                 $diretorio = dir($path);
  239.                 while ($arquivo = $diretorio->read()) {
  240.                     unlink($path . '/' . $arquivo);
  241.                 }
  242.                 $diretorio->close();
  243.                 $imageAdapterJPG->setDestination($path);
  244.                 if (is_uploaded_file($data['tmp_name'])) {
  245.                     if (!$imageAdapterJPG->receive()) {
  246.                         die('Erro - Arquivo Não Recebido');
  247.                     }
  248.                 } else {
  249.                     die('Not Upload');
  250.                 }
  251.             } else {
  252.                 if (mkdir($path, 0777)) {
  253.                     $imageAdapterJPG->setDestination($path);
  254.                     if (is_uploaded_file($data['tmp_name'])) {
  255.                         if (!$imageAdapterJPG->receive()) {
  256.                             die('Erro - Arquivo Não Recebido');
  257.                         }
  258.                     } else {
  259.                         die('Not Upload');
  260.                     }
  261.                 }
  262.             }
  263.         } catch (Exception $e) {
  264.             var_dump($e);
  265.             exit;
  266.         }
  267.     }
  268.  
  269.     private function swfUpload($data, $order, $swfref, $path) {
  270.         $imageAdapterSWF = new Zend_File_Transfer_Adapter_Http();
  271.         try {
  272.             if (file_exists($path)) {
  273.                 $diretorio = dir($path);
  274.                 while ($arquivo = $diretorio->read()) {
  275.                     unlink($path . '/' . $arquivo);
  276.                 }
  277.                 $diretorio->close();
  278.                 $imageAdapterSWF->setDestination($path);
  279.                 if (is_uploaded_file($data['tmp_name'])) {
  280.                     if (!$imageAdapterSWF->receive()) {
  281.                         die('Erro - Arquivo Não Recebido');
  282.                     }
  283.                 } else {
  284.                     die('Not Upload');
  285.                 }
  286.             } else {
  287.                 if (mkdir($path, 0777)) {
  288.                     $imageAdapterSWF->setDestination($path);
  289.                     if (is_uploaded_file($data['tmp_name'])) {
  290.                         if (!$imageAdapterSWF->receive()) {
  291.                             die('Erro - Arquivo Não Recebido');
  292.                         }
  293.                     } else {
  294.                         die('Not Upload');
  295.                     }
  296.                 }
  297.             }
  298.         } catch (Exception $e) {
  299.             var_dump($e);
  300.             exit;
  301.         }
  302.     }
  303.  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment