/**
  * Restores a deleted object
  *
  * @access public
  * @param int $id The ID of the object to delete
  * @return bool
  **/
 public function restore($id)
 {
     return parent::update($id, array('is_deleted' => FALSE));
 }
 public function update($id, $data = array())
 {
     $_data = new stdClass();
     // --------------------------------------------------------------------------
     //	Prep the data
     if (empty($data->label)) {
         $this->_set_error('"label" is a required field.');
         return FALSE;
     } else {
         $_data->label = trim($data->label);
     }
     if (isset($data->parent_id)) {
         $_data->parent_id = (int) $data->parent_id;
         if (empty($_data->parent_id)) {
             $_data->parent_id = NULL;
         }
         if ($_data->parent_id == $id) {
             $this->_set_error('"parent_id" cannot be the same as the category\'s ID.');
             return FALSE;
         }
     }
     if (isset($data->description)) {
         $_data->description = $data->description;
     }
     if (isset($data->seo_title)) {
         $_data->seo_title = strip_tags($data->seo_title);
     }
     if (isset($data->seo_description)) {
         $_data->seo_description = strip_tags($data->seo_description);
     }
     // --------------------------------------------------------------------------
     //	Generate the slug
     //	If there's a parent then prefix the slug with the parent's slug
     if (!empty($_data->parent_id)) {
         $this->db->select('slug');
         $this->db->where('id', $_data->parent_id);
         $_parent = $this->db->get($this->_table)->row();
         if (empty($_parent)) {
             $_prefix = '';
             //	Also, invalid aprent, so NULL out parent_id
             $_data->parent_id = NULL;
         } else {
             $_prefix = $_parent->slug . '/';
         }
     } else {
         //	No parent == no prefix
         $_prefix = '';
     }
     $_data->slug = $this->_generate_slug($_data->label, $_prefix, '', NULL, NULL, $id);
     $_data->slug_end = array_pop(explode('/', $_data->slug));
     // --------------------------------------------------------------------------
     //	Attempt the update
     $this->db->trans_begin();
     if (parent::update($id, $_data)) {
         //	Success! Generate this category's breadcrumbs
         $_data = new stdClass();
         $_data->breadcrumbs = json_encode($this->_generate_breadcrumbs($id));
         if (!parent::update($id, $_data)) {
             $this->db->trans_rollback();
             $this->_set_error('Failed to update category breadcrumbs.');
             return FALSE;
         }
         // --------------------------------------------------------------------------
         //	Also regenerate breadcrumbs and slugs for all children
         $_children = $this->_get_children($id);
         if ($_children) {
             foreach ($_children as $child_id) {
                 $_child_data = new stdClass();
                 //	Breadcrumbs is easy
                 $_child_data->breadcrumbs = json_encode($this->_generate_breadcrumbs($child_id));
                 //	Slugs are slightly harder, we need to get the child's parent's slug
                 //	and use it as a prefix
                 $this->db->select('parent_id, label');
                 $this->db->where('id', $child_id);
                 $_child = $this->db->get($this->_table)->row();
                 if (!empty($_child)) {
                     $this->db->select('slug');
                     $this->db->where('id', $_child->parent_id);
                     $_parent = $this->db->get($this->_table)->row();
                     $_prefix = empty($_parent) ? '' : $_parent->slug . '/';
                     $_child_data->slug = $this->_generate_slug($_child->label, $_prefix, '', NULL, NULL, $child_id);
                     $_child_data->slug_end = array_pop(explode('/', $_child_data->slug));
                 }
                 if (!parent::update($child_id, $_child_data)) {
                     $this->db->trans_rollback();
                     $this->_set_error('Failed to update child category.');
                     return FALSE;
                 }
             }
         }
         $this->db->trans_commit();
         return TRUE;
     } else {
         $this->db->trans_rollback();
         return FALSE;
     }
 }
