/**
  * Construct this module class
  * @param string $name The name of this storage mechanism
  * @param array $options
  */
 public function __construct($name, $options = array())
 {
     $this->userAccess = I2CE::getUserAccess();
     parent::__construct($name, $options);
 }
 /**
  * @param string $form.  THe form name
  * @param array $fields of string. The fields we want returned
  * Can include the special field 'last_modified' to get the last modification time for any of the fields of that form which is returned in the format  "Y-m-d H:i:s"
  * @param boolean $parent. Defaults to false.    If it is scalar and non-boolean, it is consider to be the ID of the parent, 
  * and then we get all forms with parent the given id. If true, we return the parent as one of the fields.
  *@param array $where_data.  contains the  where clause information about this form or a nested      
  * @param array $ordering. An array of fields to order by.  Defaults to the empty array.  Prepend a - to order by in descending order.
  * @param mixed $limit Defaults to false.  It true, returns only one result.  If an integer it is the numeber of records to limit to.
  *  If it is as an array of two integers, it is the offset and then number of results to limit to.
  * @param integer $mod_time. Defaults to -1.  If non-negative, we only list the requested fields for an id if at least one of them has a modification
  *    time greater than or equal to $mod_time.  If the form storage has no way of tracking modifucation time, all entries are listed.
  * @returns mixed an array with key id's and value and array of values.  the array of values has as keys the fields with their corresponding value.
  */
 public function listDisplayFields($form, $fields, $parent = false, $where_data = array(), $ordering = array(), $limit = false, $mod_time = -1, $user = false)
 {
     //public  function listDisplayFields($form, $fields, $parent = false, $where_data=array(), $ordering=array(), $limit = false, $mod_time = -1) {
     if (is_array($mod_time) && array_key_exists('mod_time', $mod_time)) {
         $mod_time = $mod_time['mod_time'];
     }
     if (is_scalar($mod_time) && $mod_time > 0) {
         $this->getDOMData($form);
         //need to do this so the mod time is set.
         if ($this->mod_time[$form] < $mod_time) {
             return array();
         }
     }
     $vals = parent::listDisplayFields($form, $fields, $parent, $where_data, $ordering, $limit, $mod_time);
     if (array_search('last_modified', $fields) !== false) {
         $mod_time = date("Y-m-d H:i:s", $this->mod_time[$form]);
         foreach ($vals as $id => &$data) {
             $data['last_modified'] = $mod_time;
         }
     }
     return $vals;
 }
 /**
  * @param string $form  The form name.
  * @param boolean $parent. Defaults to false.    If it is scalar and non-boolean, it is consider to be the ID of the parent, 
  * and then we get all forms with parent the given id.
  * @param mixed $where_data array or class implementing ArrayAccess, Iterator, and Countable (e.g. MagicDataNode) . the where data.  
  * @param array $ordering. An array of fields to order by.  Defaults to the empty array.  Prepend a - to order by in descending order.
  * @param mixed $limit. Defaults to false.  It true, returns only one result.  If an integer it is the numeber of records to limit to.
  *  If it is as an array of two integers, it is the offset and then number of results to limit to.  
  * @returns mixed an array of matching form ids.  However, ff $limit_one is true or 1 or 
  * array ($offset,1) then then we return either the id or false,  if none found or there was an error.
  */
 public function search($form, $parent = false, $where_data = array(), $ordering = array(), $limit = false)
 {
     $qry = $this->getFields($form, array('id' => 'id'), $parent, $where_data, $ordering, $limit);
     $limit_one = false;
     if (is_array($limit)) {
         if (count($limit) == 2) {
             end($limit);
             if (current($limit) == 1) {
                 $limit_one = true;
             }
         }
     } else {
         $limit_one = $limit === true || is_numeric($limit) && $limit == 1;
     }
     if (!$qry) {
         // The form may not exist yet so nothing to query so return false since nothing can be found.
         if ($limit_one) {
             return false;
         } else {
             return array();
         }
     }
     if (!$qry) {
         return parent::search($form, $parent, $where_data, $ordering, $limit);
     }
     $results = $this->db->query($qry);
     if (I2CE::pearError($results, "Bad query -- {$qry}")) {
         return parent::search($form, $parent, $where_data, $ordering, $limit);
     }
     $this->queryLastListCount($form);
     if ($limit_one) {
         //if (($data = $results->fetchRow()) !== false) {
         if ($data = $results->fetchRow()) {
             return $data->id;
         } else {
             return false;
         }
     } else {
         $res = array();
         while ($data = $results->fetchRow()) {
             $res[] = $data->id;
         }
         $results->free();
         return $res;
     }
 }
 /**
  * Save a form object into magicdata
  * @param I2CE_Form $form
  * @param I2CE_User $user
  * @param boolean $transact
  */
 public function save($form, $user, $transact)
 {
     $form_id = $form->getId();
     if (!$form_id) {
         $form_id = $this->getNextAvailableId($form->getName());
     }
     if (!$form_id) {
         return false;
     }
     $form->setId($form_id);
     $form_config = $this->getFormConfig($form, true);
     if (!$form_config instanceof I2CE_MagicDataNode) {
         return false;
     }
     $form_config->last_modified = I2CE_Date::now(I2CE_Date::DATE_TIME)->dbFormat();
     $form_config->who = $user->getId();
     $parent = $form->getParent();
     if ($parent != "") {
         /*  Does this need to be here?  the parent node may be new and doesn't exist...
             if ($form_config->is_parent('parent')) {
                 return false;
             }
             */
         $form_config->parent = $parent;
     }
     return parent::save($form, $user, $transact);
 }