Example #1
0
 /**
  * parseMeta pulls the metadata out of a model and returns that metadata as a usable array
  * @param int  $id        The content ID to pull data from
  * @return array $items - The metadata in array format
  */
 public function parseMeta($id)
 {
     $items = array();
     $data = ContentMetadata::model()->findAllByAttributes(array('content_id' => $id));
     foreach ($data as $element) {
         $items[$element->key] = $this->isJson($element->value) ? CJSON::decode($element->value) : $element->value;
     }
     return $items;
 }
Example #2
0
 /**
  * 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();
 }
Example #3
0
 /**
  * AfterSave
  * Updates the layout and view if necessary
  * @see CActiveRecord::afterSave()
  */
 public function afterSave()
 {
     // Delete the AutoSave document on update
     if ($this->isPublished()) {
         $autosaveModel = ContentMetadata::model()->findByAttributes(array('content_id' => $this->id, 'key' => 'autosave'));
         if ($autosaveModel != NULL) {
             $autosaveModel->delete();
         }
     }
     return parent::afterSave();
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 /**
  * 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();
 }
Example #6
0
 /**
  * Removes an image from a given post
  */
 public function actionRemoveImage()
 {
     $id = Cii::get($_POST, 'id');
     $key = Cii::get($_POST, 'key');
     // Only proceed if we have valid date
     if ($id == NULL || $key == NULL) {
         throw new CHttpException(403, Yii::t('Dashboard.main', 'Insufficient data provided. Invalid request'));
     }
     $model = ContentMetadata::model()->findByAttributes(array('content_id' => $id, 'key' => $key));
     if ($model === NULL) {
         throw new CHttpException(403, Yii::t('Dashboard.main', 'Cannot delete attribute that does not exist'));
     }
     return $model->delete();
 }