Esempio n. 1
0
 private function _add_to_page($tool, $page)
 {
     # make sure the tool is an object
     if (!is_object($tool)) {
         $tool = ORM::factory('tool')->where('fk_site', $this->site_id)->find($tool);
         if (!$tool->loaded) {
             return 'invalid tool';
         }
     }
     # make sure the page is an object
     if (!is_object($page)) {
         $page = ORM::factory('page')->where('fk_site', $this->site_id)->find($page);
         if (!$page->loaded) {
             return 'invalid page';
         }
     }
     # is the tool we are trying to add protected?
     if ('yes' == $tool->system_tool->protected) {
         # is this page a root page?
         str_replace('/', '', $page->page_name, $matches);
         if (0 < $matches) {
             return 'protected tools must be on root pages';
         }
         # is this page already protected?
         if ($page_config_value = yaml::does_key_exist($this->site_name, 'pages_config', $page->page_name)) {
             return 'this page already contains a protected tool';
         }
     }
     # get min position of tools on page
     $db = Database::instance();
     $lowest = $db->query("\n      SELECT MIN(position) as lowest\n      FROM pages_tools \n      WHERE page_id ='{$page->id}'\n      AND fk_site = '{$this->site_id}'\n    ")->current()->lowest;
     # add a new record to pages_tools
     # effectively "adding this tool to this page"
     $data = array('tool_id' => $tool->id, 'page_id' => $page->id, 'fk_site' => $this->site_id, 'position' => $lowest - 1);
     $instance_id = $db->insert('pages_tools', $data)->insert_id();
     # only on successs...
     if ('yes' == $tool->system_tool->protected) {
         $toolname = strtolower($tool->system_tool->name);
         # protect the page relative to this new tool.
         $newline = "\n{$page->page_name}:{$toolname}-{$tool->parent_id}";
         yaml::add_value($this->site_name, 'pages_config', $newline);
     }
     return "{$instance_id}";
 }