Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CONTROLLER
- <?php
- class Admin_SkuoptionsController extends Zend_Controller_Action {
- private $messages;
- private $obrigatorios;
- private $nomeFront;
- private $form;
- private $editData;
- private $session;
- public function init() {
- if (!Zend_Auth::getInstance()->hasIdentity()) {
- return $this->redirect('/admin/auth');
- }
- $this->messages = new Icefusion_View_Helper_General_Message();
- $this->session = new Icefusion_View_Helper_General_Session();
- $this->obrigatorios = array('sku_id' => 'Sku', 'name' => 'Nome');
- $this->nomeFront = array('sku_id' => 'Sku', 'name' => 'Nome', 'color' => 'Cor',);
- $this->form = new Icefusion_View_Helper_Admin_Forms_SkuOptions();
- $url = new Icefusion_View_Helper_General_FullUrl();
- $this->view->cadastrar = $url->FullUrl('admin/skuoptions/add');
- $this->editData = new Zend_Session_Namespace('adm');
- }
- public function indexAction() {
- $this->redirect('admin/skuoptions/list');
- }
- public function addAction() {
- $this->view->form = $this->form->getFormCadastro($this->editData->data);
- unset($this->editData->data);
- $this->view->message = $this->messages->adminMessageSession->message;
- unset($this->messages->adminMessageSession->message);
- unset($this->session->adminSession->session);
- }
- public function listAction() {
- if ($this->_request->isPost()) {
- $data = $this->_request->getPost();
- $actionUrl = explode('/', $data['gridAction_id']);
- $action = end($actionUrl);
- switch ($action) {
- case 'edit':
- $this->edit($data, $data['gridAction_id']);
- break;
- case 'delete':
- $this->delete($data, $data['gridAction_id']);
- break;
- }
- } else {
- $report = new Icefusion_View_Helper_Admin_Reports_SkuOptions();
- $this->view->report = $report->getReportTable();
- }
- }
- public function saveAction() {
- $form = new Icefusion_View_Helper_Admin_Forms_SkuOptions();
- if ($this->_request->isPost()) {
- $data = $this->_request->getPost();
- $invalido = $form->isValidated($data, $this->obrigatorios);
- if (!isset($invalido) || count($invalido) == 0) {
- unset($data['submit']);
- unset($data['MAX_FILE_SIZE']);
- $files = $_FILES;
- $sku = new Application_Model_DbTable_SkuOptions();
- if ($data['id'] == '') {
- unset($data['id']);
- $data['img'] = null;
- try {
- $lastId = $sku->insert($data);
- unset($this->messages->adminMessageSession->message);
- $this->view->message = "";
- $this->messages->successSave();
- $this->session->adminSession->lastId = $lastId;
- $this->upJpg($files['arquivojpg'], $lastId);
- ///$this->upSwf($files['arquivoswf'], $order, $swfref);
- $this->redirect("admin/skuoptions/add");
- } catch (Exception $e) {
- var_dump($e);
- exit;
- }
- } else {
- $where = 'id = ' . $data['id'];
- $lastId = $sku->update($data, $where);
- unset($this->messages->adminMessageSession->message);
- $this->view->message = "";
- $this->messages->successEdit();
- $this->session->adminSession->lastId = $lastId;
- $this->redirect("admin/skuoptions/add");
- }
- } else {
- unset($this->messages->adminMessageSession->message);
- $this->view->message = "";
- $this->messages->errorSave($invalido, $this->nomeFront);
- $this->redirect("admin/skuoptions/add");
- }
- }
- }
- private function edit($postMassIds, $retorno) {
- $ids = explode(',', $postMassIds['postMassIdsid']);
- if (count($ids) == 1) {
- $model = new Application_Model_SkuOptions();
- $data = $model->getDataById($ids[0]);
- $this->editData->data = $data;
- $this->_redirect('admin/skuoptions/add');
- } else {
- $this->_redirect($retorno);
- }
- }
- private function delete($data, $retorno) {
- $ids = explode(',', $data['postMassIdsid']);
- $categoria = new Application_Model_DbTable_SkuOptions();
- foreach ($ids as $id) {
- try {
- $where = 'id = ' . $id;
- $categoria->delete($where);
- } catch (Exception $e) {
- print_r($e);
- die;
- }
- }
- $this->_redirect($retorno);
- }
- private function upJpg($data, $order) {
- $jpg = '../../img/products/' . $order;
- $upload = new Icefusion_View_Helper_General_UploadImagens();
- $upload->upload('jpg', $data, $order, '', $jpg);
- }
- private function upSwf($data, $order, $swfref) {
- $swf = '../../img/products/' . $swfref;
- $upload = new Icefusion_View_Helper_General_UploadImagens();
- $upload->upload('swf', $data, $order, '', $swf);
- }
- }
- FORM
- <?php
- class Icefusion_View_Helper_Admin_Forms_SkuOptions extends Zend_Form{
- public function getFormCadastro($data) {
- $form = new Zend_Form;
- $form->setMethod('post');
- $form->setAction('/admin/skuoptions/save');
- $form->setAttrib('enctype', 'multipart/form-data');
- $form->setAttrib('id', 'form-application');
- $sku = new Zend_Form_Element_Select('sku_id', array('label' => 'Sku:', 'class' => 'select2 borda-arredondada'));
- $sku->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row2'))));
- $skuList = new Application_Model_Sku();
- $result = $skuList->fetchAll('id > 0','name')->toArray();
- $options = array();
- foreach ($result as $value) {
- $options[$value['id']] = $value['name'];
- }
- $sku->setMultiOptions($options);
- $sku->setRequired(true);
- $form->addElement($sku);
- $nome = $form->createElement('text', 'name', array('label' => 'Nome:', 'class' => 'campos4 borda-arredondada'))->setRequired(true);
- $nome->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row4'))));
- $form->addElement($nome);
- $color = $form->createElement('text', 'color', array('label' => 'Cor:', 'class' => 'campos1 borda-arredondada'))->setRequired(true);
- $color->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row1'))));
- $form->addElement($color);
- $arquivojpg = new Zend_Form_Element_File('arquivojpg');
- $arquivojpg->setLabel('Enviar Perspectiva JPG:');
- $arquivojpg->addValidator('Count', false, 1);
- $arquivojpg->addValidator('Extension', false, 'jpg,png,gif');
- $arquivojpg->addDecorators(array('File', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row3'))));
- $form->addElement($arquivojpg);
- $arquivoswf = new Zend_Form_Element_File('arquivoswf');
- $arquivoswf->setLabel('Enviar Perspectiva SWF:');
- $arquivoswf->addValidator('Count', false, 1);
- //$arquivoswf->addValidator('Extension', false, 'swf');
- $arquivoswf->addDecorators(array('File', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row3'))));
- $form->addElement($arquivoswf);
- $id = $form->createElement('hidden', 'id');
- $form->addElement($id);
- $submit = $form->createElement('submit', 'submit', array('label' => 'Salvar', 'class' => 'btn btn-salvar'));
- $submit->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row2'))));
- $form->addElement($submit);
- $limpar = $form->createElement('button', 'button', array('label' => 'Limpar', 'class' => 'btn btn-limpar'));
- $limpar->addDecorators(array('ViewHelper', array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row2'))));
- $form->addElement($limpar);
- if ($data) {
- $form->populate($data);
- }
- return $form;
- //$form->addElement('hash', 'csrf');
- }
- public function isValidated($data, $obrigatorios) {
- $validacoes = new Icefusion_View_Helper_General_Validacoes();
- return $validacoes->validarRequeridos($data, $obrigatorios);
- }
- }
- UPLOAD
- class Icefusion_View_Helper_General_UploadImagens extends Zend_View_Helper_Abstract {
- public function upload($type, $data, $order, $swfref, $path) {
- switch ($type) {
- case 'jpg':
- $this->jpgUpload($data, $order, $path);
- break;
- case 'swf':
- $this->swfUpload($data, $order, $swfref, $path);
- break;
- default :
- break;
- }
- }
- private function jpgUpload($data, $order, $path) {
- $imageAdapterJPG = new Zend_File_Transfer_Adapter_Http();
- try {
- if (file_exists($path)) {
- $diretorio = dir($path);
- while ($arquivo = $diretorio->read()) {
- unlink($path . '/' . $arquivo);
- }
- $diretorio->close();
- $imageAdapterJPG->setDestination($path);
- if (is_uploaded_file($data['tmp_name'])) {
- if (!$imageAdapterJPG->receive()) {
- die('Erro - Arquivo Não Recebido');
- }
- } else {
- die('Not Upload');
- }
- } else {
- if (mkdir($path, 0777)) {
- $imageAdapterJPG->setDestination($path);
- if (is_uploaded_file($data['tmp_name'])) {
- if (!$imageAdapterJPG->receive()) {
- die('Erro - Arquivo Não Recebido');
- }
- } else {
- die('Not Upload');
- }
- }
- }
- } catch (Exception $e) {
- var_dump($e);
- exit;
- }
- }
- private function swfUpload($data, $order, $swfref, $path) {
- $imageAdapterSWF = new Zend_File_Transfer_Adapter_Http();
- try {
- if (file_exists($path)) {
- $diretorio = dir($path);
- while ($arquivo = $diretorio->read()) {
- unlink($path . '/' . $arquivo);
- }
- $diretorio->close();
- $imageAdapterSWF->setDestination($path);
- if (is_uploaded_file($data['tmp_name'])) {
- if (!$imageAdapterSWF->receive()) {
- die('Erro - Arquivo Não Recebido');
- }
- } else {
- die('Not Upload');
- }
- } else {
- if (mkdir($path, 0777)) {
- $imageAdapterSWF->setDestination($path);
- if (is_uploaded_file($data['tmp_name'])) {
- if (!$imageAdapterSWF->receive()) {
- die('Erro - Arquivo Não Recebido');
- }
- } else {
- die('Not Upload');
- }
- }
- }
- } catch (Exception $e) {
- var_dump($e);
- exit;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment