/**
  * handles the adding of a channel to a layout
  *
  * @param $channel \b id of channel to add
  * @param $target \b id of tab to add channel to
  */
 public function add($channel, $target = null)
 {
     $default_layout = false;
     ignore_user_abort();
     if ($_SESSION['generic_user_type'] == 'portalord') {
         $default_layout = true;
     }
     MyPortal::force_clone($this->portal);
     $channel_id = str_replace('channel-', '', $channel);
     $target = str_replace('tab-', '', $target);
     //
     // if portal was just cloned, channel ids must be updated.
     // they referred to the default layout in the ui, but we will actually update
     // the custom user layout.
     //
     // @todo account for admins updating the default layout (GET param?)
     //
     //if we just cloned the layout, set the target as the child tab
     if ($this->portal->cloned) {
         $target = MyUserTab::get_child_id($this->portal->person, $target);
     }
     if (!$this->portal->tabs($target)) {
         $default_portal = new MyPortal(0);
         $default_tab_slug = $default_portal->tabs($target)->slug;
         $target = $this->portal->tabs($default_tab_slug)->id;
         $default_channel = $default_portal->tabs($default_tab_slug)->channels($channel_id);
         $sql = "SELECT id FROM userchannels WHERE channel_id = ? AND usertab_id = ? AND col_num = ? AND sort_order = ?";
         $channel_id = PSU::db('portal')->GetOne($sql, array($channel_id, $this->portal->tabs($default_tab_slug)->id, $default_channel->col_num, $default_channel->sort_order));
     }
     //end if
     //if we're spoofing the portalord role, edit the default layout rather than the cloned one
     if ($default_layout) {
         if (!$default_portal) {
             $default_portal = new MyPortal(0);
         }
         $target = $default_portal->tabs($target)->base->id;
     }
     $sql = "SELECT MAX(sort_order) FROM userchannels WHERE usertab_id = ? AND col_num = 2";
     $sort_order = psu::db('portal')->GetOne($sql, $target);
     $channel = new MyUserChannel(array('channel_id' => $channel_id));
     $channel->col_num = 2;
     $channel->sort_order = $sort_order + 1;
     $channel->usertab_id = $target ? $target : 1;
     $channel->save();
     echo $this->portal->is_default_layout() ? 'default' : 'user';
 }
 /**
  * Function to save or update object data back into the database
  */
 public function save()
 {
     $class = get_class($this);
     $portal = new MyPortal($GLOBALS['identifier']);
     //
     // check the database to ensure this user owns the tab in question
     //
     // TODO: do we have to instantiate the whole MyPortal object?
     //
     if (self::dbstr($class, 'type') == 'channel') {
         if ($portal->tabs($this->usertab_id, false)->wp_id != $GLOBALS['identifier']) {
             if (!$portal->is_admin()) {
                 return false;
             }
         }
     } elseif (self::dbstr($class, 'type') == 'tab') {
         if ($this->wp_id != $GLOBALS['identifier']) {
             return false;
         }
     }
     //end elseif
     // if we've passed all the validation checks, save the data
     $this->_save();
 }