Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file PNObjectArray.class.php

Documentation is available at PNObjectArray.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: PNObjectArray.class.php 24439 2008-07-03 22:43:52Z Guite $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  * @subpackage PNObjectArrayUtil
  12.  */
  13.  
  14. /**
  15.  * PNObjectArrayUtil
  16.  *
  17.  * @package Zikula_Core
  18.  * @subpackage PNObjectArrayUtil
  19.  */
  20. class PNObjectArray extends PNObject
  21. {
  22.     /**
  23.      * Constructur, init everything to sane defaults and handle parameters
  24.      *
  25.      * @param init        Initialization value (see _init() for details)
  26.      * @param where       The where clause to apply to the DB get/select (optional) (default='')
  27.      */
  28.     function PNObjectArray ($init=null$where='')
  29.     {
  30.         $this->PNObject ();
  31.  
  32.         $this->_objType         = 'pn_generic';
  33.         $this->_objPath         = 'generic_array';
  34.         $this->_objWhere        '';
  35.         $this->_objSort         '';
  36.         $this->_objLimitOffset  = -1;
  37.         $this->_objLimitNumRows = -1;
  38.         $this->_objAssocKey     '';
  39.         $this->_objDistinct     false;
  40.  
  41.         $this->_init ($init$where);
  42.     }
  43.  
  44.  
  45.     /**
  46.      * Internal intialization routine
  47.      *
  48.      * @param init        Initialization value (can be an object or a string directive) (optional) (default=null)
  49.      * @param where       The where clause to use when retrieving the object array (optional) (default='')
  50.      * @param orderBy     The order-by clause to use when retrieving the object array (optional) (default='')
  51.      * @param assocKey    Key field to use for building an associative array (optional) (default=null)
  52.      *
  53.      *  If $init is an arrary it is set(), otherwise it is interpreted as a string specifying
  54.      *  the source from where the data should be retrieved from.
  55.      */
  56.     function _init ($init=null$where=''$orderBy=''$assocKey=null)
  57.     {
  58.         if ($this->_objType != 'pn_generic'{
  59.             $pntables       pnDBGetTables();
  60.             $tkey           $this->_objType;
  61.             $ckey           $tkey "_column";
  62.             $this->_table   = isset($pntables[$tkey]$pntables[$tkey'';
  63.             $this->_columns = isset($pntables[$ckey]$pntables[$ckey'';
  64.         }
  65.  
  66.         if (!$init{
  67.             return;
  68.         }
  69.  
  70.         if (is_array($init)) {
  71.             $this->setData ($init);
  72.         elseif (is_string($init)) {
  73.             if ($init == $this->_GET_FROM_DB)
  74.                 $this->get($where$orderBy-1-1$assocKeytrue);
  75.             else
  76.             if ($init == $this->_GET_FROM_GET)
  77.                 $this->getDataFromSource ($_GET$this->_objPatharray());
  78.             else
  79.             if ($init == $this->_GET_FROM_POST)
  80.                 $this->getDataFromSource ($_POST$this->_objPatharray());
  81.             else
  82.             if ($init == $this->_GET_FROM_REQUEST)
  83.                 $this->getDataFromSource ($_REQUEST$this->_objPatharray());
  84.             else
  85.             if ($init == $this->_GET_FROM_SESSION)
  86.                 $this->getDataFromSource ($_SESSION$this->_objPatharray()false);
  87.             else
  88.                 return pn_exit ("Invalid init-directive [$init] found in PNObject::init() ...");
  89.         }
  90.         else
  91.           return pn_exit ("Unexpected parameter type init [$init] in PNObject::init() ...");
  92.     }
  93.  
  94.  
  95.     /**
  96.      * Set (and return) the object data. Since we dont' have a definitive key, we don't cache
  97.      *
  98.      * @param data      The data to assign
  99.      */
  100.     function setData ($data)
  101.     {
  102.         if (!is_array($data)) {
  103.             return false;
  104.         }
  105.  
  106.         $this->_objData = $data;
  107.         return $this->_objData;
  108.     }
  109.  
  110.  
  111.     /**
  112.      * Return the record count for the given object set
  113.      *
  114.      * @param where     The where-clause to use
  115.      * @param doJoin    whether or not to use the auto-join for the count
  116.      *
  117.      * @return The object's data set count
  118.      */
  119.     function getCount ($where=''$doJoin=false)
  120.     {
  121.         if ($this->_objJoin && $doJoin{
  122.             $this->_objData = DBUtil::selectExpandedObjectCount ($this->_objType$this->_objJoin$wherefalse$this->_objCategoryFilter);
  123.         else {
  124.             $this->_objData = DBUtil::selectObjectCount ($this->_objType$where'1'false$this->_objCategoryFilter);
  125.         
  126.         return $this->_objData;
  127.     }
  128.  
  129.  
  130.     /**
  131.      * Generate a filter for the array view. Default implementation which can be overridden by subclasses.
  132.      * 
  133.      * @param filter    An array containing the set filter values (optional) (default=array())
  134.      *
  135.      * @return The generated filter (where-clause) string
  136.      */
  137.     function genFilter ($filter=array())
  138.     {
  139.         return '';
  140.     }
  141.  
  142.  
  143.     /**
  144.      * Return/Select the object using the given where clause.
  145.      *
  146.      * @param where         The where-clause to use
  147.      * @param sort          The order-by clause to use
  148.      * @param limitOffset   The limiting offset
  149.      * @param limitNumRows  The limiting number of rows
  150.      * @param assocKey      Key field to use for building an associative array (optional) (default=null)
  151.      * @param force         whether or not to force a DB-get (optional) (default=false)
  152.      * @param distinct      whether or not to do a select distinct (optional) (default=false)
  153.      *
  154.      * @return The object's data value
  155.      */
  156.     function getWhere ($where=''$sort=''$limitOffset=-1$limitNumRows=-1$assocKey=null$force=false$distinct=false)
  157.     {
  158.         if ((($where != $this->_objWhere || $sort != $this->_objSort || 
  159.              $limitOffset != $this->_objLimitOffset || $limitNumRows != $this->_objLimitNumRows || 
  160.              $assocKey != $this->_objAssocKey || $distinct != $this->_objDistinct|| !$objData|| $force{
  161.                 $this->select($where$sort$limitOffset$limitNumRows$assocKey$distinct);
  162.         }
  163.  
  164.         return $this->_objData;
  165.     }
  166.  
  167.  
  168.     /**
  169.      * Return the current object data. Maps to $this->getWhere().
  170.      *
  171.      * @param where         The where-clause to use
  172.      * @param sort          The order-by clause to use
  173.      * @param limitOffset   The limiting offset
  174.      * @param limitNumRows  The limiting number of rows
  175.      * @param assocKey      Key field to use for building an associative array (optional) (default=null)
  176.      * @param force         whether or not to force a DB-get (optional) (default=false)
  177.      * @param distinct      whether or not to do a select distinct (optional) (default=false)
  178.      *
  179.      * @return The object's data value
  180.      */
  181.     function get ($where=''$sort=''$limitOffset=-1$limitNumRows=-1$assocKey=null$force=false$distinct=false)
  182.     {
  183.         return $this->getWhere ($where$sort$limitOffset$limitNumRows$assocKey$force$distinct);
  184.     }
  185.  
  186.  
  187.     /**
  188.      * Return the currently set object data
  189.      *
  190.      * @return The object's data array
  191.      */
  192.     function getData ()
  193.     {
  194.         return $this->_objData;
  195.     }
  196.  
  197.  
  198.     /**
  199.      * Generic select handler for an object. Select (and set) the specified object array
  200.      *
  201.      * @param where         The where-clause to use
  202.      * @param orderBy       The order-by clause to use
  203.      * @param limitOffset   The limiting offset
  204.      * @param limitNumRows  The limiting number of rows
  205.      * @param assocKey      Key field to use for building an associative array (optional) (default=null)
  206.      * @param distinct      whether or not to use the distinct clause
  207.      *
  208.      * @return The selected Object-Array
  209.      */
  210.     function select ($where=''$orderBy=''$limitOffset=-1$limitNumRows=-1$assocKey=false$distinct=false)
  211.     {
  212.         if ($this->_objJoin{
  213.             $objArr DBUtil::selectExpandedObjectArray ($this->_objType$this->_objJoin$where$orderBy$limitOffset$limitNumRows$assocKey$this->_objPermissionFilter$this->_objCategoryFilter$this->_objColumnArray);
  214.         else {
  215.             $objArr DBUtil::selectObjectArray ($this->_objType$where$orderBy$limitOffset$limitNumRows$assocKey$this->_objPermissionFilter$this->_objCategoryFilter$this->_objColumnArray);
  216.         }
  217.  
  218.         $this->_objData         = $objArr;
  219.         $this->_objWhere        $where;
  220.         $this->_objSort         $orderBy;
  221.         $this->_objLimitOffset  $limitOffset;
  222.         $this->_objLimitNumRows $limitNumRows;
  223.         $this->_objAssocKey     $assocKey;
  224.         $this->_objDistinct     $distinct;
  225.  
  226.         $this->selectPostProcess ();
  227.         return $this->_objData;
  228.     }
  229.  
  230.  
  231.     /**
  232.      * Iterate over the object data and post-process it
  233.      *
  234.      * @return The Object Data
  235.      */
  236.     function selectPostProcess ($data=null)
  237.     {
  238.     }
  239.  
  240.  
  241.     /**
  242.      * Generic function to retrieve
  243.      *
  244.      * @return The Object Data
  245.      */
  246.     function getDataField ($offset$key$default=null)
  247.     {
  248.         $obj $this->_objData[$offset];
  249.         if (isset($obj[$key])) {
  250.             return $obj[$key];
  251.         }
  252.  
  253.         return $default;
  254.     }
  255.  
  256.  
  257.     /**
  258.      * Save an object - if it has an ID update it, otherwise insert it
  259.      *
  260.      * @return The result set
  261.      */
  262.     function save()
  263.     {
  264.         $rc true;
  265.         $ak array_keys($this->_objData);
  266.         if (isset($this->_objData[$ak[0]][$this->_objField]&& $this->_objData[$ak[0]][$this->_objField]{
  267.                 $rc $this->update ();
  268.         else {
  269.                 $rc $this->insert ();
  270.         }
  271.         return $rc;
  272.     }
  273.  
  274.  
  275.     /**
  276.      * Generic insert handler for an object (ID is inserted into the object data)
  277.      *
  278.      * @return The Object Data
  279.      */
  280.     function insert ()
  281.     {
  282.         $res true;
  283.         $this->insertPreProcess ();
  284.         foreach ($this->_objData as $k=>$v{
  285.             $res $res && DBUtil::insertObject ($this->_objData[$k]$this->_objType$this->_objField);
  286.         }
  287.  
  288.         if ($res{
  289.             $this->insertPostProcess ();
  290.             return $this->_objData;
  291.         }
  292.  
  293.         return false;
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Generic upate handler for an object
  299.      *
  300.      * @return The Object Data
  301.      */
  302.     function update ()
  303.     {
  304.         $res true;
  305.         $this->updatePreProcess ();
  306.         foreach ($this->_objData as $k=>$v{
  307.             $res $res && DBUtil::updateObject ($this->_objData[$k]$this->_objType''$this->_objField);
  308.         }
  309.  
  310.         if ($res{
  311.             $this->updatePostProcess ();
  312.             return $this->_objData;
  313.         }
  314.  
  315.         return false;
  316.     }
  317.  
  318.  
  319.     /**
  320.      * Generic delete handler for an object
  321.      *
  322.      * @return The Object Data
  323.      */
  324.     function delete ()
  325.     {
  326.         $res true;
  327.         $this->deletePreProcess ();
  328.         foreach ($this->_objData as $k=>$v{
  329.             $res $res && DBUtil::deleteObjectById ($this->_objType$v[$this->_objField]$this->_objField);
  330.         }
  331.  
  332.         if ($res{
  333.             $this->deletePostProcess ();
  334.             return $this->_objData;
  335.         }
  336.  
  337.         return false;
  338.     }
  339.  
  340.  
  341.     /**
  342.      * Delete with a where-clause
  343.      *
  344.      * @param where         The where-clause to use
  345.      *
  346.      * @return The Object Data
  347.      */
  348.     function deleteWhere ($where=null)
  349.     {
  350.         if (!$where{
  351.             return false;
  352.         }
  353.  
  354.         $this->deletePreProcess ();
  355.         $res DBUtil::deleteWhere ($this->_objType$where);
  356.         $this->deletePostProcess ();
  357.  
  358.         return $this->_objData;
  359.     }
  360.  
  361.  
  362.     /**
  363.      * Clean the acquired input
  364.      *
  365.      * @param objArray    The object-array to clean (optional) (default=null, reverts to $this->_objData)
  366.      *
  367.      * @return The Object Data
  368.      */
  369.     function clean ($objArray=null)
  370.     {
  371.         if (!$objArray{
  372.             $objArray =$this->_objData;
  373.         }
  374.  
  375.         $ak array_keys($objArray);
  376.         foreach ($ak as $k{
  377.             $obj =$objArray[$k];
  378.             $ak2 array_keys($obj);
  379.             foreach ($ak2 as $f{
  380.                 $obj[$fFormUtil::getPassedValue(trim($obj[$f]));
  381.             }
  382.         }
  383.  
  384.         return $objArray;
  385.     }
  386.  
  387.  
  388.     /**
  389.      * Get a selector for the object array
  390.      *
  391.      * @param name          The name of the selector to generate
  392.      * @param selected      The currently selected value (optional) (default=-1234)
  393.      * @param defaultValue  The default value (optional) (default=0)
  394.      * @param defaultText   The default text (optional) (default='')
  395.      * @param allValue      The all-selected value (optional) (default=0)
  396.      * @param allText       The all-selected text (optional) (default='')
  397.      * @param idField       The id field to use (optional) (default='id')
  398.      * @param nameField     The name field to use (optional) (default='title')
  399.      * @param submit        whether or not to submit the form upon selection (optional) (default=false)
  400.      * @param ignoreList    The list of entries to ignore (default=null)
  401.      *
  402.      * @return The generated selector html text
  403.      */
  404.     function getSelector ($name$selected=-1234$defaultValue=0$defaultText='',
  405.                           $allValue=0$allText=''$idField='id'$nameField='title',
  406.                           $submit=false$ignoreList=null)
  407.     {
  408.         $html '<select name="'.$name.'"'
  409.                .' id="'.DataUtil::formatForDisplay($name).'"'
  410.                .($submit ' onChange="this.form.submit();"' '')
  411.                .'>';
  412.  
  413.         if ($defaultText{
  414.             $html .= "<option value=\"$defaultValue\">$defaultText</option>";
  415.         }
  416.  
  417.         if ($allText{
  418.             $html .= "<option value=\"$allValue\">$allText</option>";
  419.         }
  420.  
  421.         foreach ($this->_objData as $k => $v{
  422.             $disp  $v[$nameField];
  423.             $val   $v[$idField];
  424.  
  425.             if (strpos($ignoreList'$val'=== false{
  426.                 $sel   ($val==$selected "selected" "");
  427.                 $html .= "<option value=\"$val\" $sel>$disp</option>";
  428.             }
  429.         }
  430.         $html .= '</select>';
  431.  
  432.         return $html;
  433.     }
  434.  
  435.  
  436.     /**
  437.      * Constructur, init everything to sane defaults and handle parameters
  438.      *
  439.      * @return Boolean indicating whether or not validation passed successfully
  440.      */
  441.     function validate ()
  442.     {
  443.         Loader::loadClass ('ValidationUtil');
  444.  
  445.         $res1 ValidationUtil::validateObjectPlain ($this->_objPath$this->_objValidation);
  446.         $res2 $this->validatePostProcess();
  447.  
  448.         if (!$res || !$res2{
  449.             return false;
  450.         }
  451.  
  452.         return true;
  453.     }
  454.  
  455.  
  456.     /**
  457.      * Get the hashcode for this object data array
  458.      */
  459.     function getHash ($includeStandardFields=true$objArray=null)
  460.     {
  461.         if (!$objArray{
  462.             $objArray $this->_objData;
  463.         }
  464.  
  465.         $arrayHash array();
  466.         foreach ($objArray as $obj{
  467.             if (!$includeStandardFields{
  468.                 ObjectUtil::removeStandardFieldsFromObject ($obj);
  469.             }
  470.             $arrayHash[DataUtil::hash(serialize($obj));
  471.         }
  472.  
  473.         return DataUtil::hash(serialize($arrayHash));
  474.     }
  475. }

Documentation generated on Fri, 18 Jul 2008 21:54:06 +0200 by phpDocumentor 1.4.1