Example #1
0
 public function uploadAction()
 {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(true);
     $json = new ZendT_Json_Result();
     try {
         $options = $this->getRequest()->getParam('options');
         if ($options && !is_array($options)) {
             $options = unserialize($options);
         }
         /**
          * @var Zend_File_Transfer_Adapter_Http 
          */
         $uploads = new Zend_File_Transfer('Http', false, array('detectInfos' => false));
         if ($options['extension'] == ZendT_Type_Blob::FILTER_EXECUTABLE) {
             $options['extension'] = array('text/php', 'text/x-php', 'text/asp', 'text/x-asp');
         }
         if ($options['maxSize'] || $options['minSize']) {
             $uploads->addValidator('FilesSize', false, array('max' => $options['maxSize'], 'min' => $options['minSize']));
         }
         if (is_array($options['validators'])) {
             array_merge($options['extension'], $options['validators']);
         }
         //$uploads->addValidator('ExcludeMimeType', false, $options['extension']);
         $uploads->receive();
         if ($uploads->hasErrors()) {
             $message = $uploads->getMessages();
             throw new ZendT_Exception_Information(current($message));
         } else {
         }
         $infoFiles = $uploads->getFileInfo();
         @($content = file_get_contents($infoFiles['file']['tmp_name']));
         if ($content === false) {
             throw new ZendT_Exception_Error('Não foi possível armazenar o arquivo informado!');
         }
         $_file = new ZendT_File($infoFiles['file']['name'], $content, $infoFiles['file']['type']);
         @unlink($infoFiles['file']['tmp_name']);
         $infoFile = $_file->toArrayJson();
         $infoFile['size'] = $infoFiles['file']['size'];
         $json->setResult($infoFile);
     } catch (Exception $Ex) {
         $json->setException($Ex);
     }
     echo $json->render();
 }
 public function saveAction()
 {
     $request = $this->getRequest();
     $id = $this->getParam('id');
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $user_id = $auth->getIdentity()->id;
     }
     $form = $this->getSaveProductForm($id);
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($request->getPost())) {
             $data = $this->getRequest()->getParams();
             $upload = new Zend_File_Transfer();
             $files = $upload->getFileInfo();
             $isValid = true;
             foreach ($files as $field => $file) {
                 if (!strlen($file["name"])) {
                     continue;
                 }
                 $extension = pathinfo($file['name'], PATHINFO_EXTENSION);
                 $filename = pathinfo($file['name'], PATHINFO_FILENAME);
                 if (!file_exists(UPLOADS_IMAGES)) {
                     mkdir(UPLOADS_IMAGES, 0774, true);
                 }
                 if (!file_exists(UPLOADS_DATA)) {
                     mkdir(UPLOADS_DATA, 0774, true);
                 }
                 // upload instructions for image
                 if ($field == 'image') {
                     $upload->addFilter('Rename', array('target' => UPLOADS_IMAGES . '/' . $filename . "_" . $user_id . "_" . time() . "." . $extension, 'overwrite' => TRUE), $field)->addValidator('Extension', false, array('jpg', 'jpeg', 'png'), $field);
                     $data['image'] = $filename . "_" . $user_id . "_" . time() . "." . $extension;
                 }
                 // upload instructions for file
                 if ($field == 'file') {
                     $upload->addFilter('Rename', array('target' => UPLOADS_DATA . '/' . $filename . "_" . $user_id . "_" . time() . "." . $extension, 'overwrite' => TRUE), $field)->addValidator('Extension', false, array('doc', 'docx', 'txt', 'pdf'), $field);
                     $data['file'] = $filename . "_" . $user_id . "_" . time() . "." . $extension;
                 }
                 if ($upload->isValid($field)) {
                     if (!$upload->receive($field)) {
                         $isValid = false;
                         foreach ($upload->getMessages() as $key => $val) {
                             $this->_helper->getHelper('FlashMessenger')->addMessage($val, 'error');
                         }
                     }
                 } else {
                     $isValid = false;
                     $this->_helper->getHelper('FlashMessenger')->addMessage($file['name'] . " is not valid {$field}", 'error');
                     //return $this->_helper->redirector('save');
                 }
             }
             if ($upload->hasErrors()) {
                 $errors = $upload->getMessages();
                 foreach ($errors as $error) {
                     $this->_helper->getHelper('FlashMessenger')->addMessage("{$error}", 'error');
                 }
                 return $this->_helper->redirector('save');
             }
             if ($isValid) {
                 $product = new Application_Model_Product();
                 $productMapper = new Application_Model_ProductMapper();
                 if ($id) {
                     $product = $productMapper->getProductById($id);
                 }
                 if (isset($data['file']) && $product->file && $product->file != $data['file'] || !isset($data['file']) && $product->file) {
                     $productMapper->delete_file($product->file);
                 }
                 if (isset($data['image']) && $product->image && $product->image != $data['image'] || !isset($data['image']) && $product->image) {
                     $productMapper->delete_image($product->image);
                 }
                 $product = new Application_Model_Product($data);
                 $productMapper->save($product);
                 return $this->_helper->redirector('dashboard', 'users');
             }
         }
     }
     $this->view->headScript()->appendFile(JS_DIR . '/' . self::DELETE_FIELD . '.js');
     $this->view->form = $form;
 }