Пример #1
0
 public function getUrl()
 {
     if (!$this->isNewRecord) {
         $name = HText::translit(mb_strtolower($this->name), "-");
         return Yii::app()->createUrl(ShopModule::ROUTE_PRODUCT_BRAND, array('translitName' => $name, 'idBrand' => $this->id_brand));
     }
     return '/';
 }
Пример #2
0
 public function getUrl()
 {
     if (!$this->isNewRecord) {
         $name = HText::translit(mb_strtolower($this->name), "-");
         return Yii::app()->createUrl(ShopModule::ROUTE_PRODUCT_CATEGORY, array('translitName' => $name, 'idCategory' => $this->id_product_category));
     }
     return '/';
 }
Пример #3
0
 public function onPostForm(PostFormEvent $event)
 {
     if (!YGIN_DEVELOP && $this->model->isNewRecord) {
         // для не разработчиков системы генерируем имя строковых ключей автоматически
         $model = $event->model;
         $id = rtrim(Yii::app()->backend->prefixPkName, '-_') . '-';
         if ($this->parentSourceAttr != null) {
             $id = $model->{$this->parentSourceAttr} . '-';
         }
         $id .= $this->prefixAttr;
         foreach ($this->sourceAttr as $attr) {
             if ($model->{$attr} != null && trim($model->{$attr}) != '-') {
                 $id .= HText::translit(mb_strtolower($model->{$attr}), '-');
                 break;
             }
         }
         $id = str_replace('_', '-', $id);
         $this->model->{$this->attributeName} = $id;
     }
 }
Пример #4
0
 protected function handleUpload()
 {
     $this->beforeUpload();
     $formModel = $this->getFormModel();
     if (!$formModel instanceof FileUploadForm) {
         throw new ErrorException('Модель формы должна расширять класс FileUploadForm.');
     }
     $formModel->attributes = Yii::app()->request->getPost(get_class($formModel), array());
     $formModel->{$this->fileAttribute} = CUploadedFile::getInstance($formModel, $this->fileAttribute);
     $uploadedFile = $formModel->{$this->fileAttribute};
     if ($this->beforeUploadValidate()) {
         if ($formModel->validate()) {
             if ($this->fileName === null) {
                 if ($this->filenameRule != null) {
                     $this->fileName = $this->evaluateExpression($this->filenameRule, array('file' => $uploadedFile));
                 }
                 if ($this->fileName === null) {
                     $this->fileName = $uploadedFile->getName();
                 }
                 if ($this->getTranslitFileName()) {
                     $this->fileName = HText::translit($this->fileName, '-');
                 }
             }
             $oldFile = $this->getOldFile();
             if (!$this->checkAccess($oldFile)) {
                 throw new CHttpException(403, 'Вы не можете выполнить данную операцию.');
             }
             $newFile = null;
             //Если существует старый файл, то его надо либо заменить, либо создать файл с новым именем
             if ($oldFile !== null) {
                 if ($this->rewriteIfFileExist) {
                     if (file_exists($oldFile->getFilePath(true))) {
                         unlink($oldFile->getFilePath(true));
                     }
                     $oldFile->deleteChildFile();
                     $newFile = $oldFile;
                 } else {
                     $this->fileName = $this->getCopyFileName($oldFile->getFilePath(true));
                     $newFile = new File();
                 }
             } else {
                 $newFile = new File();
             }
             $destDir = '';
             if ($formModel->instanceId) {
                 $model = $formModel->getInstanceModel();
                 if ($model == null) {
                     throw new CHttpException(404, 'Экземпляр модели id_object = ' . $formModel->objectId . ', с id_instance = ' . $formModel->instanceId . ' не найден.');
                 }
                 $destDir = $model->getDir(false, true);
             } else {
                 $destDir = Yii::getPathOfAlias('temp') . '/' . $formModel->tmpId;
             }
             $destDir = $this->createDir($destDir);
             $normalazedPath = rtrim($destDir, '/') . '/';
             $path = $normalazedPath . $this->fileName;
             //возможно такой файл уже существует
             //тогда надо сгенерить новое имя
             if (file_exists($path)) {
                 $this->fileName = $this->getCopyFileName($path);
                 $path = $normalazedPath . $this->fileName;
             }
             if (!$uploadedFile->saveAs($path)) {
                 throw new CHttpException(500, 'Не удалось сохранить файл "' . $path . '".');
             } else {
                 chmod($path, 0777);
             }
             $newFile->create_date = time();
             $newFile->id_object = $formModel->objectId;
             $newFile->id_instance = $formModel->instanceId;
             $newFile->id_parameter = $formModel->parameterId;
             $newFile->id_tmp = $formModel->tmpId;
             $newFile->file_path = $this->getRelativePath($path);
             if (!$newFile->save()) {
                 throw new CHttpException(500, 'Не удалось сохранить модель File: ' . "\n" . $this->getModelErrors($newFile));
             }
             $this->savedFile = $newFile;
             //если модель существующая и тип поля - Файл, то сразу обновим поле
             if ($formModel->instanceId) {
                 $objectParameter = $formModel->getObjectParameter();
                 if ($objectParameter == null) {
                     throw new CHttpException(404, 'Параметр объекта id_object = ' . $formModel->objectId . ', id_parameter = ' . $formModel->parameterId . ' не найден.');
                 }
                 if ($objectParameter->id_parameter_type == DataType::FILE) {
                     $model = $formModel->getInstanceModel();
                     $model->{$objectParameter->field_name} = $newFile->id_file;
                     $model->save(false, array($objectParameter->field_name));
                 }
             }
             $this->afterUpload();
         } else {
             if (isset($formModel->errors['file'])) {
                 throw new DaHttpException(400, $this->getModelErrors($formModel));
                 // без логирования
             } else {
                 throw new CHttpException(400, $this->getModelErrors($formModel));
             }
         }
     }
 }
Пример #5
0
 protected function beforeSave()
 {
     $this->alias = HText::translit($this->alias, '_', false);
     return parent::beforeSave();
 }
Пример #6
0
 protected function beforeSave()
 {
     if ($this->isNewRecord) {
         $site = $this->link;
         $site = str_replace(array("http://", "www."), "", $site);
         $site = str_replace("/", "_", $site);
         $site = HText::translit($site) . "_" . rand(10, 1000);
         $site = str_replace("__", "_", $site);
         $this->unique_name = $site;
     }
     return parent::beforeSave();
 }