Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class UTIUpload
- {
- /**
- * Объект с кем связывается приклеплеяемый файл
- * @var
- */
- private $_model;
- /**
- * Сценарий валидации, правила хранятся в конфигурации
- * @var
- */
- private $_scenario;
- /**
- * Конфигурация для работы с изображениями
- * @var
- */
- private $_imageConfig;
- /**
- * Массив хранящий объекты модели Attachments
- * @var array
- */
- private $_attachments = array();
- /**
- * Флаг о прохождения всех файлов валидации
- * @var bool
- */
- private $_valid = TRUE;
- /**
- * Флаг для загрузки двух и более файлов
- * @var bool
- */
- private $multiple = FALSE;
- /**
- * Количество файлов для загрузков по умолчанию
- * @var int
- */
- private $defaultInputsCount = 1;
- public function __construct(CActiveRecord $model, $fieldName = 'file', $scenario = 'default', $imageConfig = NULL, $options = NULL)
- {
- $this->_model = $model;
- $this->_scenario = $scenario;
- $this->_imageConfig = $imageConfig;
- if ($options !== NULL && is_array($options))
- foreach ($options as $key => $value)
- if (isset($this->$key))
- $this->$key = $value;
- $iterations = ($this->multiple === TRUE && $this->defaultInputsCount > 0) ? $this->defaultInputsCount : 1;
- for ($i = 0; $i < $iterations; $i++)
- $this->_attachments[$i] = new Attachments($this->_scenario);
- }
- public function validateFiles()
- {
- $files = Yii::app()->request->getPost('Attachments');
- foreach ($files as $i => $file)
- {
- // На случай динамического добавления инпутов для загрузки файлов
- if (!isset($this->_attachments[$i]) || !$this->_attachments[$i] instanceof Attachments)
- $this->_attachments[$i] = new Attachments($this->_scenario);
- $this->_attachments[$i]->file = CUploadedFile::getInstance($this->_attachments[$i], '['.$i.']file');
- $this->_valid = $this->_attachments[$i]->validate() && $this->_valid === TRUE;
- }
- return $this->_valid;
- }
- public function saveFiles()
- {
- if ($this->_valid !== TRUE)
- return FALSE;
- // Загрузка
- }
- /**
- * Возвращает массив вложений или одно вложение ЕСЛИ multiple FALSE
- * @return array
- */
- public function getAttachments()
- {
- return ($this->multiple === TRUE) ? $this->_attachments : current($this->_attachments);
- }
- /**
- * Прочие методы
- */
- }
Add Comment
Please, Sign In to add comment