Source for file gacl_api.class.php

Documentation is available at gacl_api.class.php

  1. <?php
  2. /**
  3. @package Mambo
  4. @author Mambo Foundation Inc see README.php
  5. @copyright Mambo Foundation Inc.
  6. *  See COPYRIGHT.php for copyright notices and details.
  7. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see
  8. *  LICENSE.php
  9. *  Mambo is free software; you can redistribute it and/or
  10. *  modify it under the terms of the GNU General Public License
  11. *  as published by the Free Software Foundation; version 2 of the
  12. *  License.
  13. */ 
  14.  
  15. /*
  16.  * phpGACL - Generic Access Control List
  17.  * Copyright (C) 2002,2003 Mike Benoit
  18.  *
  19.  * This library is free software; you can redistribute it and/or
  20.  * modify it under the terms of the GNU Lesser General Public
  21.  * License as published by the Free Software Foundation; either
  22.  * version 2.1 of the License, or (at your option) any later version.
  23.  *
  24.  * This library is distributed in the hope that it will be useful,
  25.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  27.  * Lesser General Public License for more details.
  28.  *
  29.  * You should have received a copy of the GNU Lesser General Public
  30.  * License along with this library; if not, write to the Free Software
  31.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  32.  *
  33.  * For questions, help, comments, discussion, etc., please join the
  34.  * phpGACL mailing list. http://sourceforge.net/mail/?group_id=57103
  35.  *
  36.  * You may contact the author of phpGACL by e-mail at:
  37.  * ipso@snappymail.ca
  38.  *
  39.  * The latest version of phpGACL can be obtained from:
  40.  * http://phpgacl.sourceforge.net/
  41.  *
  42.  */
  43.  
  44. /** ensure this file is being included by a parent file */
  45. defined'_VALID_MOS' or die'Direct Access to this location is not allowed.' );
  46.  
  47. /*
  48.  *
  49.  *
  50.  *  == If you find a feature may be missing from this API, please email me: ipso@snappymail.ca and I will be happy to add it. ==
  51.  *
  52.  *
  53.  * Example:
  54.  *    $gacl_api = new gacl_api;
  55.  *
  56.  *    $section_id = $gacl_api->get_aco_section_id('System');
  57.  *    $aro_id= $gacl_api->add_aro($section_id, 'John Doe', 10);
  58.  *
  59.  * For more examples, see the Administration interface, as it makes use of nearly every API Call.
  60.  *
  61.  */
  62.  
  63. class gacl_api extends gacl {
  64.     /*
  65.      * Administration interface settings
  66.      */
  67.     var $_items_per_page = 100;
  68.     var $_max_select_box_items = 100;
  69.     var $_max_search_return_items = 100;
  70.  
  71.     /*
  72.      *
  73.      * Misc helper functions.
  74.      *
  75.      */
  76.  
  77.     /*======================================================================*\
  78.         Function:   showarray()
  79.         Purpose:    Dump all contents of an array in HTML (kinda).
  80.     \*======================================================================*/
  81.     function showarray($array{
  82.         echo "<br><pre>\n";
  83.         var_dump($array);
  84.         echo "</pre><br>\n";
  85.     }
  86.  
  87.     /*======================================================================*\
  88.         Function:   $gacl_api->return_page()
  89.         Purpose:    Sends the user back to a passed URL, unless debug is enabled, then we don't redirect.
  90.                         If no URL is passed, try the REFERER
  91.     \*======================================================================*/
  92.     function return_page($url=""{
  93.         global $_SERVER$debug;
  94.  
  95.         if (empty($urlAND !empty($_SERVER[HTTP_REFERER])) {
  96.             $this->debug_text("return_page(): URL not set, using referer!");
  97.             $url $_SERVER[HTTP_REFERER];
  98.         }
  99.  
  100.         if (!$debug OR $debug==0{
  101.             header("Location: $url\n\n");
  102.         else {
  103.             $this->debug_text("return_page(): URL: $url -- Referer: $_SERVER[HTTP_REFERRER]");
  104.         }
  105.     }
  106.  
  107.     /*======================================================================*\
  108.         Function:   get_paging_data()
  109.         Purpose:    Creates a basic array for Smarty to deal with paging large recordsets.
  110.                         Pass it the ADODB recordset.
  111.     \*======================================================================*/
  112.     function get_paging_data($rs{
  113.                 return array(
  114.                                 'prevpage' => $rs->absolutepage(1,
  115.                                 'currentpage' => $rs->absolutepage(),
  116.                                 'nextpage' => $rs->absolutepage(1,
  117.                                 'atfirstpage' => $rs->atfirstpage(),
  118.                                 'atlastpage' => $rs->atlastpage(),
  119.                                 'lastpageno' => $rs->lastpageno()
  120.                         );
  121.     }
  122.  
  123.     /*======================================================================*\
  124.         Function:    count_all()
  125.         Purpose:    Recursively counts elements in an array and sub-arrays.
  126.                     The returned count is a count of all scalar elements found.
  127.                     
  128.                     This is different from count($arg, COUNT_RECURSIVE)
  129.                     in PHP >= 4.2.0, which includes sub-arrays in the count.
  130.     \*======================================================================*/
  131.     function count_all($arg NULL{
  132.         switch (TRUE{
  133.             case is_scalar($arg):
  134.             case is_object($arg):
  135.                 // single object
  136.                 return 1;
  137.             case is_array($arg):
  138.                 // call recursively for all elements of $arg
  139.                 $count 0;
  140.                 foreach ($arg as $val{
  141.                     $count += $this->count_all($val);
  142.                 }
  143.                 return $count;
  144.         }
  145.         return FALSE;
  146.     }
  147.  
  148.     /*======================================================================*\
  149.         Function:    get_version()
  150.         Purpose:    Grabs phpGACL version from the database.
  151.     \*======================================================================*/
  152.  
  153.  
  154.     /*======================================================================*\
  155.         Function:    get_schema_version()
  156.         Purpose:    Grabs phpGACL schema version from the database.
  157.     \*======================================================================*/
  158.     /*
  159.      *
  160.      * ACL
  161.      *
  162.      */
  163.  
  164.     /*======================================================================*\
  165.         Function:    consolidated_edit_acl()
  166.         Purpose:    Add's an ACL but checks to see if it can consolidate it with another one first.
  167.                     This ONLY works with ACO's and ARO's. Groups, and AXO are excluded.
  168.                     As well this function is designed for handling ACLs with return values,
  169.                     and consolidating on the return_value, in hopes of keeping the ACL count to a minimum.
  170.  
  171.                     A return value of false must _always_ be handled outside this function.
  172.                     As this function will remove AROs from ACLs and return false, in most cases
  173.                     you will need to a create a completely new ACL on a false return.
  174.     \*======================================================================*/
  175.     
  176.     /*======================================================================*\
  177.         Function:    shift_acl()
  178.         Purpose:    Opposite of append_acl(). Removes objects from a specific ACL. (named after PHP's array_shift())
  179.     \*======================================================================*/
  180.  
  181.     /*======================================================================*\
  182.         Function:    get_acl()
  183.         Purpose:    Grabs ACL data.
  184.     \*======================================================================*/
  185.     
  186.     /*======================================================================*\
  187.         Function:    is_conflicting_acl()
  188.         Purpose:    Checks for conflicts when adding a specific ACL.
  189.     \*======================================================================*/
  190.     
  191.     /*======================================================================*\
  192.         Function:    add_acl()
  193.         Purpose:    Add's an ACL. ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays.
  194.     \*======================================================================*/
  195.  
  196.     /*======================================================================*\
  197.         Function:    edit_acl()
  198.         Purpose:    Edit's an ACL, ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays.
  199.     \*======================================================================*/
  200.  
  201.     /*======================================================================*\
  202.         Function:    del_acl()
  203.         Purpose:    Deletes a given ACL
  204.     \*======================================================================*/
  205.  
  206.  
  207.     /*
  208.      *
  209.      * Groups
  210.      *
  211.      */
  212.  
  213.     /*======================================================================*\
  214.         Function:    sort_groups()
  215.         Purpose:    Grabs all the groups from the database doing preliminary grouping by parent
  216.     \*======================================================================*/
  217.  
  218.     /*======================================================================*\
  219.         Function:    format_groups()
  220.         Purpose:    Takes the array returned by sort_groups() and formats for human consumption.
  221.     \*======================================================================*/
  222.  
  223.     /*======================================================================*\
  224.         Function:    get_group_id()
  225.         Purpose:    Gets the group_id given the name.
  226.                         Will only return one group id, so if there are duplicate names, it will return false.
  227.     \*======================================================================*/
  228.     function get_group_id($name null$group_type 'ARO'{
  229.  
  230.         $this->debug_text("get_group_id(): Name: $name");
  231.  
  232.         switch(strtolower(trim($group_type))) {
  233.             case 'axo':
  234.                 $table $this->_db_table_prefix .'axo_groups';
  235.                 break;
  236.             default:
  237.                 $table $this->_db_table_prefix .'aro_groups';
  238.                 break;
  239.         }
  240.  
  241.         $name trim($name);
  242.  
  243.         if (empty($name) ) {
  244.             $this->debug_text("get_group_id(): name ($name) is empty, this is required");
  245.             return false;
  246.         }
  247.  
  248.         $this->db->setQuery"SELECT group_id FROM $table WHERE name='$name');
  249.  
  250.         $rows $this->db->loadRowList();
  251.         if ($this->db->getErrorNum()) {
  252.             $this->debug_db('get_group_id');
  253.             return false;
  254.         }
  255.  
  256.         $row_count count$rows );
  257.  
  258.         if ($row_count 1{
  259.             $this->debug_text("get_group_id(): Returned $row_count rows, can only return one. Please make your names unique.");
  260.             return false;
  261.         }
  262.  
  263.         if ($row_count == 0{
  264.             $this->debug_text("get_group_id(): Returned $row_count rows");
  265.             return false;
  266.         }
  267.  
  268.         $row $rows[0];
  269.  
  270.         //Return the ID.
  271.         return $row[0];
  272.     }
  273.  
  274.     /*======================================================================*\
  275.         Function:    get_group_name()
  276.         Purpose:    Gets the name given the group_id.
  277.                         Will only return one group id, so if there are duplicate names, it will return false.
  278.     \*======================================================================*/
  279.     function get_group_name($group_id null$group_type 'ARO'{
  280.  
  281.         $this->debug_text("get_group_name(): ID: $group_id");
  282.  
  283.         switch(strtolower(trim($group_type))) {
  284.             case 'axo':
  285.                 $table $this->_db_table_prefix .'axo_groups';
  286.                 break;
  287.             default:
  288.                 $table $this->_db_table_prefix .'aro_groups';
  289.                 break;
  290.         }
  291.  
  292.         $group_id intval($group_id);
  293.  
  294.         if (!$group_id{
  295.             $this->debug_text("get_group_name(): group_id ($group_id) is empty, this is required");
  296.             return false;
  297.         }
  298.  
  299.         $this->db->setQuery"SELECT name FROM $table WHERE group_id='$group_id');
  300.  
  301.         $rows $this->db->loadRowList();
  302.         if ($this->db->getErrorNum()) {
  303.             $this->debug_db('get_group_name');
  304.             return false;
  305.         }
  306.  
  307.         $row_count count$rows );
  308.  
  309.         if ($row_count 1{
  310.             $this->debug_text("get_group_name(): Returned $row_count rows, can only return one. Please make your names unique.");
  311.             return false;
  312.         }
  313.  
  314.         if ($row_count == 0{
  315.             $this->debug_text("get_group_name(): Returned $row_count rows");
  316.             return false;
  317.         }
  318.  
  319.         $row $rows[0];
  320.  
  321.         //Return the ID.
  322.         return $row[0];
  323.     }
  324.  
  325.     /*======================================================================*\
  326.         Function:    get_group_children()
  327.         Purpose:    Gets a groups child IDs
  328.     \*======================================================================*/
  329.     function get_group_children($group_id$group_type 'ARO'$recurse 'NO_RECURSE'{
  330.         $this->debug_text("get_group_children(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse");
  331.  
  332.         switch (strtolower(trim($group_type))) {
  333.             case 'axo':
  334.                 $group_type 'axo';
  335.                 $table $this->_db_table_prefix .'axo_groups';
  336.                 break;
  337.             default:
  338.                 $group_type 'aro';
  339.                 $table $this->_db_table_prefix .'aro_groups';
  340.         }
  341.  
  342.         if (empty($group_id)) {
  343.             $this->debug_text("get_group_children(): ID ($group_id) is empty, this is required");
  344.             return FALSE;
  345.         }
  346.  
  347.         $query  '
  348.                 SELECT        g1.group_id
  349.                 FROM        '$table .' g1';
  350.  
  351.         //FIXME-mikeb: Why is group_id in quotes?
  352.         switch (strtoupper($recurse)) {
  353.             case 'RECURSE':
  354.                 $query .= '
  355.                 LEFT JOIN     '$table .' g2 ON g2.lft<g1.lft AND g2.rgt>g1.rgt
  356.                 WHERE        g2.group_id='$group_id;
  357.                 break;
  358.             default:
  359.                 $query .= '
  360.                 WHERE        g1.parent_id='$group_id;
  361.         }
  362.  
  363.         $query .= '
  364.                 ORDER BY    g1.name';
  365.         
  366.  
  367.         $this->db->setQuery$query );
  368.         return $this->db->loadResultArray();
  369.     }
  370.  
  371.     /*======================================================================*\
  372.         Function:    get_group_data()
  373.         Purpose:    Gets the group data given the GROUP_ID.                        
  374.     \*======================================================================*/
  375.  
  376.     /*======================================================================*\
  377.         Function:    get_group_parent_id()
  378.         Purpose:    Grabs the parent_id of a given group
  379.     \*======================================================================*/
  380.  
  381.     /*======================================================================*\
  382.         Function:    get_group_children()
  383.         Purpose:    Gets a groups child IDs
  384.     \*======================================================================*/
  385.     function get_group_parents($group_id$group_type 'ARO'$recurse 'NO_RECURSE'{
  386.         $this->debug_text("get_group_parents(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse");
  387.  
  388.         switch (strtolower(trim($group_type))) {
  389.             case 'axo':
  390.                 $group_type 'axo';
  391.                 $table $this->_db_table_prefix .'axo_groups';
  392.                 break;
  393.             default:
  394.                 $group_type 'aro';
  395.                 $table $this->_db_table_prefix .'aro_groups';
  396.         }
  397.  
  398.         if (empty($group_id)) {
  399.             $this->debug_text("get_group_parents(): ID ($group_id) is empty, this is required");
  400.             return FALSE;
  401.         }
  402.  
  403.         $query  '
  404.                 SELECT        g2.group_id
  405.                 FROM        '$table .' g1';
  406.  
  407.         //FIXME-mikeb: Why is group_id in quotes?
  408.         switch (strtoupper($recurse)) {
  409.             case 'RECURSE':
  410.                 $query .= '
  411.                 LEFT JOIN     '$table .' g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt
  412.                 WHERE        g1.group_id='$group_id;
  413.                 break;
  414.             case 'RECURSE_INCL':
  415.                 // inclusive resurse
  416.                 $query .= '
  417.                 LEFT JOIN     '$table .' g2 ON g1.lft >= g2.lft AND g1.lft <= g2.rgt
  418.                 WHERE        g1.group_id='$group_id;
  419.                 break;
  420.             default:
  421.                 $query .= '
  422.                 WHERE        g1.parent_id='$group_id;
  423.         }
  424.  
  425.         $query .= '
  426.                 ORDER BY    g2.lft';
  427.         
  428.  
  429.         $this->db->setQuery$query );
  430.         return $this->db->loadResultArray();
  431.     }
  432.  
  433.     /*======================================================================*\
  434.         Function:    get_root_group_id ()
  435.         Purpose:    Grabs the id of the root group for the specified tree
  436.     \*======================================================================*/
  437.  
  438.     /*======================================================================*\
  439.         Function:    map_path_to_root()
  440.         Purpose:    Maps a unique path to root to a specific group. Each group can only have
  441.                         one path to root.
  442.     \*======================================================================*/
  443.         \*======================================================================*/
  444.     
  445.         \*======================================================================*/
  446.     
  447.         \*======================================================================*/
  448.     
  449.         \*======================================================================*/
  450.     
  451.     function add_group($name$parent_id=0$group_type='ARO'{
  452.         
  453.         switch(strtolower(trim($group_type))) {
  454.             case 'axo':
  455.                 $group_type 'axo';
  456.                 $table $this->_db_table_prefix .'axo_groups';
  457.                 break;
  458.             default:
  459.                 $group_type 'aro';
  460.                 $table $this->_db_table_prefix .'aro_groups';
  461.                 break;
  462.         }
  463.  
  464.         $this->debug_text("add_group(): Name: $name Parent ID: $parent_id Group Type: $group_type");
  465.  
  466.         $name trim($name);
  467.         
  468.         if (empty($name)) {
  469.             $this->debug_text("add_group(): name ($name) OR parent id ($parent_id) is empty, this is required");
  470.             return false;
  471.         }
  472.  
  473.         //This has to be outside the transaction, because the first time it is run, it will say the sequence
  474.         //doesn't exist. Then try to create it, but the transaction will already by aborted by then.
  475.         //$insert_id = $this->db->GenID($this->_db_table_prefix.$group_type.'_groups_id_seq',10);
  476.         $this->db->setQuery"SELECT MAX(group_id)+1 FROM $table);
  477.         $insert_id intval$this->db->loadResult() );
  478.         
  479.         // <mos> $this->db->BeginTrans();
  480.         
  481.         // special case for root group
  482.         if ($parent_id == 0{
  483.             // check a root group is not already defined
  484.             $this->db->setQuery'SELECT group_id FROM '$table .' WHERE parent_id=0' );
  485.             $rs $this->db->loadResultArray();
  486.             
  487.             if (!is_array$rs )) {
  488.                 $this->debug_db('add_group');
  489.                 $this->db->RollBackTrans();
  490.                 return FALSE;
  491.             }
  492.             
  493.             if (count$rs 0{
  494.                 $this->debug_text('add_group (): A root group already exists.');
  495.                 // <mos> $this->db->RollBackTrans();
  496.                 return FALSE;
  497.             }
  498.             
  499.             $parent_lft 0;
  500.             $parent_rgt 1;
  501.         else {
  502.             if (empty($parent_id)) {
  503.                 $this->debug_text("add_group (): parent id ($parent_id) is empty, this is required");
  504.                 return FALSE;
  505.             }
  506.             
  507.             // grab parent details from database
  508.             $this->db->setQuery'SELECT group_id, lft, rgt FROM '$table .' WHERE group_id='$parent_id );
  509.             $rows $this->db->loadRowList();
  510.             
  511.             if (!is_array($rowsOR $this->db->getErrorNum(0{
  512.                 $this->debug_db('add_group');
  513.                 // <mos> $this->db->RollBackTrans();
  514.                 return FALSE;
  515.             }
  516.             
  517.             if (empty($rows)) {
  518.                 $this->debug_text('add_group (): Parent ID: '$parent_id .' not found.');
  519.                 // <mos> $this->db->RollBackTrans();
  520.                 return FALSE;
  521.             }
  522.             $row $rows[0];
  523.             $parent_lft &$row[1];
  524.             $parent_rgt &$row[2];
  525.  
  526.             // make room for the new group
  527.             $this->db->setQuery'UPDATE '$table .' SET rgt=rgt+2 WHERE rgt>='$parent_rgt );
  528.             $rs $this->db->query();
  529.             
  530.             if (!$rs{
  531.                 $this->debug_db('add_group: make room for the new group - right');
  532.                 // <mos> $this->db->RollBackTrans();
  533.                 return FALSE;
  534.             }
  535.             
  536.             $this->db->setQuery'UPDATE '$table .' SET lft=lft+2 WHERE lft>'$parent_rgt );
  537.             $rs $this->db->query();
  538.             
  539.             if (!$rs{
  540.                 $this->debug_db('add_group: make room for the new group - left');
  541.                 // <mos> $this->db->RollBackTrans();
  542.                 return FALSE;
  543.             }
  544.         }
  545.         
  546.         $this->db->setQuery'INSERT INTO '$table .' (group_id,parent_id,name,lft,rgt) VALUES ('$insert_id .','$parent_id .',\''$this->db->getEscaped($name.'\','$parent_rgt .','($parent_rgt 1.')' );
  547.         $rs $this->db->query();
  548.         
  549.         if (!$rs{
  550.             $this->debug_db('add_group: insert record');
  551.             // <mos> $this->db->RollBackTrans();
  552.             return FALSE;
  553.         }
  554.         
  555.         // <mos> $this->db->CommitTrans();
  556.         
  557.         $this->debug_text('add_group (): Added group as ID: '$insert_id);
  558.         return $insert_id;
  559.     }
  560.     
  561.     /*======================================================================*\
  562.         Function:    get_group_objects()
  563.         Purpose:    Gets all objects assigned to a group.
  564.                         If $option == 'RECURSE' it will get all objects in child groups as well.
  565.                         defaults to omit child groups.
  566.     \*======================================================================*/
  567.     function get_group_objects($group_id$group_type='ARO'$option='NO_RECURSE'{
  568.  
  569.         switch(strtolower(trim($group_type))) {
  570.             case 'axo':
  571.                 $group_type 'axo';
  572.                 $object_table $this->_db_table_prefix .'axo';
  573.                 $group_table $this->_db_table_prefix .'axo_groups';
  574.                 $map_table $this->_db_table_prefix .'groups_axo_map';
  575.                 break;
  576.             default:
  577.                 $group_type 'aro';
  578.                 $object_table $this->_db_table_prefix .'aro';
  579.                 $group_table $this->_db_table_prefix .'aro_groups';
  580.                 $map_table $this->_db_table_prefix .'groups_aro_map';
  581.                 break;
  582.         }
  583.  
  584.         $this->debug_text("get_group_objects(): Group ID: $group_id");
  585.  
  586.         if (empty($group_id)) {
  587.             $this->debug_text("get_group_objects(): Group ID:  ($group_id) is empty, this is required");
  588.             return false;
  589.         }
  590.  
  591.         $query '
  592.                 SELECT        o.section_value,o.value
  593.                 FROM        '$object_table .' o
  594.                 LEFT JOIN    '$map_table .' gm ON o.'$group_type .'_id=gm.'$group_type .'_id';
  595.  
  596.         if ($option == 'RECURSE'{
  597.             $query .= '
  598.                 LEFT JOIN    '$group_table .' g1 ON g1.group_id=gm.group_id
  599.                 LEFT JOIN    '$group_table .' g2 ON g2.lft<=g1.lft AND g2.rgt>=g1.rgt
  600.                 WHERE        g2.group_id='$group_id;
  601.         else {
  602.             $query .= '
  603.                 WHERE        gm.group_id='$group_id;
  604.         }
  605.  
  606.         $this->db->setQuery$query );
  607.  
  608.         $rs $this->db->loadRowList();
  609.  
  610.         if (!is_array$rs )) {
  611.             $this->debug_db('get_group_objects');
  612.             return false;
  613.         }
  614.  
  615.         $this->debug_text("get_group_objects(): Got group objects, formatting array.");
  616.  
  617.         $retarr array();
  618.  
  619.         //format return array.
  620.         foreach ($rs as $row{
  621.             $section &$row[0];
  622.             $value &$row[1];
  623.  
  624.             $retarr[$section][$value;
  625.         }
  626.  
  627.         return $retarr;
  628.     }
  629.  
  630.     /*======================================================================*\
  631.         Function:    add_group_object()
  632.         Purpose:    Assigns an Object to a group
  633.     \*======================================================================*/
  634.     function add_group_object($group_id$object_section_value$object_value$group_type='ARO'{
  635.  
  636.         switch(strtolower(trim($group_type))) {
  637.             case 'axo':
  638.                 $group_type 'axo';
  639.                 $table $this->_db_table_prefix .'groups_axo_map';
  640.                 $object_table $this->_db_table_prefix .'axo';
  641.                 $group_table $this->_db_table_prefix .'axo_groups';
  642.                 break;
  643.             default:
  644.                 $group_type 'aro';
  645.                 $table $this->_db_table_prefix .'groups_aro_map';
  646.                 $object_table $this->_db_table_prefix .'aro';
  647.                 $group_table $this->_db_table_prefix .'aro_groups';
  648.                 break;
  649.         }
  650.  
  651.         $this->debug_text("add_group_object(): Group ID: $group_id, Section Value: $object_section_value, Value: $object_value, Group Type: $group_type");
  652.  
  653.         $object_section_value trim($object_section_value);
  654.         $object_value trim($object_value);
  655.  
  656.         if (empty($group_idOR empty($object_valueOR empty($object_section_value)) {
  657.             $this->debug_text("add_group_object(): Group ID:  ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is empty, this is required");
  658.             return false;
  659.         }
  660.  
  661.         // test to see if object & group exist and if object is already a member
  662.         $this->db->setQuery'
  663.             SELECT        g.group_id,o.'$group_type .'_id,gm.group_id AS member
  664.             FROM        '$object_table .' o
  665.             LEFT JOIN    '$group_table .' g ON g.group_id='$group_id .'
  666.             LEFT JOIN    '$table .' gm ON (gm.group_id=g.group_id AND gm.'$group_type .'_id=o.'$group_type .'_id)
  667.             WHERE        (o.section_value=\''$this->db->getEscaped($object_section_value.'\' AND o.value=\''$this->db->getEscaped($object_value.'\')'
  668.         );
  669.  
  670.         $rows $this->db->loadRowList();
  671.         if ($this->db->getErrorNum()) {
  672.             $this->debug_db('add_group_object');
  673.             return FALSE;
  674.         }
  675.  
  676.         if (count$rows != 1{
  677.             $this->debug_text("add_group_object (): Group ID ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is invalid. Does this object exist?");
  678.             return FALSE;
  679.         }
  680.  
  681.         $row $rows[0];
  682.  
  683.         if ($row[2== 1{
  684.             $this->debug_text("add_group_object (): Object: $object_value is already a member of Group ID: $group_id");
  685.             //Object is already assigned to group. Return true.
  686.             return true;
  687.         }
  688.  
  689.         $object_id $row[1];
  690.  
  691.         $this->db->setQuery'INSERT INTO '$table .' (group_id,'$group_type .'_id) VALUES ('$group_id .','$object_id .')' );
  692.  
  693.         if (!$this->db->query()) {
  694.             $this->debug_db('add_group_object');
  695.             return FALSE;
  696.         }
  697.  
  698.         $this->debug_text('add_group_object(): Added Object: '$object_id .' to Group ID: '$group_id);
  699.  
  700.         if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE{
  701.             //Expire all cache.
  702.             $this->Cache_Lite->clean('default');
  703.         }
  704.  
  705.         return TRUE;
  706.     }
  707.  
  708.     /*======================================================================*\
  709.         Function:    del_group_object()
  710.         Purpose:    Removes an Object from a group.
  711.     \*======================================================================*/
  712.     function del_group_object($group_id$object_section_value$object_value$group_type='ARO'{
  713.  
  714.         switch(strtolower(trim($group_type))) {
  715.             case 'axo':
  716.                 $group_type 'axo';
  717.                 $table $this->_db_table_prefix .'groups_axo_map';
  718.                 break;
  719.             default:
  720.                 $group_type 'aro';
  721.                 $table $this->_db_table_prefix .'groups_aro_map';
  722.                 break;
  723.         }
  724.  
  725.         $this->debug_text("del_group_object(): Group ID: $group_id Section value: $object_section_value Value: $object_value");
  726.  
  727.         $object_section_value trim($object_section_value);
  728.         $object_value trim($object_value);
  729.  
  730.         if (empty($group_idOR empty($object_valueOR empty($object_section_value)) {
  731.             $this->debug_text("del_group_object(): Group ID:  ($group_id) OR Section value: $object_section_value OR Value ($object_value) is empty, this is required");
  732.             return false;
  733.         }
  734.  
  735.          if (!$object_id $this->get_object_id($object_section_value$object_value$group_type)) {
  736.             $this->debug_text ("del_group_object (): Group ID ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is invalid. Does this object exist?");
  737.             return FALSE;
  738.         }
  739.  
  740.         $this->db->setQuery'DELETE FROM '$table .' WHERE group_id='$group_id .' AND '$group_type .'_id='$object_id );
  741.         $this->db->query();
  742.  
  743.         if ($this->db->getErrorNum()) {
  744.             $this->debug_db('del_group_object');
  745.             return false;
  746.         }
  747.  
  748.         $this->debug_text("del_group_object(): Deleted Value: $object_value to Group ID: $group_id assignment");
  749.  
  750.         if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE{
  751.             //Expire all cache.
  752.             $this->Cache_Lite->clean('default');
  753.         }
  754.  
  755.         return true;
  756.     }
  757.  
  758.     /*======================================================================*\
  759.         Function:    edit_group()
  760.         Purpose:    Edits a group
  761.     \*======================================================================*/
  762.     
  763.     /*======================================================================*\
  764.         Function:    rebuild_tree ()
  765.         Purpose:    rebuilds the group tree for the given type
  766.     \*======================================================================*/
  767.     
  768.     /*======================================================================*\
  769.         Function:    del_group()
  770.         Purpose:    deletes a given group
  771.     \*======================================================================*/
  772.     function del_group($group_id$reparent_children=TRUE$group_type='ARO'{
  773.         
  774.         switch(strtolower(trim($group_type))) {
  775.             case 'axo':
  776.                 $group_type 'axo';
  777.                 $table $this->_db_table_prefix .'axo_groups';
  778.                 $groups_map_table $this->_db_table_prefix .'axo_groups_map';
  779.                 $groups_object_map_table $this->_db_table_prefix .'groups_axo_map';
  780.                 break;
  781.             default:
  782.                 $group_type 'aro';
  783.                 $table $this->_db_table_prefix .'aro_groups';
  784.                 $groups_map_table $this->_db_table_prefix .'aro_groups_map';
  785.                 $groups_object_map_table $this->_db_table_prefix .'groups_aro_map';
  786.                 break;
  787.         }
  788.  
  789.         $this->debug_text("del_group(): ID: $group_id Reparent Children: $reparent_children Group Type: $group_type");
  790.         
  791.         if (empty($group_id) ) {
  792.             $this->debug_text("del_group(): Group ID ($group_id) is empty, this is required");
  793.             return false;
  794.         }
  795.  
  796.         // Get details of this group
  797.         $this->db->setQuery'SELECT group_id, parent_id, name, lft, rgt FROM '$table .' WHERE group_id='$group_id );
  798.         $group_details $this->db->loadRow($query);
  799.         
  800.         if (!is_array($group_details)) {
  801.             $this->debug_db('del_group: get group details');
  802.             return false;
  803.         }
  804.         
  805.         $parent_id $group_details[1];
  806.         
  807.         $left $group_details[3];
  808.         $right $group_details[4];
  809.         
  810.         // <mos> $this->db->BeginTrans();
  811.  
  812.         // grab list of all children
  813.         $children_ids $this->get_group_children($group_id$group_type'RECURSE');
  814.         
  815.         // prevent deletion of root group & reparent of children if it has more than one immediate child
  816.         if ($parent_id == 0{
  817.             $this->db->setQuery'SELECT count(*) FROM '$table .' WHERE parent_id='$group_id );
  818.             $child_count $this->db->loadResult($query);
  819.             
  820.             if ($child_count && $reparent_children{
  821.                 $this->debug_text ('del_group (): You cannot delete the root group and reparent children, this would create multiple root groups.');
  822.                 return FALSE;
  823.             }
  824.         }
  825.         
  826.         $success FALSE;
  827.         
  828.         /*
  829.          * Handle children here.
  830.          */
  831.         switch (TRUE{
  832.             // there are no child groups, just delete group
  833.             case !is_array($children_ids):
  834.             case count($children_ids== 0:
  835.                 // remove acl maps
  836.             /* Reapply when ACL's implemented
  837.                 $this->db->setQuery( 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id );
  838.                 $rs = $this->db->Execute($query);
  839.                 
  840.                 if (!is_object($rs)) {
  841.                     break;
  842.                 }*/
  843.                 
  844.                 // remove group object maps
  845.                 $this->db->setQuery'DELETE FROM '$groups_object_map_table .' WHERE group_id='$group_id );
  846.                 $rs $this->db->query();
  847.                 
  848.                 if (!$rs{
  849.                     break;
  850.                 }
  851.                 
  852.                 // remove group
  853.                 $this->db->setQuery'DELETE FROM '$table .' WHERE group_id='$group_id );
  854.                 $rs $this->db->query();
  855.  
  856.                 if (!$rs{
  857.                     break;
  858.                 }
  859.                 
  860.                 // move all groups right of deleted group left by width of deleted group
  861.                 $this->db->setQuery'UPDATE '$table .' SET lft=lft-'($right-$left+1.' WHERE lft>'$right );
  862.                 $rs $this->db->query();
  863.                 
  864.                 if (!$rs{
  865.                     break;
  866.                 }
  867.                 
  868.                 $this->db->setQuery'UPDATE '$table .' SET rgt=rgt-'($right-$left+1.' WHERE rgt>'$right );
  869.                 $rs $this->db->query();
  870.                 
  871.                 if (!$rs{
  872.                     break;
  873.                 }
  874.                 
  875.                 $success TRUE;
  876.                 break;
  877.             case $reparent_children == TRUE:
  878.                 // remove acl maps
  879.             /* Reapply when ACL's implemented
  880.                 $query = 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id;
  881.                 $rs = $this->db->Execute($query);
  882.                 
  883.                 if (!is_object($rs)) {
  884.                     break;
  885.                 }*/
  886.                 
  887.                 // remove group object maps
  888.                 $this->db->setQuery'DELETE FROM '$groups_object_map_table .' WHERE group_id='$group_id );
  889.                 $rs $this->db->query();
  890.                 
  891.                 if (!$rs{
  892.                     break;
  893.                 }
  894.                 
  895.                 // remove group
  896.                 $this->db->setQuery'DELETE FROM '$table .' WHERE group_id='$group_id );
  897.                 $rs $this->db->query();
  898.                 
  899.                 if (!$rs{
  900.                     break;
  901.                 }
  902.                 
  903.                 // set parent of immediate children to parent group
  904.                 $this->db->setQuery'UPDATE '$table .' SET parent_id='$parent_id .' WHERE parent_id='$group_id );
  905.                 $rs $this->db->query();
  906.                 
  907.                 if (!$rs{
  908.                     break;
  909.                 }
  910.                 
  911.                 // move all children left by 1
  912.                 $this->db->setQuery'UPDATE '$table .' SET lft=lft-1, rgt=rgt-1 WHERE lft>'$left .' AND rgt<'$right );
  913.                 $rs $this->db->query();
  914.                 
  915.                 if (!$rs{
  916.                     break;
  917.                 }
  918.                 
  919.                 // move all groups right of deleted group left by 2
  920.                 $this->db->setQuery'UPDATE '$table .' SET lft=lft-2 WHERE lft>'$right );
  921.                 $rs $this->db->query();
  922.                 
  923.                 if (!$rs{
  924.                     break;
  925.                 }
  926.                 
  927.                 $this->db->setQuery'UPDATE '$table .' SET rgt=rgt-2 WHERE rgt>'$right );
  928.                 $rs $this->db->query();
  929.                 
  930.                 if (!$rs{
  931.                     break;
  932.                 }
  933.                 
  934.                 $success TRUE;
  935.                 break;
  936.             default:
  937.                 // make list of group and all children
  938.                 $group_ids $children_ids;
  939.                 $group_ids[$group_id;
  940.                 
  941.                 // remove acl maps
  942.             /* Reapply when ACL's implemented
  943.                 $query = 'DELETE FROM '. $groups_map_table .' WHERE group_id IN ('. implode (',', $group_ids) .')';
  944.                 $rs = $this->db->Execute($query);
  945.                 
  946.                 if (!is_object($rs)) {
  947.                     break;
  948.                 }*/
  949.                 
  950.                 // remove group object maps
  951.                 $this->db->setQuery'DELETE FROM '$groups_object_map_table .' WHERE group_id IN ('implode (','$group_ids.')' );
  952.                 $rs $this->db->query();
  953.                 
  954.                 if (!$rs{
  955.                     break;
  956.                 }
  957.                 
  958.                 // remove groups
  959.                 $this->db->setQuery'DELETE FROM '$table .' WHERE group_id IN ('implode (','$group_ids.')' );
  960.                 $rs $this->db->query();
  961.                 
  962.                 if (!$rs{
  963.                     break;
  964.                 }
  965.                 
  966.                 // move all groups right of deleted group left by width of deleted group
  967.                 $this->db->setQuery'UPDATE '$table .' SET lft=lft-'($right $left 1.' WHERE lft>'$right );
  968.                 $rs $this->db->query();
  969.                 
  970.                 if (!$rs{
  971.                     break;
  972.                 }
  973.                 
  974.                 $this->db->setQuery'UPDATE '$table .' SET rgt=rgt-'($right $left 1.' WHERE rgt>'$right );
  975.                 $rs $this->db->query();
  976.                 
  977.                 if (!$rs{
  978.                     break;
  979.                 }
  980.                 
  981.                 $success TRUE;
  982.         }
  983.         
  984.         // if the delete failed, rollback the trans and return false
  985.         if (!$success{
  986.  
  987.             $this->debug_db('del_group');
  988.             $this->db->RollBackTrans();
  989.             return false;
  990.         }
  991.         
  992.         $this->debug_text("del_group(): deleted group ID: $group_id");
  993.         // <mos> $this->db->CommitTrans();
  994.  
  995.         if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE{
  996.             //Expire all cache.    
  997.             $this->Cache_Lite->clean('default');
  998.         }
  999.  
  1000.         return true;
  1001.  
  1002.     }
  1003.  
  1004.  
  1005.     /*
  1006.      *
  1007.      * Objects (ACO/ARO/AXO)
  1008.      *
  1009.      */
  1010.     /*======================================================================*\
  1011.         Function:    get_object()
  1012.         Purpose:    Grabs all Objects's in the database, or specific to a section_value
  1013.     \*======================================================================*/
  1014.     function get_object($section_value null$return_hidden=1$object_type=NULL{
  1015.  
  1016.         switch(strtolower(trim($object_type))) {
  1017.             case 'aco':
  1018.                 $object_type 'aco';
  1019.                 $table $this->_db_table_prefix .'aco';
  1020.                 break;
  1021.             case 'aro':
  1022.                 $object_type 'aro';
  1023.                 $table $this->_db_table_prefix .'aro';
  1024.                 break;
  1025.             case 'axo':
  1026.                 $object_type 'axo';
  1027.                 $table $this->_db_table_prefix .'axo';
  1028.                 break;
  1029.             default:
  1030.                 $this->debug_text('get_object(): Invalid Object Type: '$object_type);
  1031.                 return FALSE;
  1032.         }
  1033.  
  1034.         $this->debug_text("get_object(): Section Value: $section_value Object Type: $object_type");
  1035.  
  1036.         $$this->db->setQuery'SELECT '$object_type .'_id FROM '$table );
  1037.  
  1038.         $where array();
  1039.  
  1040.         if (!empty($section_value)) {
  1041.             $where['section_value='$this->db->getEscaped($section_value);
  1042.         }
  1043.  
  1044.         if ($return_hidden==0{
  1045.             $where['hidden=0';
  1046.         }
  1047.  
  1048.         if (!empty($where)) {
  1049.             $query .= ' WHERE 'implode(' AND '$where);
  1050.         }
  1051.  
  1052.         $rs $this->db->loadResultArray();
  1053.  
  1054.         if (!is_array($rs)) {
  1055.             $this->debug_db('get_object');
  1056.             return false;
  1057.         }
  1058.  
  1059.         // Return Object IDs
  1060.         return $rs;
  1061.     }
  1062.  
  1063.     /*======================================================================*\
  1064.         Function:    get_objects ()
  1065.         Purpose:    Grabs all Objects in the database, or specific to a section_value
  1066.                     returns format suitable for add_acl and is_conflicting_acl
  1067.     \*======================================================================*/
  1068.  
  1069.     /*======================================================================*\
  1070.         Function:    get_object_data()
  1071.         Purpose:    Gets all data pertaining to a specific Object.
  1072.     \*======================================================================*/
  1073.  
  1074.     /*======================================================================*\
  1075.         Function:    get_object_groups()
  1076.         Purpose:    Gets the group_id's for the given the section_value AND value
  1077.         of the object.
  1078.     \*======================================================================*/
  1079.     function get_object_groups($object_section_value$object_value$object_type=NULL{
  1080.  
  1081.         switch(strtolower(trim($object_type))) {
  1082.             case 'aro':
  1083.                 $group_type 'aro';
  1084.                 $table $this->_db_table_prefix .'groups_aro_map';
  1085.                 $object_table $this->_db_table_prefix .'aro';
  1086.                 $group_table $this->_db_table_prefix .'aro_groups';
  1087.                 break;
  1088.             case 'axo':
  1089.                 $group_type 'axo';
  1090.                 $table $this->_db_table_prefix .'groups_axo_map';
  1091.                 $object_table $this->_db_table_prefix .'axo';
  1092.                 $group_table $this->_db_table_prefix .'axo_groups';
  1093.                 break;
  1094.             default:
  1095.                 $this->debug_text('get_object_groups(): Invalid Object Type: '$object_type);
  1096.                 return FALSE;
  1097.         }
  1098.  
  1099.         $this->debug_text("get_object_groups(): Section Value: $object_section_value Value: $object_value Object Type: $object_type");
  1100.  
  1101.         $object_section_value trim($object_section_value);
  1102.         $object_value trim($object_value);
  1103.  
  1104.         if (empty($object_section_valueAND empty($object_value) ) {
  1105.             $this->debug_text("get_object_groups(): Section Value ($object_section_value) AND value ($object_value) is empty, this is required");
  1106.             return false;
  1107.         }
  1108.  
  1109.         if (empty($object_type) ) {
  1110.             $this->debug_text("get_object_groups(): Object Type ($object_type) is empty, this is required");
  1111.             return false;
  1112.         }
  1113. //            SELECT        g.group_id,o.'. $group_type .'_id,(gm.group_id IS NOT NULL) AS member
  1114.  
  1115.         $this->db->setQuery'
  1116.             SELECT        g.group_id,o.'$group_type .'_id,(gm.group_id IS NOT NULL) AS member
  1117.             FROM        '$group_table .' g
  1118.             LEFT JOIN    '$table .' gm ON gm.group_id=g.group_id
  1119.             LEFT JOIN    '$object_table .' o ON o.'$group_type .'_id = gm.'$group_type .'_id
  1120.             WHERE        (o.section_value=\''$this->db->getEscaped($object_section_value.'\' AND o.value=\''$this->db->getEscaped($object_value.'\')'
  1121.         );
  1122.         $rs $this->db->loadResultArray();
  1123.  
  1124.         if ($this->db->getErrorNum()) {
  1125.             $this->debug_db('get_object_id');
  1126.             return false;
  1127.         }
  1128.  
  1129.         //Return the array of group id's
  1130.         return $rs;
  1131.     }
  1132.  
  1133.     /*======================================================================*\
  1134.         Function:    get_object_id()
  1135.         Purpose:    Gets the object_id given the section_value AND value of the object.
  1136.     \*======================================================================*/
  1137.     function get_object_id($section_value$value$object_type=NULL{
  1138.  
  1139.         switch(strtolower(trim($object_type))) {
  1140.             case 'aco':
  1141.                 $object_type 'aco';
  1142.                 $table $this->_db_table_prefix .'aco';
  1143.                 break;
  1144.             case 'aro':
  1145.                 $object_type 'aro';
  1146.                 $table $this->_db_table_prefix .'aro';
  1147.                 break;
  1148.             case 'axo':
  1149.                 $object_type 'axo';
  1150.                 $table $this->_db_table_prefix .'axo';
  1151.                 break;
  1152.             default:
  1153.                 $this->debug_text('get_object_id(): Invalid Object Type: '$object_type);
  1154.                 return FALSE;
  1155.         }
  1156.  
  1157.         $this->debug_text("get_object_id(): Section Value: $section_value Value: $value Object Type: $object_type");
  1158.  
  1159.         $section_value trim($section_value);
  1160.         $value trim($value);
  1161.  
  1162.         if (empty($section_valueAND empty($value) ) {
  1163.             $this->debug_text("get_object_id(): Section Value ($value) AND value ($value) is empty, this is required");
  1164.             return false;
  1165.         }
  1166.  
  1167.         if (empty($object_type) ) {
  1168.             $this->debug_text("get_object_id(): Object Type ($object_type) is empty, this is required");
  1169.             return false;
  1170.         }
  1171.  
  1172.         $this->db->setQuery'SELECT '$object_type .'_id FROM '$table .' WHERE section_value=\''$this->db->getEscaped($section_value.'\' AND value=\''$this->db->getEscaped($value.'\''
  1173.         );
  1174.         $rs $this->db->loadRowList();
  1175.  
  1176.         if ($this->db->getErrorNum()) {
  1177.             $this->debug_db('get_object_id');
  1178.             return false;
  1179.         }
  1180.  
  1181.         $row_count count$rs );
  1182.  
  1183.         if ($row_count 1{
  1184.             $this->debug_text("get_object_id(): Returned $row_count rows, can only return one. This should never happen, the database may be missing a unique key.");
  1185.             return false;
  1186.         }
  1187.  
  1188.         if ($row_count == 0{
  1189.             $this->debug_text("get_object_id(): Returned $row_count rows");
  1190.             return false;
  1191.         }
  1192.  
  1193.         $row $rs[0];
  1194.  
  1195.         //Return the ID.
  1196.         return $row[0];
  1197.     }
  1198.  
  1199.     /*======================================================================*\
  1200.         Function:    get_object_section_value()
  1201.         Purpose:    Gets the object_section_value given object id
  1202.     \*======================================================================*/
  1203.  
  1204.     /*======================================================================*\
  1205.         Function:    get_object_groups()
  1206.         Purpose:    Gets all groups an object is a member of.
  1207.                     If $option == 'RECURSE' it will get all ancestor groups.
  1208.                     defaults to only get direct parents.
  1209.     \*======================================================================*/
  1210.  
  1211.     /*======================================================================*\
  1212.         Function:    add_object()
  1213.         Purpose:    Inserts a new object
  1214.     \*======================================================================*/
  1215.     function add_object($section_value$name$value=0$order=0$hidden=0$object_type=NULL{
  1216.  
  1217.         switch(strtolower(trim($object_type))) {
  1218.             case 'aco':
  1219.                 $object_type 'aco';
  1220.                 $table $this->_db_table_prefix .'aco';
  1221.                 $object_sections_table $this->_db_table_prefix .'aco_sections';
  1222.                 break;
  1223.             case 'aro':
  1224.                 $object_type 'aro';
  1225.                 $table $this->_db_table_prefix .'aro';
  1226.                 $object_sections_table $this->_db_table_prefix .'aro_sections';
  1227.                 break;
  1228.             case 'axo':
  1229.                 $object_type 'axo';
  1230.                 $table $this->_db_table_prefix .'axo';
  1231.                 $object_sections_table $this->_db_table_prefix .'axo_sections';
  1232.                 break;
  1233.             default:
  1234.                 $this->debug_text('add_object(): Invalid Object Type: '$object_type);
  1235.                 return FALSE;
  1236.         }
  1237.  
  1238.         $this->debug_text("add_object(): Section Value: $section_value Value: $value Order: $order Name: $name Object Type: $object_type");
  1239.  
  1240.         $section_value trim($section_value);
  1241.         $name trim($name);
  1242.         $value trim($value);
  1243.         $order trim($order);
  1244.  
  1245.         if ($order == NULL OR $order == ''{
  1246.             $order 0;
  1247.         }
  1248.  
  1249.         if (empty($nameOR empty($section_value) ) {
  1250.             $this->debug_text("add_object(): name ($name) OR section value ($section_value) is empty, this is required");
  1251.             return false;
  1252.         }
  1253.  
  1254.         if (strlen($name>= 255 OR strlen($value>= 230 {
  1255.             $this->debug_text("add_object(): name ($name) OR value ($value) is too long.");
  1256.             return false;
  1257.         }
  1258.  
  1259.         if (empty($object_type) ) {
  1260.             $this->debug_text("add_object(): Object Type ($object_type) is empty, this is required");
  1261.             return false;
  1262.         }
  1263.  
  1264.         // Test to see if the section is invalid or object already exists.
  1265.         $this->db->setQuery'
  1266.             SELECT        (o.'$object_type .'_id IS NOT NULL) AS object_exists
  1267.             FROM        '$object_sections_table .' s
  1268.             LEFT JOIN    '$table .' o ON (s.value=o.section_value AND o.value=\''$this->db->getEscaped($value.'\')
  1269.             WHERE        s.value=\''$this->db->getEscaped($section_value)'\''
  1270.         );
  1271.  
  1272.         $rows $this->db->loadRowList();
  1273.         if ($this->db->getErrorNum()) {
  1274.             $this->debug_db('add_object');
  1275.             return FALSE;
  1276.         }
  1277.  
  1278.         if (count$rows != 1{
  1279.             // Section is invalid
  1280.             $this->debug_text("add_object(): Section Value: $section_value Object Type ($object_type) does not exist, this is required");
  1281.             return false;
  1282.         }
  1283.  
  1284.         $row $rows[0];
  1285.  
  1286.         if ($row[0== 1{
  1287.             //Object is already created.
  1288.             return true;
  1289.         }
  1290.  
  1291.         $insert_id $this->db->GenID($this->_db_table_prefix.$object_type.'_seq',10);
  1292.         $this->db->setQuery"INSERT INTO $table ({$object_type}_id,section_value,value,order_value,name,hidden) VALUES($insert_id,'$section_value','$value','$order','$name','$hidden'));
  1293.  
  1294.         if (!$this->db->query()) {
  1295.             $this->debug_db('add_object');
  1296.             return false;
  1297.         }
  1298.  
  1299.         $insert_id $this->db->insertid();
  1300.         $this->debug_text("add_object(): Added object as ID: $insert_id");
  1301.         return $insert_id;
  1302.     }
  1303.     /*======================================================================*\
  1304.         Function:    edit_object()
  1305.         Purpose:    Edits a given Object
  1306.     \*======================================================================*/
  1307.     function edit_object($object_id$section_value$name$value=0$order=0$hidden=0$object_type=NULL{
  1308.  
  1309.         switch(strtolower(trim($object_type))) {
  1310.             case 'aco':
  1311.                 $object_type 'aco';
  1312.                 $table $this->_db_table_prefix .'aco';
  1313.                 $object_map_table 'aco_map';
  1314.                 break;
  1315.             case 'aro':
  1316.                 $object_type 'aro';
  1317.                 $table $this->_db_table_prefix .'aro';
  1318.                 $object_map_table 'aro_map';
  1319.                 break;
  1320.             case 'axo':
  1321.                 $object_type 'axo';
  1322.                 $table $this->_db_table_prefix .'axo';
  1323.                 $object_map_table 'axo_map';
  1324.                 break;
  1325.         }
  1326.  
  1327.         $this->debug_text("edit_object(): ID: $object_id, Section Value: $section_value, Value: $value, Order: $order, Name: $name, Object Type: $object_type");
  1328.  
  1329.         $section_value trim($section_value);
  1330.         $name trim($name);
  1331.         $value trim($value);
  1332.         $order trim($order);
  1333.  
  1334.         if (empty($object_idOR empty($section_value) ) {
  1335.             $this->debug_text("edit_object(): Object ID ($object_id) OR Section Value ($section_value) is empty, this is required");
  1336.             return false;
  1337.         }
  1338.  
  1339.         if (empty($name) ) {
  1340.             $this->debug_text("edit_object(): name ($name) is empty, this is required");
  1341.             return false;
  1342.         }
  1343.  
  1344.         if (empty($object_type) ) {
  1345.             $this->debug_text("edit_object(): Object Type ($object_type) is empty, this is required");
  1346.             return false;
  1347.         }
  1348.  
  1349.         //Get old value incase it changed, before we do the update.
  1350.         $this->db->setQuery'SELECT value, section_value FROM '$table .' WHERE '$object_type .'_id='$object_id );
  1351.         $old $this->db->loadRow();
  1352.  
  1353.         $this->db->setQuery'
  1354.             UPDATE    '$table .'
  1355.             SET        section_value=\''$this->db->getEscaped($section_value.'\',
  1356.                     value='$this->db->getEscaped($value.',
  1357.                     order_value='$this->db->getEscaped($order.',
  1358.                     name=\''$this->db->getEscaped($name.'\',
  1359.                     hidden='$hidden .'
  1360.             WHERE    '$object_type .'_id='$object_id
  1361.         );
  1362.         $this->db->query();
  1363.  
  1364.         if (!$this->db->getErrorNum()) {
  1365.             $this->debug_db('edit_object');
  1366.             return false;
  1367.         }
  1368.  
  1369.         $this->debug_text('edit_object(): Modified 'strtoupper($object_type.' ID: '$object_id);
  1370.  
  1371.         if ($old[0!= $value OR $old[1!= $section_value{
  1372.             $this->debug_text("edit_object(): Value OR Section Value Changed, update other tables.");
  1373.  
  1374.             $this->db->setQuery'
  1375.                 UPDATE    '$object_map_table .'
  1376.                 SET        value=\''$this->db->getEscaped($value.'\',
  1377.                         section_value=\''$this->db->getEscaped($section_value.'\'
  1378.                 WHERE    section_value=\''$this->db->getEscaped($old[1].'\'
  1379.                     AND    value='$this->db->getEscaped($old[0])
  1380.             );
  1381.             $this->db->query();
  1382.  
  1383.         if (!$this->db->getErrorNum()) {
  1384.                 $this->debug_db('edit_object');
  1385.                 return FALSE;
  1386.             }
  1387.  
  1388.             $this->debug_text ('edit_object(): Modified Map Value: '$value .' Section Value: '$section_value);
  1389.         }
  1390.  
  1391.         return TRUE;
  1392.     }
  1393.  
  1394.     /*======================================================================*\
  1395.         Function:    del_object()
  1396.         Purpose:    Deletes a given Object and, if instructed to do so,
  1397.                         erase all referencing objects
  1398.                         ERASE feature by: Martino Piccinato
  1399.     \*======================================================================*/
  1400.     function del_object($object_id$object_type=NULL$erase=FALSE{
  1401.  
  1402.         switch(strtolower(trim($object_type))) {
  1403.             case 'aco':
  1404.                 $object_type 'aco';
  1405.                 $table $this->_db_table_prefix .'aco';
  1406.                 $object_map_table $this->_db_table_prefix .'aco_map';
  1407.                 break;
  1408.             case 'aro':
  1409.                 $object_type 'aro';
  1410.                 $table $this->_db_table_prefix .'aro';
  1411.                 $object_map_table $this->_db_table_prefix .'aro_map';
  1412.                 $groups_map_table $this->_db_table_prefix .'aro_groups_map';
  1413.                 $object_group_table $this->_db_table_prefix .'groups_aro_map';
  1414.                 break;
  1415.             case 'axo':
  1416.                 $object_type 'axo';
  1417.                 $table $this->_db_table_prefix .'axo';
  1418.                 $object_map_table $this->_db_table_prefix .'axo_map';
  1419.                 $groups_map_table $this->_db_table_prefix .'axo_groups_map';
  1420.                 $object_group_table $this->_db_table_prefix .'groups_axo_map';
  1421.                 break;
  1422.             default:
  1423.                 $this->debug_text('del_object(): Invalid Object Type: '$object_type);
  1424.                 return FALSE;
  1425.         }
  1426.  
  1427.         $this->debug_text("del_object(): ID: $object_id Object Type: $object_type, Erase all referencing objects: $erase");
  1428.  
  1429.         if (empty($object_id) ) {
  1430.             $this->debug_text("del_object(): Object ID ($object_id) is empty, this is required");
  1431.             return false;
  1432.         }
  1433.  
  1434.         if (empty($object_type) ) {
  1435.             $this->debug_text("del_object(): Object Type ($object_type) is empty, this is required");
  1436.             return false;
  1437.         }
  1438.  
  1439.         // <mos> $this->db->BeginTrans();
  1440.  
  1441.         // Get Object section_value/value (needed to look for referencing objects)
  1442.         $this->db->setQuery'SELECT section_value,value FROM '$table .' WHERE '$object_type .'_id='$object_id );
  1443.         $object $this->db->loadRow();
  1444.  
  1445.         if (empty($object)) {
  1446.             $this->debug_text('del_object(): The specified object ('strtoupper($object_type.' ID: '$object_id .') could not be found.<br />SQL = '.$this->db->stderr());
  1447.             return FALSE;
  1448.         }
  1449.  
  1450.         $section_value $object[0];
  1451.         $value $object[1];
  1452.  
  1453.         // Get ids of acl referencing the Object (if any)
  1454.         $this->db->setQuery"SELECT acl_id FROM $object_map_table WHERE value='$value' AND section_value='$section_value');
  1455.         $acl_ids $this->db->loadResultArray();
  1456.  
  1457.         if ($erase{
  1458.             // We were asked to erase all acl referencing it
  1459.  
  1460.             $this->debug_text("del_object(): Erase was set to TRUE, delete all referencing objects");
  1461.  
  1462.             if ($object_type == "aro" OR $object_type == "axo"{
  1463.                 // The object can be referenced in groups_X_map tables
  1464.                 // in the future this branching may become useless because
  1465.                 // ACO might me "groupable" too
  1466.  
  1467.                 // Get rid of groups_map referencing the Object
  1468.                 $this->db->setQuery'DELETE FROM '$object_group_table .' WHERE '$object_type .'_id='$object_id );
  1469.                 $rs $this->db->query();
  1470.  
  1471.                 if (!$rs{
  1472.                     $this->debug_db('edit_object');
  1473.                     // <mos> $this->db->RollBackTrans();
  1474.                     return false;
  1475.                 }
  1476.             }
  1477.  
  1478.             if ($acl_ids{
  1479.                 //There are acls actually referencing the object
  1480.  
  1481.                 if ($object_type == 'aco'{
  1482.                     // I know it's extremely dangerous but
  1483.                     // if asked to really erase an ACO
  1484.                     // we should delete all acl referencing it
  1485.                     // (and relative maps)
  1486.  
  1487.                     // Do this below this branching
  1488.                     // where it uses $orphan_acl_ids as
  1489.                     // the array of the "orphaned" acl
  1490.                     // in this case all referenced acl are
  1491.                     // orhpaned acl
  1492.  
  1493.                     $orphan_acl_ids $acl_ids;
  1494.                 else {
  1495.                     // The object is not an ACO and might be referenced
  1496.                     // in still valid acls regarding also other object.
  1497.                     // In these cases the acl MUST NOT be deleted
  1498.  
  1499.                     // Get rid of $object_id map referencing erased objects
  1500.                     $this->db->setQuery"DELETE FROM $object_map_table WHERE section_value='$section_value' AND value='$value');
  1501.                     $rs $this->db->query();
  1502.  
  1503.                     if (!$rs{
  1504.                         $this->debug_db('edit_object');
  1505.                         $this->db->RollBackTrans();
  1506.                         return false;
  1507.                     }
  1508.  
  1509.                     // Find the "orphaned" acl. I mean acl referencing the erased Object (map)
  1510.                     // not referenced anymore by other objects
  1511.  
  1512.                     $sql_acl_ids implode(","$acl_ids);
  1513.  
  1514.                     $this->db->setQuery'
  1515.                         SELECT        a.id
  1516.                         FROM        '$this->_db_table_prefix .'acl a
  1517.                         LEFT JOIN    '$object_map_table .' b ON a.id=b.acl_id
  1518.                         './* <mos return for full acl stuff> LEFT JOIN    '. $groups_map_table .' c ON a.id=c.acl_id*/'
  1519.                         WHERE        value IS NULL
  1520.                             AND        section_value IS NULL
  1521.                             AND        group_id IS NULL
  1522.                             AND        a.id in ('$sql_acl_ids .')');
  1523.                     $orphan_acl_ids $this->db->loadResultArray();
  1524.  
  1525.                 // End of else section of "if ($object_type == "aco")"
  1526.  
  1527.                 if ($orphan_acl_ids{
  1528.                 // If there are orphaned acls get rid of them
  1529.  
  1530.                     foreach ($orphan_acl_ids as $acl{
  1531.                         $this->del_acl($acl);
  1532.                     }
  1533.                 }
  1534.  
  1535.             // End of if ($acl_ids)
  1536.  
  1537.             // Finally delete the Object itself
  1538.             $this->db->setQuery"DELETE FROM $table WHERE {$object_type}_id='$object_id');
  1539.             $rs $this->db->query();
  1540.  
  1541.             if (!$rs{
  1542.                 $this->debug_db('edit_object');
  1543.                 // <mos> $this->db->RollBackTrans();
  1544.                 return false;
  1545.             }
  1546.  
  1547.             // <mos> $this->db->CommitTrans();
  1548.             return true;
  1549.  
  1550.         // End of "if ($erase)"
  1551.  
  1552.         $groups_ids FALSE;
  1553.  
  1554.         if ($object_type == 'axo' OR $object_type == 'aro'{
  1555.             // If the object is "groupable" (may become unnecessary,
  1556.             // see above
  1557.  
  1558.             // Get id of groups where the object is assigned:
  1559.             // you must explicitly remove the object from its groups before
  1560.             // deleting it (don't know if this is really needed, anyway it's safer ;-)
  1561.  
  1562.             $this->db->setQuery'SELECT group_id FROM '$object_group_table .' WHERE '$object_type .'_id='$object_id );
  1563.             $groups_ids $this->db->loadResultArray();
  1564.         }
  1565.  
  1566.         if ( ( isset($acl_idsAND $acl_ids !== FALSE OR isset($groups_idsAND $groups_ids !== FALSE) ) {
  1567.             // The Object is referenced somewhere (group or acl), can't delete it
  1568.  
  1569.             $this->debug_text("del_object(): Can't delete the object as it is being referenced by GROUPs (".@implode($group_ids).") or ACLs (".@implode($acl_ids,",").")");
  1570.  
  1571.             return false;
  1572.         else {
  1573.             // The Object is NOT referenced anywhere, delete it
  1574.  
  1575.             $this->db->setQuery"DELETE FROM $table WHERE {$object_type}_id='$object_id');
  1576.             $this->db->query();
  1577.  
  1578.             if $this->db->getErrorNum() ) {
  1579.                 $this->debug_db('edit_object');
  1580.                 // <mos> $this->db->RollBackTrans();
  1581.                 return false;
  1582.             }
  1583.  
  1584.             // <mos> $this->db->CommitTrans();
  1585.             return true;
  1586.         }
  1587.  
  1588.         return false;
  1589.     }
  1590.  
  1591.     /*
  1592.      *
  1593.      * Object Sections
  1594.      *
  1595.      */
  1596.  
  1597.     /*======================================================================*\
  1598.         Function:    get_object_section_section_id()
  1599.         Purpose:    Gets the object_section_id given the name AND/OR value of the section.
  1600.                     Will only return one section id, so if there are duplicate names it will return false.
  1601.     \*======================================================================*/
  1602.  
  1603.     /*======================================================================*\
  1604.         Function:    add_object_section()
  1605.         Purpose:    Inserts an object Section
  1606.     \*======================================================================*/
  1607.  
  1608.     /*======================================================================*\
  1609.         Function:    edit_object_section()
  1610.         Purpose:    Edits a given Object Section
  1611.     \*======================================================================*/
  1612.  
  1613.     /*======================================================================*\
  1614.         Function:    del_object_section()
  1615.         Purpose:    Deletes a given Object Section and, if explicitly
  1616.                         asked, all the section objects
  1617.                         ERASE feature by: Martino Piccinato
  1618.     \*======================================================================*/
  1619.  
  1620.     /*
  1621.      *
  1622.      * Mambo Utility Methods
  1623.      *
  1624.      */
  1625.  
  1626.     /*======================================================================*\
  1627.         Function:    has_group_parent
  1628.         Purpose:    Checks whether the 'source' group is a child of the 'target'
  1629.     \*======================================================================*/
  1630.     function is_group_child_of$grp_src$grp_tgt$group_type='ARO' {
  1631.         $this->debug_text("has_group_parent(): Source=$grp_src, Target=$grp_tgt, Type=$group_type");
  1632.  
  1633.         switch(strtolower(trim($group_type))) {
  1634.             case 'axo':
  1635.                 $table $this->_db_table_prefix .'axo_groups';
  1636.                 break;
  1637.             default:
  1638.                 $table $this->_db_table_prefix .'aro_groups';
  1639.                 break;
  1640.         }
  1641.  
  1642.         if (is_int$grp_src && is_int($grp_tgt)) {
  1643.             $this->db->setQuery"SELECT COUNT(*)"
  1644.                 . "\nFROM $table AS g1"
  1645.                 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
  1646.                 . "\nWHERE g1.group_id=$grp_src AND g2.group_id=$grp_tgt"
  1647.             );
  1648.         else if (is_string$grp_src && is_string($grp_tgt)) {
  1649.             $this->db->setQuery"SELECT COUNT(*)"
  1650.                 . "\nFROM $table AS g1"
  1651.                 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
  1652.                 . "\nWHERE g1.name='$grp_src' AND g2.name='$grp_tgt'"
  1653.             );
  1654.         else if (is_int$grp_src && is_string($grp_tgt)) {
  1655.             $this->db->setQuery"SELECT COUNT(*)"
  1656.                 . "\nFROM $table AS g1"
  1657.                 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
  1658.                 . "\nWHERE g1.group_id='$grp_src' AND g2.name='$grp_tgt'"
  1659.             );
  1660.         else {
  1661.             $this->db->setQuery"SELECT COUNT(*)"
  1662.                 . "\nFROM $table AS g1"
  1663.                 . "\nLEFT JOIN $table AS g2 ON g1.lft > g2.lft AND g1.lft < g2.rgt"
  1664.                 . "\nWHERE g1.name=$grp_src AND g2.group_id='$grp_tgt'"
  1665.             );
  1666.         }
  1667.  
  1668.         return $this->db->loadResult();
  1669.     }
  1670.  
  1671.     function getAroGroup$value {
  1672.         return $this->_getGroup'aro'$value );
  1673.     }
  1674.  
  1675.     function _getGroup$type$value {
  1676.         global $database;
  1677.  
  1678.         $database->setQuery"SELECT g.*"
  1679.             . "\nFROM #__core_acl_{$type}_groups AS g"
  1680.             . "\nINNER JOIN #__core_acl_groups_{$type}_map AS gm ON gm.group_id = g.group_id"
  1681.             . "\nINNER JOIN #__core_acl_{$type} AS ao ON ao.{$type}_id = gm.{$type}_id"
  1682.             . "\nWHERE ao.value='$value'"
  1683.         );
  1684.         $obj null;
  1685.         $database->loadObject$obj );
  1686.         return $obj;
  1687.     }
  1688.  
  1689.     function _getAbove({
  1690.     }
  1691.  
  1692.     function _getBelow$table$fields$groupby=null$root_id=null$root_name=null$inclusive=true {
  1693.         global $database;
  1694.  
  1695.         $root new stdClass();
  1696.         $root->lft 0;
  1697.         $root->rgt 0;
  1698.  
  1699.         if ($root_id{
  1700.         else if ($root_name{
  1701.             $database->setQuery"SELECT lft, rgt FROM $table WHERE name='$root_name');
  1702.             $database->loadObject$root );
  1703.         }
  1704.  
  1705.         $where '';
  1706.         if ($root->lft+$root->rgt <> 0{
  1707.             if ($inclusive{
  1708.                 $where "WHERE g1.lft BETWEEN $root->lft AND $root->rgt";
  1709.             else {
  1710.                 $where "WHERE g1.lft BETWEEN $root->lft+1 AND $root->rgt-1";
  1711.             }
  1712.         }
  1713.  
  1714.         $database->setQuery"SELECT $fields"
  1715.             . "\nFROM $table AS g1"
  1716.             . "\nINNER JOIN $table AS g2 ON g1.lft BETWEEN g2.lft AND g2.rgt"
  1717.             . "\n$where"
  1718.             . ($groupby "\nGROUP BY $groupby"")
  1719.             . "\nORDER BY g1.lft"
  1720.         );
  1721.  
  1722.         //echo $database->getQuery();
  1723.         return $database->loadObjectList();
  1724.     }
  1725.  
  1726.     function get_group_children_tree$root_id=null$root_name=null$inclusive=true {
  1727.         global $database;
  1728.  
  1729.         $tree gacl_api::_getBelow'#__core_acl_aro_groups',
  1730.             'g1.group_id, g1.name, COUNT(g2.name) AS level',
  1731.             'g1.name',
  1732.             $root_id$root_name$inclusive );
  1733.  
  1734.         // first pass get level limits
  1735.         $n count$tree );
  1736.         $min $tree[0]->level;
  1737.         $max $tree[0]->level;
  1738.         for ($i=0$i $n$i++{
  1739.             $min min$min$tree[$i]->level );
  1740.             $max max$max$tree[$i]->level );
  1741.         }
  1742.  
  1743.         $indents array();
  1744.         foreach (range$min$max as $i{
  1745.             $indents[$i'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  1746.         }
  1747.         // correction for first indent
  1748.         $indents[$min'';
  1749.  
  1750.         $list array();
  1751.         for ($i=$n-1$i >= 0$i--{
  1752.             $shim '';
  1753.             foreach (range$min$tree[$i]->level as $j{
  1754.                 $shim .= $indents[$j];
  1755.             }
  1756.  
  1757.             if (@$indents[$tree[$i]->level+1== '.&nbsp;'{
  1758.                 $twist '&nbsp;';
  1759.             else {
  1760.                 $twist "-&nbsp;";
  1761.             }
  1762.  
  1763.             //$list[$i] = $tree[$i]->level.$shim.$twist.$tree[$i]->name;
  1764.             $list[$imosHTML::makeOption$tree[$i]->group_id$shim.$twist.$tree[$i]->name );
  1765.             if ($tree[$i]->level @$tree[$i-1]->level{
  1766.                 $indents[$tree[$i]->level+1'.&nbsp;';
  1767.             }
  1768.         }
  1769.  
  1770.         ksort($list);
  1771.         return $list;
  1772.     }
  1773. }
  1774.  
  1775. class mosARO extends mosDBTable {
  1776. /** @var int Primary key */
  1777.     var $aro_id=null;
  1778.     var $section_value=null;
  1779.     var $value=null;
  1780.     var $order_value=null;
  1781.     var $name=null;
  1782.     var $hidden=null;
  1783.  
  1784.     function mosARO&$db {
  1785.         $this->mosDBTable'#__core_acl_aro''aro_id'$db );
  1786.     }
  1787.  
  1788. /**
  1789. * Utility function for returning groups
  1790. */
  1791.  
  1792. }
  1793.  
  1794. class mosAroGroup extends mosDBTable {
  1795. /** @var int Primary key */
  1796.     var $group_id=null;
  1797.     var $parent_id=null;
  1798.     var $name=null;
  1799.     var $lft=null;
  1800.     var $rgt=null;
  1801.  
  1802.     function mosAroGroup&$db {
  1803.         $this->mosDBTable'#__core_acl_aro_groups''group_id'$db );
  1804.     }
  1805. }
  1806.  
  1807. ?>

Documentation generated on Mon, 05 May 2008 16:19:56 +0400 by phpDocumentor 1.4.0