/** * Receive the file from the client (Upload) * * @param string|array $files (Optional) Files to receive * @return bool */ public function receive($files = null, $storedId = null) { if (!$this->isValid($files)) { return false; } $_processedFiles = array(); $check = $this->_getFiles($files); foreach ($check as $file => $content) { if (!$content['received']) { $directory = ''; $destination = $this->getDestination($file); if ($destination !== null) { $directory = $destination . DIRECTORY_SEPARATOR; } $filename = $directory . $content['name']; $rename = $this->getFilter('Rename'); $storage = new Z_File_Storage(); if ($rename !== null) { $tmp = $rename->getNewName($content['tmp_name']); if ($tmp != $content['tmp_name']) { $filename = $tmp; } if (dirname($filename) == '.') { $filename = $directory . $filename; } $key = array_search(get_class($rename), $this->_files[$file]['filters']); unset($this->_files[$file]['filters'][$key]); } elseif ($storedId == null) { $tmp = $storage->getNewFolderName(); if (!$tmp) { throw new Exception("Не удалось выбрать имя для папки.<br>Попробуйте использовать более длинное имя или увеличьте количество попыток для подбора"); } $filename = $tmp; } // Should never return false when it's tested by the upload validator $lid = $storedId == null ? $storage->create($content['tmp_name'], array('filename' => $filename, 'realname' => $content['name'])) : $storage->replace($storedId, $content['tmp_name'], $content['name']); if (!$lid) { if ($content['options']['ignoreNoFile']) { $this->_files[$file]['received'] = true; $this->_files[$file]['filtered'] = true; continue; } $this->_files[$file]['received'] = false; return false; } $this->_processedFiles[$file] = $storedId == null ? $lid : $storedId; if ($rename !== null) { $this->_files[$file]['destination'] = dirname($filename); $this->_files[$file]['name'] = basename($filename); } $this->_files[$file]['tmp_name'] = $filename; $this->_files[$file]['received'] = true; } if (!$content['filtered']) { if (!$this->_filter($file)) { $this->_files[$file]['filtered'] = false; return false; } $this->_files[$file]['filtered'] = true; } } return true; }
public function multiaddAction() { $this->setViewPathes(); $data = $this->_request->getPost(); $images = null; if (isset($data['flag_'])) { $images = $data['flag_']; if (!isset($data['msubmit'])) { $data = null; } } elseif (isset($data['IENoeQerinqQRHNQhopq35ui56'])) { $images = explode('*', $data['IENoeQerinqQRHNQhopq35ui56']); } else { $this->disableRenderView(); $this->ajaxGo($this->view->url(array('action' => $this->z_defaultAction))); } $form = $this->getFormInstances($images); if (isset($data['msubmit'])) { $modelForms = new Z_Model_Resourceforms(); $elements = $modelForms->fetchAll(array('resourceid=?' => $this->z_resourceInfo->id)); if (count($elements) && count($images)) { $k = 0; foreach ($images as $image) { $k++; foreach ($elements as $el) { $d = ''; if ($el->type == 'MultiImage') { $d = 'Multi$' . $image; } $data[$el->field . '_' . $k] = $d; } } } } $this->view->da = $data; // return; // Z_FlashMessenger::addMessage('wrong2'); //$data = $this->addOverridePrevalidate($data); if (!$this->validateAction($data)) { Z_FlashMessenger::addMessage("Невозможно добавить новый элемент!"); $this->disableRenderView(); $this->ajaxGo($this->view->url(array("action" => $this->z_defaultAction))); return; } // $data= array(16) { // ["IENoeQerinqQRHNQhopq35ui56"]=> // string(75) "/upload/tmpajdoqODU1/1_italiya_copy.jpg@/upload/tmpajdoqODU1/3_yaponiya.jpg" // ["title_1"]=> // string(4) "fdsf" // ["price_1"]=> // string(5) "fdsad" // ["kaka_Multi1"]=> // string(39) "/upload/tmpajdoqODU1/1_italiya_copy.jpg" // ["date_1"]=> // string(10) "2011-12-24" // ["text_1"]=> // string(5) "value" // ["mce_1"]=> // string(0) "" // ["MAX_FILE_SIZE"]=> // string(8) "33554432" // ["title_2"]=> // string(5) "sfadf" // ["price_2"]=> // string(6) "fdsafa" // ["kaka_Multi2"]=> // string(35) "/upload/tmpajdoqODU1/3_yaponiya.jpg" // ["date_2"]=> // string(10) "2011-12-24" // ["text_2"]=> // string(5) "value" // ["mce_2"]=> // string(0) "" // ["submit"]=> // string(18) "Сохранить" // ["z-ajax-form"]=> // string(1) "1" //} // $dataForm= array(25) { // ["IENoeQerinqQRHNQhopq35ui56"]=> // string(75) "/upload/tmpajdoqODU1/1_italiya_copy.jpg@/upload/tmpajdoqODU1/3_yaponiya.jpg" // ["splitter_1"]=> // string(28) " //Элемент №1 //" // ["imagecontainer_1"]=> // string(131) "" // ["title_1"]=> // string(6) "jfdskl" // ["price_1"]=> // string(5) "fjals" // ["kaka_1"]=> // NULL // ["date_1"]=> // string(10) "2011-12-24" // ["text_1"]=> // string(5) "value" // ["mce_1"]=> // string(0) "" // ["file_1"]=> // int(0) // ["list_1"]=> // NULL // ["radio_1"]=> // NULL // ["flag_1"]=> // NULL // ["splitter_2"]=> // string(28) " //Элемент №2 //" // ["imagecontainer_2"]=> // string(127) "" // ["title_2"]=> // string(4) "woei" // ["price_2"]=> // string(3) "320" // ["kaka_2"]=> // NULL // ["date_2"]=> // string(10) "2011-12-24" // ["text_2"]=> // string(5) "value" // ["mce_2"]=> // string(0) "" // ["file_2"]=> // int(0) // ["list_2"]=> // NULL // ["radio_2"]=> // NULL // ["flag_2"]=> // NULL //} // ech(count($data).count(i)) if (!empty($data) && count($images) && $form->isValid($data)) { $storage = new Z_File_Storage(); $multiDataForm = $form->getValues($data); $dataForms = null; $unrecognized = null; // $this->view->da = $multiDataForm; //return; foreach ($multiDataForm as $key => $val) { if (strpos($val, 'Multi$') !== false) { $path = substr($val, 6); if ($path[0] != '/') { $path = '/' . $path; } if ($sp = strpos($path, '@')) { $filepath = substr($path, 0, $sp); $filename = substr($path, $sp + 1); } $ite = explode('/', $filepath); $filepath = SITE_PATH . implode(DIRECTORY_SEPARATOR, $ite); if (file_exists($filepath)) { $val = $storage->create($filepath, array('realname' => $filename)); } } if (strpos($key, '_') !== false) { $str = explode("_", $key); $sim = array_pop($str); $kl = implode('_', $str); $dataForms[$sim][$kl] = $val; } else { $unrecognized[$key] = $val; } } if (count($unrecognized)) { if (count($dataForms)) { foreach ($dataForms as $dfk => $df) { foreach ($unrecognized as $kv => $unr) { $df[$kv] = $unr; } $dataForms[$dfk] = $df; } } else { $dataForms[] = $unrecognized; } } if (count($dataForms)) { foreach ($dataForms as $dataForm) { //получение данных из формы if ($this->z_datatype == 'catalog' && !isset($dataForm['parentid'])) { $dataForm['parentid'] = $this->_getParam('parentid', 0); } //если есть дополнительные поля, то добавляем их в массив для insert if (!empty($this->z_addfields)) { $dataForm = array_merge($this->z_addfields, $dataForm); } if (!$this->addCheck($dataForm)) { $this->disableRenderView(); Z_FlashMessenger::addMessage('Нельзя добавить новый элемент!'); return; } //оверрайд $dataForm = $this->addOverride($dataForm); //добавление элемента в таблицу $item = $this->z_model->createRow($dataForm); $item->save(); $id = $item->id; //саксэс $this->addSuccess(array_merge($item->toArray(), $dataForm)); $this->addToIndex($item->toArray()); //добавление связей многие ко многим if (!empty($this->z_refers)) { foreach ($this->z_refers as $referName => $refer) { $referData = $dataForm[$referName] ? $dataForm[$referName] : array(); $referModel = new $refer['model'](); foreach ($referData as $referDataEl) { $referModel->createRow(array($refer['field1'] => $id, $refer['field2'] => $referDataEl))->save(); } } } //если список сортируемый, то устанавливаем orderid для элемента if ($this->z_sortable) { $select = $this->z_model->select(true); $select->reset('columns')->columns(($this->z_sortable_position == 'top' ? 'MIN' : 'MAX') . '(orderid)')->where('id!=?', $id); $orderid = $select->query()->fetchColumn(); if (!is_numeric($orderid)) { $orderid = $id; } else { $this->z_sortable_position == 'top' ? $orderid-- : $orderid++; } $item->orderid = $orderid; $item->save(); } } } $this->disableRenderView(); $this->ajaxGo($this->view->url(array('action' => $this->z_defaultAction))); } $this->view->form = $form; }