/** * Promotes an image to blog-image * @param string $key The key to be promoted * @return boolean If the change was sucessfuly commited */ private function _promote($key = NULL) { $promotedKey = 'blog-image'; // Only proceed if we have valid date if ($this->_id == NULL || $key == NULL) { return false; } $model = ContentMetadata::model()->findByAttributes(array('content_id' => $this->_id, 'key' => $key)); // If the current model is already blog-image, return true (consider it a successful promotion, even though we didn't do anything) if ($model->key == $promotedKey) { return true; } $model2 = ContentMetadata::model()->findByAttributes(array('content_id' => $this->_id, 'key' => $promotedKey)); if ($model2 === NULL) { $model2 = new ContentMetadata(); $model2->content_id = $this->_id; $model2->key = $promotedKey; } $model2->value = $model->value; if (!$model2->save()) { return false; } return true; }
/** * Saves or deletes the view in the db if necessary */ private function saveView() { $model = ContentMetadata::model()->findByAttributes(array('content_id' => $this->id, 'key' => 'view')); // If we don't have anything in ContentMetadata and the layout file is blog if ($model === NULL && $this->viewFile === 'blog') { return; } if ($model === NULL) { $model = new ContentMetadata(); $model->content_id = $this->id; $model->key = 'view'; } // If this is an existing record, and we're changing it to blog, delete it instead of saving. if ($this->viewFile == 'blog' && !$model->isNewRecord) { return $model->delete(); } $model->value = $this->viewFile; return $model->save(); }
/** * Provides functionality for "liking and un-liking" a post * @param int $id The Content ID */ public function actionLike($id = NULL) { $this->layout = false; header('Content-type: application/json'); // Load the content $content = ContentMetadata::model()->findByAttributes(array('content_id' => $id, 'key' => 'likes')); if ($content === NULL) { $content = new ContentMetadata(); $content->content_id = $id; $content->key = 'likes'; $content->value = 0; } if ($id === NULL || $content === NULL) { echo CJavaScript::jsonEncode(array('status' => 'error', 'message' => Yii::t('ciims.controllers.Content', 'Unable to access post'))); return Yii::app()->end(); } // Load the user likes, create one if it does not exist $user = UserMetadata::model()->findByAttributes(array('user_id' => Yii::app()->user->id, 'key' => 'likes')); if ($user === NULL) { $user = new UserMetadata(); $user->user_id = Yii::app()->user->id; $user->key = 'likes'; $user->value = json_encode(array()); } $type = "inc"; $likes = json_decode($user->value, true); if (in_array($id, array_values($likes))) { $type = "dec"; $content->value -= 1; if ($content->value <= 0) { $content->value = 0; } $element = array_search($id, $likes); unset($likes[$element]); } else { $content->value += 1; array_push($likes, $id); } $user->value = json_encode($likes); if (!$user->save()) { echo CJavaScript::jsonEncode(array('status' => 'error', 'message' => Yii::t('ciims.controllers.Content', 'Unable to save user like'))); return Yii::app()->end(); } if (!$content->save()) { echo CJavaScript::jsonEncode(array('status' => 'error', 'message' => Yii::t('ciims.controllers.Content', 'Unable to save like'))); return Yii::app()->end(); } echo CJavaScript::jsonEncode(array('status' => 'success', 'type' => $type, 'message' => Yii::t('ciims.controllers.Content', 'Liked saved'))); return Yii::app()->end(); }