コード例 #1
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate()
 {
     if ($this->catalog) {
         $catalog = ucwords($this->catalog);
         $id = (int) Yii::app()->request->getParam("id", 0);
         if (!empty($id)) {
             $model = $catalog::fetch($id);
         } else {
             $model = new $catalog();
         }
         $message = "";
         // Сохрание полей
         if (isset($_POST[$catalog])) {
             $model->setAttributesFromArray($_POST[$catalog]);
             if ($model->description) {
                 $model->description = str_replace("../../../../", Yii::app()->params["baseUrl"], $model->description);
             }
             if (!empty($_FILES[$catalog])) {
                 foreach ($_FILES[$catalog]["name"] as $key => $field) {
                     if (property_exists($model, $key)) {
                         $model->{$key} = $field;
                     }
                 }
             }
             if ($model->saveWithRelation()) {
                 $message = "Данные успешно сохраненны";
                 //$this->redirect(array('view','id'=>$model->id));
             }
         }
         // Сохранение TITLE галереи
         if (!empty($_POST["image_submit"]) && !empty($_POST["image"])) {
             foreach ($_POST["image"] as $value) {
                 if ($value["id"] > 0) {
                     $imageModel = CatGallery::fetch($value["id"]);
                     if ($imageModel->id > 0) {
                         $imageModel->name = $value["name"];
                         $imageModel->pos = $value["pos"];
                         $imageModel->type = $value["type"];
                         $imageModel->save();
                     }
                 }
             }
             $message = "Галерея успешно сохраненна";
         }
         // Добвление картинки
         $addGallery = new CatGallery();
         if (!empty($_POST["submit_add_gallery"]) && !empty($id)) {
             $addGallery->setAttributesFromArray($_POST["CatGallery"]);
             $addGallery->image = $_FILES["CatGallery"]["name"]["image"];
             $addGallery->catalog = $model->tableName();
             $addGallery->item_id = $id;
             $addGallery->save();
             // "", false
             $addGallery = new CatGallery();
         }
         if ($model->id > 0) {
             $listImage = CatGallery::findByAttributes(array("catalog" => $model->tableName(), "item_id" => $model->id));
         } else {
             $listImage = array();
         }
         $this->render('edit', array('arrayParams' => $this->arrayParams, 'form' => $model, 'catalog' => $this->catalog, 'listImage' => $listImage, 'message' => $message, 'addGallery' => $addGallery));
     }
 }
コード例 #2
0
 function uploadImages($id)
 {
     $error = "";
     if ($id > 0) {
         ///////
         // Проверить колиство файлов - не более 6,8
         // Проверить нет ли общибок в $_FILES[][error]
         // Проверить размер фотографий не более 5,6 мв на каждую
         // проверитьтиф файлов пропускать только gif|jpg|png|jpeg
         $modelName = $this->addModel;
         $item = $modelName::fetch($id);
         if ($item->id > 0) {
             // Для сохранения груп фотографий, будет задействованна фунция CCModel::save
             // а там уже будет сохранятся картинка, но там используется формат данных $_FILES как при одном файле а масиве
             // поэтому мы сохраним масив $_FILES затем его очистим и будет подставлять необходиммые для сохранения картинки значения
             if (!empty($_FILES["CatGallery"])) {
                 $postImages = $_FILES["CatGallery"];
                 // Очищаем масив чтобы подставлять данные в нужном формате
                 unset($_FILES["CatGallery"]);
             }
             // Проверем на наличие ошибок
             $haveError = false;
             if (empty($postImages)) {
                 $haveError = true;
                 $error = "Произошла ошибка скачивания";
             }
             if (!$error) {
                 for ($i = 0; $i < sizeof($postImages["name"]["images"]); $i++) {
                     if ($i > 8) {
                         $error = "Максимальное количество 8 файлов";
                         break;
                     }
                     $error = ImageHelper::checkError($postImages["type"]["images"][$i], $postImages["size"]["images"][$i], $postImages["error"]["images"][$i], array("jpg", "jpeg"), 5242880);
                     if (empty($error)) {
                         $_FILES["CatGallery"] = array("name" => array("image" => $postImages["name"]["images"][$i]), "type" => array("image" => $postImages["type"]["images"][$i]), "tmp_name" => array("image" => $postImages["tmp_name"]["images"][$i]), "error" => array("image" => $postImages["error"]["images"][$i]), "size" => array("image" => $postImages["size"]["images"][$i]));
                         $addGallery = new CatGallery();
                         $addGallery->image = $postImages["name"]["images"][$i];
                         $addGallery->catalog = $this->tableName;
                         $addGallery->item_id = $id;
                         $addGallery->save();
                         if ($addGallery->getErrors() && sizeof($addGallery->getErrors()) > 0) {
                             print_r($addGallery->getErrors());
                         }
                     }
                 }
                 if (!$error) {
                     $this->redirect(SiteHelper::createUrl("/user/" . Yii::app()->controller->getId() . "/description/", array("id" => $id)));
                 } else {
                     $this->redirect(SiteHelper::createUrl("/user/" . Yii::app()->controller->getId() . "/description/", array("id" => $id, "error" => "gallError")));
                 }
             }
         } else {
             throw new Exception("Ошибка групповой закачи картиноку ( Указанному ID нет соответствующей записи )");
         }
     } else {
         throw new Exception("Ошибка групповой закачи картиноку ( Не указан ID )");
     }
 }