/** * * 插入或者更新内容时,同时更新标签数据 * @author Sim <*****@*****.**> * @param $tagdata tag数据 * @param $param [content_id]关联内容id [status]关联内容状态 [type_id](1-5) * **/ public static function updateTagData($tagdata = array(), $param = array()) { foreach ((array) $tagdata as $value) { if ($value) { $model_tag = new Tag(); $get_tags = $model_tag->find('tag_name=:tagname', array(':tagname' => $value)); if ($get_tags) { $tag_id = $get_tags->id; } else { $model_tag->data_count = 1; $model_tag->tag_name = $value; $model_tag->save(); $tag_id = $model_tag->id; } //添加关联表数据 $tagData = TagData::model()->findByPk(array('tag_id' => $tag_id, 'content_id' => $param['content_id'])); if (!$tagData) { $tagData = new TagData(); $tagData->tag_id = $tag_id; $tagData->content_id = $param['content_id']; $tagData->type = $param['type_id']; $tagData->status = $param['status']; $tagData->save(); } } } //更新关联的标签状态 $tagData = TagData::model()->updateAll(array('status' => $param['status']), 'content_id =:id AND type =:type', array(':id' => $param['content_id'], ':type' => $param['type_id'])); return true; }
/** * 更新 * * @param $id */ public function actionUpdate($id) { $model = Image::model()->findByPk($id); if (isset($_POST['Image'])) { $model->attributes = $_POST['Image']; //标题样式 $title_style = $this->_request->getPost('style'); if ($title_style['bold'] != 'Y') { unset($title_style['bold']); } if ($title_style['underline'] != 'Y') { unset($title_style['underline']); } if (!$title_style['color']) { unset($title_style['color']); } if ($title_style) { $model->title_style = serialize($title_style); } else { $model->title_style = ''; } if ($_FILES['attach']['error'] == UPLOAD_ERR_OK) { //新上传的封面图片 $upload = new Uploader(); $upload->_thumb_width = 300; $upload->_thumb_height = 300; $upload->uploadFile($_FILES['attach'], true); if ($upload->_error) { $upload->deleteFile($upload->_file_name); $upload->deleteFile($upload->_thumb_name); $this->message('error', Yii::t('admin', $upload->_error)); return; } $model->attach_file = $upload->_file_name; $model->attach_thumb = $upload->_thumb_name; } else { //未改变前的封面图片 $model->attach_file = $_POST['old_file']; $model->attach_thumb = $_POST['old_thumb']; } //组图 $imageList = $this->_request->getPost('imageList'); $imageListSerialize = $this->imageListSerialize($imageList); $model->image_list = $imageListSerialize['dataSerialize']; //标签 (前10个标签有效) $tags = trim($_POST['Image']['tags']); $explodeTags = array_unique(explode(',', str_replace(array(' ', ','), array('', ','), $tags))); $explodeTags = array_slice($explodeTags, 0, 10); $model->tags = implode(',', $explodeTags); $model->update_time = time(); if ($model->save()) { //更新标签数据 foreach ((array) $explodeTags as $value) { if ($value) { $model_tag = new Tag(); $get_tags = $model_tag->find('tag_name=:tagname', array(':tagname' => $value)); if ($get_tags) { $tag_id = $get_tags->id; } else { $model_tag->data_count = 1; $model_tag->tag_name = $value; $model_tag->save(); $tag_id = $model_tag->id; } //添加关联表数据 $tagData = TagData::model()->findByPk(array('tag_id' => $tag_id, 'content_id' => $model->id)); if (!$tagData) { $tagData = new TagData(); $tagData->tag_id = $tag_id; $tagData->content_id = $model->id; $tagData->type = $this->_type_ids['image']; $tagData->save(); } } } //更新关联的标签 $tagData = TagData::model()->updateAll(array('status' => $model->status), 'content_id =:id AND type =:type', array(':id' => $model->id, ':type' => $this->_type_ids['image'])); $this->message('success', Yii::t('admin', 'Update Success'), $this->createUrl('index')); } } else { $imageList = unserialize($model->image_list); $style = unserialize($model->title_style); } $this->render('update', array('model' => $model, 'imageList' => $imageList, 'style' => $style)); }