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

Source for file PNObject.class.php

Documentation is available at PNObject.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: PNObject.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  * @subpackage PNObjectUtil
  12.  */
  13.  
  14. /**
  15.  * PNObjectUtil
  16.  *
  17.  * @package Zikula_Core
  18.  * @subpackage PNObjectUtil
  19.  */
  20. class PNObject
  21. {
  22.  
  23.     // state/type (static)
  24.     var $_objType;                // object type
  25.     var $_objJoin;                // object join data
  26.     var $_objValidation;          // object validation data
  27.  
  28.     // data + access descriptor
  29.     var $_objData;                // object data
  30.     var $_objField;               // object key retrieval field
  31.     var $_objKey;                 // object key value
  32.     var $_objPath;                // object input path
  33.     var $_objSessionPath;         // object session access path
  34.     var $_objPermissionFilter;    // object permission filter applied
  35.     var $_objColumnArray;         // columns to select
  36.  
  37.     // support
  38.     var $_table;                  // table name
  39.     var $_columns;                // column array
  40.  
  41.     // constants
  42.     var $_GET_FROM_DB                = 'D'// get data from DB
  43.     var $_GET_FROM_GET               = 'G'// get data from $_GET
  44.     var $_GET_FROM_POST              = 'P'// get data from $_POST
  45.     var $_GET_FROM_REQUEST           = 'R'// get data from $_REQUEST
  46.     var $_GET_FROM_SESSION           = 'S'// get data from $_SESSION
  47.     var $_GET_FROM_VALIDATION_FAILED = 'V'// get data from failed validation
  48.  
  49.  
  50.     /**
  51.      * Constructur, init everything to sane defaults and handle parameters
  52.      *
  53.      * @param init       Initialization value (see _init() for details)
  54.      * @param key        The DB key to use to retrieve the object (optional) (default=null)
  55.      * @param field      The field containing the key value (optional) (default=null)
  56.      */
  57.     function PNObject ($init=null$key=null$field=null)
  58.     {
  59.         $this->_objType             = 'pn_generic';
  60.         $this->_objValidation       = null;
  61.         $this->_objJoin             = null;
  62.         $this->_objData             = null;
  63.         $this->_objField            = 'id';
  64.         $this->_objKey              = 0;
  65.         $this->_objPath             = 'generic';
  66.         $this->_objSessionPath      = '';
  67.         $this->_objPermissionFilter = null;
  68.         $this->_objCategoryFilter   null;
  69.         $this->_objColumnArray      = null;
  70.  
  71.         $this->_init ($init$key$field);
  72.     }
  73.  
  74.  
  75.     /**
  76.      * Internal intialization routine
  77.      *
  78.      * @param init       Initialization value (can be an object or a string directive)
  79.      * @param key        The DB key to use to retrieve the object (optional) (default=null)
  80.      * @param field      The field containing the key value (optional) (default=null)
  81.      *
  82.      *  If $_init is an arrary it is set(), otherwise it is interpreted as a string specifying
  83.      *  the source from where the data should be retrieved from.
  84.      */
  85.     function _init ($init=null$key=null$field=null)
  86.     {
  87.         if ($this->_objType != 'pn_generic'{
  88.             $pntables         pnDBGetTables();
  89.             $tkey             $this->_objType;
  90.             $ckey             $this->_objType . "_column";
  91.             $this->_table     = $pntables[$tkey];
  92.             $this->_columns   = $pntables[$ckey];
  93.             if ($field{
  94.                 $this->_objField  = $field;
  95.             }
  96.         }
  97.  
  98.         if (!$init{
  99.             return;
  100.         }
  101.  
  102.         if (is_array($init)) {
  103.             $this->setData ($init);
  104.         elseif (is_string($init)) {
  105.             if ($init == $this->_GET_FROM_DB{
  106.                 if (!strlen($key))
  107.                     return pn_exit ("Missing DB-key in PNObject::init() ...");
  108.  
  109.                 $this->get($key$this->_objFieldtrue);
  110.             }
  111.             else
  112.             if ($init == $this->_GET_FROM_GET)
  113.                 $this->setData (FormUtil::getPassedValue ($this->_objPathnull'GET'));
  114.             else
  115.             if ($init == $this->_GET_FROM_POST)
  116.                 $this->setData (FormUtil::getPassedValue ($this->_objPathnull'POST'));
  117.             else
  118.             if ($init == $this->_GET_FROM_REQUEST)
  119.                 $this->setData (FormUtil::getPassedValue ($this->_objPathnull'REQUEST'));
  120.             else
  121.             if ($init == $this->_GET_FROM_SESSION)
  122.                 $this->getDataFromSource ($_SESSION$this->_objPath);
  123.             else
  124.             if ($init == $this->_GET_FROM_VALIDATION_FAILED)
  125.                 $this->getDataFromSource ($_SESSION['validationFailedObjects']$this->_objPath);
  126.             else
  127.                 return pn_exit ("Invalid init-directive [$init] found in PNObject::init() ...");
  128.         }
  129.         else
  130.             return pn_exit ("Unexpected parameter type init [$init] in PNObject::init() ...");
  131.     }
  132.  
  133.  
  134.     /**
  135.      * Generate an empty object with the fields initialized to null
  136.      */
  137.     function generateEmptyObject ()
  138.     {
  139.         $pntables         pnDBGetTables();
  140.         $tkey             $this->_objType;
  141.         $ckey             $this->_objType . "_column";
  142.         $this->_table     = isset($pntables[$tkey]$pntables[$tkey'';
  143.         $this->_columns   = isset($pntables[$ckey]$pntables[$ckey'';
  144.  
  145.         $data array();
  146.         foreach ($this->_columns as $k=>$v
  147.             $data[$knull;
  148.         }
  149.  
  150.         $this->setData ($data);
  151.         return $data;
  152.     }
  153.  
  154.  
  155.     /**
  156.      * Set (and return) the object data.
  157.      *
  158.      * @param data        The data to assign
  159.      * @param cache       Whether or not to cache the data in session (optional) (default=true)
  160.      */
  161.     function setData ($data$cache=true)
  162.     {
  163.         if (!is_array($data)) {
  164.             return false;
  165.         }
  166.  
  167.         $this->_objData = $data;
  168.         if ($cache{
  169.             $_SESSION[$this->_objPath$data;
  170.         }
  171.  
  172.         return $this->_objData;
  173.     }
  174.  
  175.  
  176.     /**
  177.      * Return the current object data. If $key and $field are supplied,
  178.      * the object is fetched again from the database.
  179.      *
  180.      * @param key        The record's key value
  181.      * @param field      The field we wish to get (optional) (default='id')
  182.      * @param force      If false, the system attempts to return the object from the session cache (optional) (default=false) (currently not used)
  183.      *
  184.      * @return The object's data value
  185.      */
  186.     function &get ($key=0$field='id'$force=false)
  187.     {
  188.         if (!$key{
  189.             return $this->_objData;
  190.         }
  191.  
  192.         return $this->select ($key$field);
  193.     }
  194.  
  195.  
  196.     /**
  197.      * Return the currently set object data
  198.      *
  199.      * @return The object's data array
  200.      */
  201.     function getData ()
  202.     {
  203.         return $this->_objData;
  204.     }
  205.  
  206.  
  207.     /**
  208.      * Return the object ID field name
  209.      *
  210.      * @return The object's ID field name
  211.      */
  212.     function getIDField ()
  213.     {
  214.         return $this->_objField;
  215.     }
  216.  
  217.  
  218.     /**
  219.      * Return the object-ID or false
  220.      *
  221.      * @return The object-ID or false
  222.      */
  223.     function getID ()
  224.     {
  225.         if (isset($this->_objData[$this->_objField])) {
  226.             return $this->_objData[$this->_objField];
  227.         }
  228.  
  229.         return false;
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Return/Select the object using the given where clause.
  235.      *
  236.      * @param where        The where-clause to use
  237.      *
  238.      * @return The object's data value
  239.      */
  240.     function &getWhere ($where)
  241.     {
  242.         return $this->select (nullnull$where);
  243.     }
  244.  
  245.  
  246.     /**
  247.      * Get the object which failed validation
  248.      *
  249.      * @return The object's data value
  250.      */
  251.     function &getFailedValidationData ()
  252.     {
  253.         $this->_objData = FormUtil::getFailedValidationObjects ($this->_objPath);
  254.         return $this->_objData;
  255.     }
  256.  
  257.  
  258.  
  259.     /**
  260.      * Return whether or not this object has a set id field
  261.      *
  262.      * @return Boolean 
  263.      */
  264.     function hasID ()
  265.     {
  266.         return (isset($this->_objData[$this->_objField]&& $this->_objData[$this->_objField]);
  267.     }
  268.  
  269.  
  270.     /**
  271.      * Select the object from the database using the specified key (and field)
  272.      *
  273.      * @param key        The record's key value (if init is a string directive)
  274.      * @param field      The key-field we wish to select by (optional) (default='id')
  275.      * @param where      The key-field we wish to select by (optional) (default='id')
  276.      *
  277.      * @return The object's data value
  278.      */
  279.     function &select ($key$field='id'$where='')
  280.     {
  281.         if (!$this->_objType{
  282.             return array();
  283.         }
  284.  
  285.         if ((!$key || !$field&& !$where{
  286.             return array();
  287.         }
  288.  
  289.         // use explicit where clause
  290.         if ($where{
  291.             if ($this->_objJoin{
  292.                 $objArray DBUtil::selectExpandedObjectArray ($this->_objType$this->_objJoin$where''-1-1''$this->_objPermissionFilter$this->_objCategoryFilter$this->_objColumnArray);
  293.             else {
  294.                 $objArray DBUtil::selectObjectArray ($this->_objType$where''-1-1''$this->_objPermissionFilter$this->_objCategoryFilter$this->_objColumnArray);
  295.             }
  296.  
  297.             if ($objArray === false{
  298.                 $this->_objData = false;
  299.             else {
  300.                 if (isset($objArray[0])) {
  301.                     $this->_objData = $objArray[0];
  302.                 else {
  303.                     $this->_objData = array();
  304.                 }
  305.             }
  306.         }
  307.         // generic key=>value lookup
  308.         else
  309.         {
  310.             if ($this->_objJoin{
  311.                 $this->_objData = DBUtil::selectExpandedObjectById ($this->_objType$this->_objJoin$key$field$this->_objColumnArray$this->_objPermissionFilter$this->_objCategoryFilter);
  312.             else {
  313.                 $this->_objData = DBUtil::selectObjectById ($this->_objType$key$field$this->_objColumnArray$this->_objPermissionFilter$this->_objCategoryFilter);
  314.             }
  315.  
  316.             $this->_key   $key;
  317.             $this->_field $field;
  318.         }
  319.  
  320.         $this->selectPostProcess ();
  321.         return $this->_objData;
  322.     }
  323.  
  324.  
  325.     /**
  326.      * Post-Process the newly selected object. Subclasses can define appropriate implementations.
  327.      *
  328.      * @param obj         Override object (needed for selectObjectArray processing) (optional) (default=null)
  329.      *
  330.      * @return The object's data value
  331.      */
  332.     function selectPostProcess ($obj=null)
  333.     {
  334.     }
  335.  
  336.  
  337.     /**
  338.      * Get the data from the various input streams provided.
  339.      *
  340.      * @param key        The access key of the object (optional) (default=null, reverts to $this->_objPath)
  341.      * @param default    The default value to return (optional) (default=null)
  342.      * @param source     Where to get the variable from (optional) (default='REQUEST')
  343.      *
  344.      * @return The requested object/value
  345.      */
  346.     function &getDataFromInput ($key=null$default=null$source='REQUEST')
  347.     {
  348.         if (!$key{
  349.             $key $this->_objPath;
  350.         }
  351.  
  352.         $obj FormUtil::getPassedValue ($key$default$source);
  353.  
  354.         if ($obj{
  355.             $this->_objData = $obj;
  356.             $this->getDataFromInputPostProcess ();
  357.             return $this->_objData;
  358.         }
  359.  
  360.         return $default;
  361.     }
  362.  
  363.  
  364.     /**
  365.      * Get the data from the various input streams provided.
  366.      *
  367.      * @param key        The access key of the object (optional) (default=null, reverts to $this->_objPath)
  368.      * @param default    The default value to return (optional) (default=null)
  369.      * @param source     Where to get the variable from (optional) (default=null)
  370.      *
  371.      * @return The requested object/value
  372.      */
  373.     function &getDataFromSession ($key=null$default=null$path=''$autocreate=true$overwriteExistingVar=false)
  374.     {
  375.         if (!$key{
  376.             $key $this->_objPath;
  377.         }
  378.         if (!$path{
  379.             $path $this->_objSessionPath;
  380.         }
  381.  
  382.         $obj SessionUtil::getVar($key$default$path$autocreate$overwriteExistingVar);
  383.         if ($obj && is_array($obj)) {
  384.             $this->_objData = $obj;
  385.             $this->getDataFromSessionPostProcess ();
  386.             return $this->_objData;
  387.         }
  388.  
  389.         return $default;
  390.     }
  391.  
  392.  
  393.     /**
  394.      * Set the current object data into session
  395.      *
  396.      * @param autocreate            The autocreate passed to SessionUtil::setVar
  397.      * @param overwriteExistingVar  The overwriteExistingVar variable passed to SessionUtil::setVar
  398.      *
  399.      * @return The session data
  400.      */
  401.     function &setDataToSession ($data=null$key=null$path=''$autocreate=true$overwriteExistingVar=false)
  402.     {
  403.         if (!$data{
  404.             $data $this->_objData;
  405.         }
  406.         if (!$key{
  407.             $key $this->_objPath;
  408.         }
  409.         if (!$path{
  410.             $path $this->_objSessionPath;
  411.         }
  412.  
  413.         $this->setDataToSessionPreProcess ($data);
  414.         SessionUtil::setVar($path$data$path$autocreate$overwriteExistingVar);
  415.         $this->_objData = $data;
  416.         return $this->_objData;
  417.     }
  418.  
  419.  
  420.     /**
  421.      * Post-Process the data after getting it from Input. Subclasses can define appropriate implementations.
  422.      */
  423.     function getDataFromInputPostProcess ($data=null)
  424.     {
  425.     }
  426.  
  427.  
  428.     /**
  429.      * Post-Process the data after getting it from Session. Subclasses can define appropriate implementations.
  430.      */
  431.     function getDataFromSessionPostProcess ($data=null)
  432.     {
  433.     }
  434.  
  435.  
  436.     /**
  437.      * Pre-Process the data before writing it to Session. Subclasses can define appropriate implementations.
  438.      */
  439.     function setDataToSessionPreProcess ($data=null)
  440.     {
  441.     }
  442.  
  443.  
  444.     /**
  445.      * Generic access function to retrieve data from the specified source
  446.      *
  447.      * @param source    The source data
  448.      * @param key       The access key of the object (optional) (default=null)
  449.      * @param default   The default value to return (optional) (default=null)
  450.      * @param clean     Whether or not to clean the acquired data (optional) (default=true)
  451.      *
  452.      * @return The requested object/value
  453.      */
  454.     function getDataFromSource ($source$key=null$default=null$clean=true)
  455.     {
  456.         if (!$key{
  457.             $key $this->_objPath;
  458.         }
  459.  
  460.         if (isset($source[$key])) {
  461.             return $this->setData ($source[$key]);
  462.         }
  463.  
  464.         return $this->setData ($default);
  465.     }
  466.  
  467.  
  468.     /**
  469.      * Generic function to retrieve
  470.      *
  471.      * @param key        The access key of the object field
  472.      * @param default    The default value to return (optional) (default=null)
  473.      *
  474.      * @return The Object Data
  475.      */
  476.     function getDataField ($key$default=null)
  477.     {
  478.         $objData $this->_objData;
  479.         if (isset($objData[$key])) {
  480.             return $objData[$key];
  481.         }
  482.  
  483.         return $default;
  484.     }
  485.  
  486.  
  487.     /**
  488.      * Generic function to retrieve
  489.      *
  490.      * @param key         The access key of the object field
  491.      * @param value       The value to assign to the specified object field
  492.      *
  493.      * @return The value which was set into the specified object field
  494.      */
  495.     function setDataField ($key$value)
  496.     {
  497.         $objData =$this->_objData;
  498.         $objData[$key$value;
  499.  
  500.         return $value;
  501.     }
  502.  
  503.  
  504.     /**
  505.      * Generic insert handler for an object (ID is inserted into the object data). If the object
  506.      * contains a valid ID, it is updated, otherwise it it inserted
  507.      *
  508.      * @return The result set
  509.      */
  510.     function save ()
  511.     {
  512.         if ($this->hasID()) {
  513.             $this->update ();
  514.         else {
  515.             $this->insert ();
  516.         }
  517.  
  518.         return $this->_objData;
  519.     }
  520.  
  521.  
  522.     /**
  523.      * Generic insert handler for an object (ID is inserted into the object data)
  524.      *
  525.      * @return The Object Data
  526.      */
  527.     function insert ()
  528.     {
  529.         $this->insertPreProcess ();
  530.         $res DBUtil::insertObject ($this->_objData$this->_objType$this->_objField);
  531.         if ($res{
  532.             $this->insertPostProcess ();
  533.             return $this->_objData;
  534.         }
  535.  
  536.         return false;
  537.     }
  538.  
  539.  
  540.     /**
  541.      * Pre-Process the data prior to an insert. Subclasses can define appropriate implementations.
  542.      */
  543.     function insertPreProcess ($data=null)
  544.     {
  545.     }
  546.  
  547.  
  548.     /**
  549.      * Post-Process the data after an insert. Subclasses can define appropriate implementations.
  550.      */
  551.     function insertPostProcess ($data=null)
  552.     {
  553.     }
  554.  
  555.  
  556.     /**
  557.      * Generic upate handler for an object
  558.      *
  559.      * @return The Object Data
  560.      */
  561.     function update ()
  562.     {
  563.         $this->updatePreProcess ();
  564.         $res DBUtil::updateObject ($this->_objData$this->_objType''$this->_objField);
  565.         if ($res{
  566.             $this->updatePostProcess ();
  567.             return $this->_objData;
  568.         }
  569.  
  570.         return false;
  571.     }
  572.  
  573.  
  574.     /**
  575.      * Pre-Process the data prior to an update. Subclasses can define appropriate implementations.
  576.      */
  577.     function updatePreProcess ($data=null)
  578.     {
  579.         // empty function, should be implemented by child classes
  580.     }
  581.  
  582.  
  583.     /**
  584.      * Post-Process the data after an update. Subclasses can define appropriate implementations.
  585.      */
  586.     function updatePostProcess ($data=null)
  587.     {
  588.         // empty function, should be implemented by child classes
  589.     }
  590.  
  591.  
  592.     /**
  593.      * Generic delete handler for an object
  594.      *
  595.      * @return The Object Data
  596.      */
  597.     function delete ()
  598.     {
  599.         if ($this->hasID()) {
  600.             $this->deletePreProcess ();
  601.             $res DBUtil::deleteObjectById ($this->_objType$this->_objData[$this->_objField]$this->_objField);
  602.             if ($res{
  603.                 $this->deletePostProcess ();
  604.                 return $this->_objData;
  605.             }
  606.         }
  607.  
  608.         return false;
  609.     }
  610.  
  611.  
  612.     /**
  613.      * Pre-Process the data prior a delete. Subclasses can define appropriate implementations.
  614.      */
  615.     function deletePreProcess ($data=null)
  616.     {
  617.         // empty function, should be implemented by child classes
  618.     }
  619.  
  620.  
  621.     /**
  622.      * Post-Process the data after a delete. Subclasses can define appropriate implementations.
  623.      */
  624.     function deletePostProcess ($data=null)
  625.     {
  626.         // empty function, should be implemented by child classes
  627.     }
  628.  
  629.  
  630.     function getValidation ()
  631.     {
  632.         return $this->_objValidation;
  633.     }
  634.  
  635.  
  636.     /**
  637.      * Generic function to validate an object
  638.      *
  639.      * @return Boolean indicating whether validation has passed or failed
  640.      */
  641.     function validate ()
  642.     {
  643.         if (!$this->_objValidation{
  644.             return true;
  645.         }
  646.  
  647.         if (!$this->_objData{
  648.             return false;
  649.         }
  650.  
  651.         Loader::loadClass ('ValidationUtil');
  652.         $res1 ValidationUtil::validateObjectPlain ($this->_objPath$this->_objData$this->_objValidation);
  653.         $res2 $this->validatePostProcess();
  654.  
  655.         return ($res1 && $res2);
  656.     }
  657.  
  658.  
  659.     /**
  660.      * Post-Process the basic object validation with class specific logic.
  661.      * Subclasses can define appropriate implementations.
  662.      */
  663.     function validatePostProcess ($type='user')
  664.     {
  665.         // empty function, should be implemented by child classes
  666.         return true;
  667.     }
  668.  
  669.  
  670.     /**
  671.      * Get the hashcode for this object data
  672.      */
  673.     function getHash ($includeStandardFields=true$objData=null)
  674.     {
  675.         if (!$objData{
  676.             $objData $this->_objData;
  677.         }
  678.  
  679.         if (!$includeStandardFields{
  680.             $objData $this->_objData// copy
  681.             ObjectUtil::removeStandardFieldsFromObject ($objData);
  682.         }
  683.  
  684.         return DataUtil::hash(serialize($objData));
  685.     }
  686.  
  687.  
  688.     /**
  689.      * Clear the failed validation errors for this object
  690.      */
  691.     function clearValidationErrors ()
  692.     {
  693.         FormUtil::clearValidationErrors ($this->_objPath);
  694.     }
  695.  
  696.  
  697.     /**
  698.      * Clear the failed validation object data for this object
  699.      */
  700.     function clearFailedValidationData ()
  701.     {
  702.         FormUtil::clearValidationFailedObjects ($this->_objPath);
  703.     }
  704.  
  705.  
  706.     /**
  707.      * Clear the session cache for this object
  708.      */
  709.     function clearSessionCache ()
  710.     {
  711.         unset ($_SESSION[$this->_objPath]);
  712.     }
  713.  
  714.  
  715.     /**
  716.      * Print HTML-formatted debug output for the object
  717.      */
  718.     function prayer ()
  719.     {
  720.         prayer ($this);
  721.     }
  722.  
  723.  
  724.     /**
  725.      * Print HTML-formatted debug output for the object data
  726.      */
  727.     function prayerData ()
  728.     {
  729.         prayer ($this->_objData);
  730.     }
  731. }

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