public static function handleRequest()
 {
     global $Session;
     $Session->requireAuthentication();
     if (static::peekPath() == 'json') {
         static::$responseMode = static::shiftPath();
     }
     return static::respond('drafts', array('success' => true, 'data' => CMS_Content::getAllByWhere(array('AuthorID' => $Session->PersonID, 'Status = "Draft" OR Published > CURRENT_TIMESTAMP'))));
 }
 public static function handleEditRequest(CMS_Content $Content)
 {
     $data = static::getRequestData();
     //Debug::dump($Content);
     //Debug::dump($Content->Items);
     // edit Content Object
     $Content->Title = $data['Title'];
     $Content->save();
     // edit text ContentItem object
     $ContentItem = array_shift($Content->Items);
     // we need the first element out of the array but the key will not always be 0. The key is the actually the object's SQL assigned ID
     $ContentItem->Data = $data['Data'];
     $ContentItem->save();
     // edit Categories
     $CategoryItems = CategoryItem::getAllByWhere("`ContextClass`='CMS_BlogPost' AND `ContextID`='{$Content->ID}'");
     if (count($CategoryItems)) {
         // check which need to be removed
         foreach ($CategoryItems as $CategoryItem) {
             if (!in_array($CategoryItem->CategoryID, $data['Categories'])) {
                 // if existing CategoryItem isn't found in the input, destroy it
                 $CategoryItem->destroy();
             }
         }
         // go through input data and create new CategoryItems if not found
         if (is_array($data['Categories']) && count($data['Categories'])) {
             foreach ($data['Categories'] as $cat) {
                 $found = false;
                 foreach ($CategoryItems as $CategoryItem) {
                     if ($CategoryItem->CategoryID == $cat) {
                         $found = true;
                     }
                 }
                 if (!$found) {
                     $Category = Category::getByID($cat);
                     $CategoryItem = new CategoryItem();
                     $CategoryItem->ContextClass = 'CMS_BlogPost';
                     $CategoryItem->ContextID = $Content->ID;
                     $CategoryItem->CategoryID = $Category->ID;
                     $CategoryItem->save();
                 }
             }
         }
     }
     //Debug::dump($data['Categories']);
     // edit Tags
     $TagItems = TagItem::getAllByWhere("`ContextClass`='CMS_BlogPost' AND `ContextID`='{$Content->ID}'");
     if (count($TagItems)) {
         // check which need to be removed
         foreach ($TagItems as $TagItem) {
             $Tag = Tag::getByID($TagItem->TagID);
             if (!in_array($Tag->Title, $data['Tags'])) {
                 // if existing Tag isn't found in the input, destroy it's TagItem
                 $TagItem->destroy();
             }
         }
         // go through input data and create new TagItems if not found
         if (is_array($data['Tags']) && count($data['Tags'])) {
             foreach ($data['Tags'] as $word) {
                 $found = false;
                 foreach ($TagItems as $TagItem) {
                     $Tag = Tag::getByID($TagItem->TagID);
                     if ($Tag->Title == $word) {
                         $found = true;
                     }
                 }
                 if (!$found) {
                     $Tag = Tag::getFromHandle($word, true);
                     // second boolean is telling method to make the Tag object for us if it doesn't exist
                     $TagItem = new TagItem();
                     $TagItem->ContextClass = 'CMS_BlogPost';
                     $TagItem->ContextID = $Content->ID;
                     $TagItem->TagID = $Tag->ID;
                     $TagItem->save();
                 }
             }
         }
     }
     //Debug::dump($data['Tags']);
     //Debug::dump($Content->Tags);
     return static::respondCRUD($Content, 'singular', 'updated');
 }
 public static function getAllPublishedByContextObject(ActiveRecord $Context, $options = array())
 {
     $options = MICS::prepareOptions($options, array('conditions' => array()));
     $options['conditions']['Class'] = __CLASS__;
     return parent::getAllPublishedByContextObject($Context, $options);
 }