Example #3
0
 public function update($page_id, $data)
 {
     //	Check the data
     if (empty($data->data->template)) {
         $this->_set_error('"data.template" is a required field.');
         return FALSE;
     }
     // --------------------------------------------------------------------------
     //	Fetch the current version of this page, for reference.
     $_current = $this->get_by_id($page_id);
     if (!$_current) {
         $this->_set_error('Invalid Page ID');
         return FALSE;
     }
     // --------------------------------------------------------------------------
     //	Clone the data object so we can mutate it without worry. Unset id and
     //	hash as we don't need to store them
     $_clone = clone $data;
     unset($_clone->id);
     unset($_clone->hash);
     // --------------------------------------------------------------------------
     //	Start the transaction
     $this->db->trans_begin();
     // --------------------------------------------------------------------------
     //	Start prepping the data which doesn't require much thinking
     $_data = new stdClass();
     $_data->draft_parent_id = !empty($_clone->data->parent_id) ? (int) $_clone->data->parent_id : NULL;
     $_data->draft_title = !empty($_clone->data->title) ? trim($_clone->data->title) : 'Untitled';
     $_data->draft_seo_title = !empty($_clone->data->seo_title) ? trim($_clone->data->seo_title) : '';
     $_data->draft_seo_description = !empty($_clone->data->seo_description) ? trim($_clone->data->seo_description) : '';
     $_data->draft_seo_keywords = !empty($_clone->data->seo_keywords) ? trim($_clone->data->seo_keywords) : '';
     $_data->draft_template = $_clone->data->template;
     $_data->draft_template_data = json_encode($_clone, JSON_UNESCAPED_SLASHES);
     $_data->draft_hash = md5($_data->draft_template_data);
     // --------------------------------------------------------------------------
     //	Additional sanitising; encode HTML entities. Also encode the pipe character
     //	in the title, so that it doesn't break our explode
     $_data->draft_title = htmlentities(str_replace('|', '|', $_data->draft_title), ENT_COMPAT | ENT_HTML401, 'UTF-8', FALSE);
     $_data->draft_seo_title = htmlentities($_data->draft_seo_title, ENT_COMPAT | ENT_HTML401, 'UTF-8', FALSE);
     $_data->draft_seo_description = htmlentities($_data->draft_seo_description, ENT_COMPAT | ENT_HTML401, 'UTF-8', FALSE);
     $_data->draft_seo_keywords = htmlentities($_data->draft_seo_keywords, ENT_COMPAT | ENT_HTML401, 'UTF-8', FALSE);
     // --------------------------------------------------------------------------
     //	Prep data which requires a little more intensive processing
     // --------------------------------------------------------------------------
     //	Work out the slug
     if ($_data->draft_parent_id) {
         //	There is a parent, so set it's slug as the prefix
         $_parent = $this->get_by_id($_data->draft_parent_id);
         if (!$_parent) {
             $this->_set_error('Invalid Parent ID.');
             $this->db->trans_rollback();
             return FALSE;
         }
         $_prefix = $_parent->draft->slug . '/';
     } else {
         //	No parent, no need for a prefix
         $_prefix = '';
     }
     $_data->draft_slug = $this->_generate_slug($_data->draft_title, $_prefix, '', NULL, 'draft_slug', $_current->id);
     $_data->draft_slug_end = end(explode('/', $_data->draft_slug));
     // --------------------------------------------------------------------------
     //	Generate the breadcrumbs
     $_data->draft_breadcrumbs = array();
     if ($_data->draft_parent_id) {
         //	There is a parent, use it's breadcrumbs array as the starting point.
         //	No need to fetch the parent again.
         $_data->draft_breadcrumbs = $_parent->draft->breadcrumbs;
     }
     $_temp = new stdClass();
     $_temp->id = $_current->id;
     $_temp->title = $_data->draft_title;
     $_temp->slug = $_data->draft_slug;
     $_data->draft_breadcrumbs[] = $_temp;
     unset($_temp);
     //	Encode the breadcrumbs for the database
     $_data->draft_breadcrumbs = json_encode($this->_generate_breadcrumbs($_current->id));
     // --------------------------------------------------------------------------
     if (parent::update($_current->id, $_data)) {
         //	Update was successful, set the breadcrumbs
         $_breadcrumbs = $this->_generate_breadcrumbs($_current->id);
         $this->db->set('draft_breadcrumbs', json_encode($_breadcrumbs));
         $this->db->where('id', $_current->id);
         if (!$this->db->update($this->_table)) {
             $this->_set_error('Failed to generate breadcrumbs.');
             $this->db->trans_rollback();
             return FALSE;
         }
         //	For each child regenerate the breadcrumbs and slugs (only if the title or slug has changed)
         if ($_current->draft->title != $_data->draft_title || $_current->draft->slug != $_data->draft_slug) {
             $_children = $this->get_ids_of_children($_current->id);
             if ($_children) {
                 //	Loop each child and update it's details
                 foreach ($_children as $child_id) {
                     //	We can assume that the children are in a sensible order, loop them and
                     //	process. For nested children, their parent will have been processed by
                     //	the time we process it.
                     $_child = $this->get_by_id($child_id);
                     if (!$_child) {
                         continue;
                     }
                     $_data = new stdClass();
                     //	Generate the breadcrumbs
                     $_data->draft_breadcrumbs = json_encode($this->_generate_breadcrumbs($_child->id));
                     //	Generate the slug
                     if ($_child->draft->parent_id) {
                         //	Child has a parent, fetch it and use it's slug as the prefix
                         $_parent = $this->get_by_id($_child->draft->parent_id);
                         if ($_parent) {
                             $_data->draft_slug = $_parent->draft->slug . '/' . $_child->draft->slug_end;
                         } else {
                             //	Parent is bad, make this a parent page. Poor wee orphan.
                             $_data->draft_parent_id = NULL;
                             $_data->draft_slug = $_child->draft->slug_end;
                         }
                     } else {
                         //	Would be weird if this happened, but ho hum handle it anyway
                         $_data->draft_parent_id = NULL;
                         $_data->draft_slug = $_child->draft->slug_end;
                     }
                     //	Update the child and move on
                     if (!parent::update($_child->id, $_data)) {
                         $this->_set_error('Failed to update breadcrumbs and/or slug of child page.');
                         $this->db->trans_rollback();
                         return FALSE;
                     }
                 }
             }
         }
         // --------------------------------------------------------------------------
         //	Finish up.
         $this->db->trans_commit();
         return TRUE;
     } else {
         $this->_set_error('Failed to update page object.');
         $this->db->trans_rollback();
         return FALSE;
     }
 }