_adapter) { $this->setTransferAdapter(new Qs_File_Transfer_Adapter_Db()); } return $this->_adapter; } public function getValue() { if (!is_null($this->_value)) { return $this->_value; } if ($value = $this->getOldValue()) { $this->setValue($value); return $this->_value; } if (!$this->isValid(null)) { return null; } if (!$this->receive()) { return null; } $content = $this->getTransferAdapter()->getFileName($this->getName()); if (empty($content)) { return null; } $filenames = $this->getFileName(); if (count($filenames) == 1) { $this->_value = basename($filenames); return $this->_value; } $this->_value = []; foreach ($filenames as $filename) { $this->_value[] = basename($filename); } return $this->_value; } public function setValue($value) { if (is_array($value)) { if (array_key_exists('oldValue', $value)) { $this->setOldValue($value['oldValue']); } return $this; } $this->_value = $value; return $this; } /** * @param string $value * @return string */ public function getDownloadUrl($value = null) { if (null !== $this->_downloadUrl) { return $this->_downloadUrl; } if (null === $value) { $value = $this->getValue(); } $fullFileName = $this->getTransferAdapter()->getDestination($this->getName()) . '/' . rawurlencode($value); $this->_downloadUrl = str_replace(WWW_PATH, BASE_URL, $fullFileName); return $this->_downloadUrl; } public function addErrorMessage($message) { $this->_errorMessages[] = (string) $message; return $this; } protected function _saveSession($value) { $session = new Qs_Session_Namespace(CURRENT_PAGE); if (!isset($session->files)) { $session->files = []; } $session->files[$value] = $this->getName(); return $this; } public function isValid($value, $context = null) { $isValid = parent::isValid($value, $context); if (is_array($context)) { $name = $this->getName(); $del = isset($context[$name]['del']); $isOldFile = (isset($context[$name]['oldFile']) && !empty($context[$name]['oldFile'])); $isNewFile = $this->getTransferAdapter()->isUploaded($this->getName()); if ($this->isRequired()) { if ($isOldFile) { if ($isNewFile && $isValid) { $this->_saveSession($context[$name]['oldFile']); } else { $this->setOldValue($context[$name]['oldFile']); if ($del) { // message $this->getTransferAdapter()->addError('File is required'); return false; } } } else { if (!$isNewFile) { // message $this->getTransferAdapter()->addError('File is required'); return false; } } } else { if ($del && $isOldFile && $isValid || $isOldFile && $isNewFile && $isValid) { $this->_saveSession($context[$name]['oldFile']); } else { $this->setOldValue(isset($context[$name]['oldFile']) ? $context[$name]['oldFile'] : null); } } if ($isNewFile) { //$isValid = parent::isValid($value, $context); if (!$isValid && isset($context[$name]['oldFile'])) { $this->setOldValue($context[$name]['oldFile']); } return $isValid; } /** ігноруємо всі помилки навіть якщо вони були */ $this->getTransferAdapter()->setMessages([]); return true; } return $isValid; } public function getOldValue() { return $this->_oldValue; } public function setOldValue($value) { // Set old value only if it not already set. if (null === $this->_oldValue && !empty($value)) { $this->_oldValue = $value; } return $this; } public function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return $this; } $decorators = $this->getDecorators(); if (empty($decorators)) { $this->addDecorator('ExtendedFile') ->addDecorator('Errors') ->addDecorator('Description') ->addDecorator('HtmlTag', ['tag' => 'dd', 'id' => $this->getName() . '-element']) ->addDecorator('Label', ['tag' => 'dt']); } return $this; } }