public function actionIndex() { if (isset($_POST['ajax'])) { $slideShows = Slideshow::model()->findAll(); $images = array(); if ($slideShows) { foreach ($slideShows as $slideShow) { $images[] = $slideShow->imageLink; } echo json_encode($images); } } else { $newProducts = NewProducts::model()->findAll(); $hotProducts = HotProducts::model()->findAll(array("limit" => 17)); $amthucs = Amthuc::model()->findAll(array("order" => 'id DESC', "limit" => 4)); if ($newProducts || $hotProducts) { $newProduct = array(); foreach ($newProducts as $newProduct1) { $product = Products::model()->findByPk($newProduct1->product_id); if ($product) { $newProduct[$newProduct1->id] = $product; } } foreach ($hotProducts as $key => $value) { $product = Products::model()->find('id=:product_id', array(':product_id' => $value->product_id)); if ($product) { $products[] = $product; } } if ($newProduct && $products || $amthucs) { $this->render('index', array('newProduct' => $newProduct, 'products' => $products, 'amthucs' => $amthucs)); } else { $this->render('index'); } } } }
/** * 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($id) { $model = $this->loadModel($id); $categorys = Categorys::model()->findAll(array('order' => 'name')); if (isset($_POST['Products'])) { $_POST['Products']['imageLink'] = $model->imageLink; $model->attributes = $_POST['Products']; $model->update_time = date("Y-m-d H:i:s"); if (isset($_POST['category'])) { $model->category = $_POST['category']; } $uploadedFile = CUploadedFile::getInstance($model, 'imageLink'); if ($uploadedFile) { $fileExtensionName = $uploadedFile->extensionName; $md5FileName = md5($uploadedFile); $fileName = "{$md5FileName}.{$fileExtensionName}"; // random number + file name $model->imageLink = $fileName; } if ($model->save()) { //if click checkbox it will insert product_id to newProduct vs hotProduct table in database if (isset($_POST['newProduct_check'])) { $product = Products::model()->find('name=:name', array(':name' => $model->name)); $id = $product->id; $tableName = 'newproducts'; $newProduct = NewProducts::model()->find('product_id=:product_id', array(':product_id' => $id)); if (!$newProduct) { $this->insertData($tableName, $id); } } if (isset($_POST['hotProduct_check'])) { $product = Products::model()->find('name=:name', array(':name' => $model->name)); $id = $product->id; $tableName = 'hotproducts'; $hotProduct = HotProducts::model()->find('product_id=:product_id', array(':product_id' => $id)); if (!$hotProduct) { $this->insertData($tableName, $id); } } if (!empty($uploadedFile)) { $uploadedFile->saveAs(Yii::app()->basePath . '/../images/Product/' . $model->imageLink); } $this->redirect(array('admin')); } } $this->render('update', array('model' => $model, 'categorys' => $categorys)); }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return HotProducts the loaded model * @throws CHttpException */ public function loadModel($id) { $model = HotProducts::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }