/**
  * Add a layout
  * @param <type> $layout
  * @param <type> $description
  * @param <type> $tags
  * @param <type> $userid
  * @param <type> $templateId
  * @param <type> $templateId
  * @param <string> $xml Use the provided XML instead of a template
  * @return <type>
  */
 public function Add($layout, $description, $tags, $userid, $templateId, $resolutionId, $xml = '')
 {
     Debug::LogEntry('audit', 'Adding new Layout', 'Layout', 'Add');
     try {
         $dbh = PDOConnect::init();
         $currentdate = date("Y-m-d H:i:s");
         // We must provide either a template or a resolution
         if ($templateId == 0 && $resolutionId == 0 && $xml == '') {
             $this->ThrowError(__('To add a Layout either a Template or Resolution must be provided'));
         }
         // Validation
         if (strlen($layout) > 50 || strlen($layout) < 1) {
             $this->ThrowError(25001, __("Layout Name must be between 1 and 50 characters"));
         }
         if (strlen($description) > 254) {
             $this->ThrowError(25002, __("Description can not be longer than 254 characters"));
         }
         if (strlen($tags) > 254) {
             $this->ThrowError(25003, __("Tags can not be longer than 254 characters"));
         }
         // Ensure there are no layouts with the same name
         $sth = $dbh->prepare('SELECT layout FROM `layout` WHERE layout = :layout AND userID = :userid');
         $sth->execute(array('layout' => $layout, 'userid' => $userid));
         if ($row = $sth->fetch()) {
             $this->ThrowError(25004, sprintf(__("You already own a layout called '%s'. Please choose another name."), $layout));
         }
         Debug::LogEntry('audit', 'Validation Compelte', 'Layout', 'Add');
         // End Validation
         // Are we coming from a template?
         if ($templateId != '' && $templateId != 0) {
             // Copy the template layout and adjust
             if (!($id = $this->Copy($templateId, $layout, $description, $userid, true))) {
                 throw new Exception(__('Unable to use this template'));
             }
         } else {
             // We should use the resolution or the provided XML.
             if ($xml != '') {
                 $initialXml = $xml;
             } else {
                 // Do we have a template?
                 if (!($initialXml = $this->GetInitialXml($resolutionId, $userid))) {
                     throw new Exception(__('Unable to get initial XML'));
                 }
             }
             Debug::LogEntry('audit', 'Retrieved template xml', 'Layout', 'Add');
             $SQL = 'INSERT INTO layout (layout, description, userID, createdDT, modifiedDT, xml, status)';
             $SQL .= ' VALUES (:layout, :description, :userid, :createddt, :modifieddt, :xml, :status)';
             $sth = $dbh->prepare($SQL);
             $sth->execute(array('layout' => $layout, 'description' => $description, 'userid' => $userid, 'createddt' => $currentdate, 'modifieddt' => $currentdate, 'xml' => $initialXml, 'status' => 3));
             $id = $dbh->lastInsertId();
             // Create a campaign
             $campaign = new Campaign($this->db);
             $campaignId = $campaign->Add($layout, 1, $userid);
             $campaign->Link($campaignId, $id, 0);
             // What permissions should we create this with?
             if (Config::GetSetting('LAYOUT_DEFAULT') == 'public') {
                 $security = new CampaignSecurity($this->db);
                 $security->LinkEveryone($campaignId, 1, 0, 0);
                 // Permissions on the new region(s)?
                 $layout = new Layout($this->db);
                 foreach ($layout->GetRegionList($id) as $region) {
                     $security = new LayoutRegionGroupSecurity($this->db);
                     $security->LinkEveryone($id, $region['regionid'], 1, 0, 0);
                 }
             }
         }
         // By this point we should have a layout record created
         // Are there any tags?
         if ($tags != '') {
             // Create an array out of the tags
             $tagsArray = explode(',', $tags);
             // Add the tags XML to the layout
             if (!$this->EditTags($id, $tagsArray)) {
                 $this->ThrowError(__('Unable to edit tags'));
             }
         }
         Debug::LogEntry('audit', 'Complete', 'Layout', 'Add');
         return $id;
     } catch (Exception $e) {
         Debug::LogEntry('error', $e->getMessage());
         if (!$this->IsError()) {
             $this->SetError(25005, __('Could not add Layout'));
         }
         return false;
     }
 }
 /**
  * Adds a region to the specified layoutid
  * @param $layoutid Object
  * @param $regionid Object[optional]
  * @return string The region id
  */
 public function AddRegion($layoutid, $userid, $regionid = "", $width = 250, $height = 250, $top = 50, $left = 50, $name = '')
 {
     Debug::LogEntry('audit', 'LayoutId: ' . $layoutid . ', Width: ' . $width . ', Height: ' . $height . ', Top: ' . $top . ', Left: ' . $left . ', Name: ' . $name . '.', 'region', 'AddRegion');
     //Load the XML for this layout
     $xml = new DOMDocument("1.0");
     $xml->loadXML($this->GetLayoutXml($layoutid));
     //Do we have a region ID provided?
     if ($regionid == '') {
         $regionid = Kit::uniqueId();
     }
     // Validation
     if (!is_numeric($width) || !is_numeric($height) || !is_numeric($top) || !is_numeric($left)) {
         return $this->SetError(__('Size and coordinates must be generic'));
     }
     if ($width <= 0) {
         return $this->SetError(__('Width must be greater than 0'));
     }
     if ($height <= 0) {
         return $this->SetError(__('Height must be greater than 0'));
     }
     // make a new region node
     $newRegion = $xml->createElement("region");
     if ($name != '') {
         $newRegion->setAttribute('name', $name);
     }
     $newRegion->setAttribute('id', $regionid);
     $newRegion->setAttribute('userId', $userid);
     $newRegion->setAttribute('width', $width);
     $newRegion->setAttribute('height', $height);
     $newRegion->setAttribute('top', $top);
     $newRegion->setAttribute('left', $left);
     $xml->firstChild->appendChild($newRegion);
     if (!$this->SetLayoutXml($layoutid, $xml->saveXML())) {
         return false;
     }
     // What permissions should we create this with?
     if (Config::GetSetting('LAYOUT_DEFAULT') == 'public') {
         Kit::ClassLoader('layoutregiongroupsecurity');
         $security = new LayoutRegionGroupSecurity($this->db);
         $security->LinkEveryone($layoutid, $regionid, 1, 0, 0);
     }
     // Update layout status
     Kit::ClassLoader('Layout');
     $layout = new Layout($this->db);
     $layout->SetValid($layoutid, true);
     return $regionid;
 }