示例#1
0
 /**
  * Loads a row from the database and binds the fields to the object properties
  * If $load_eav is true, binds also the eav fields linked to this entity
  *
  * @access	public
  * @param	mixed	Optional primary key.  If not specifed, the value of current key is used
  * @param	bool	reset the object values?
  * @param	bool	load the eav values for this object
  *
  * @return	boolean	True if successful
  */
 function load($oid = null, $reset = true, $load_eav = true)
 {
     $eavs = array();
     /* Get the application */
     $app = JFactory::getApplication();
     $editable_by = $app->isAdmin() ? 1 : 2;
     if ($app->isAdmin()) {
         $view = $app->input->get('view', '');
         //$view = JRequest::get('view', '' );
         if ($view == 'pos') {
             // display all for POS
             $editable_by = array(1, 2);
         }
     }
     if (!is_array($oid)) {
         // load by primary key if not array
         $keyName = $this->getKeyName();
         $oid = array($keyName => $oid);
     }
     if (empty($oid)) {
         // if empty, use the value of the current key
         $keyName = $this->getKeyName();
         $oid = $this->{$keyName};
         if (empty($oid)) {
             // if still empty, fail
             $this->setError(JText::_('COM_CITRUSCART_CANNOT_LOAD_WITH_EMPTY_KEY'));
             return false;
         }
     }
     // allow $oid to be an array of key=>values to use when loading
     $oid = (array) $oid;
     $this->_linked_table_key = isset($this->_linked_table_key) ? $this->_linked_table_key : (isset($oid[$this->_linked_table_key_name]) ? $oid[$this->_linked_table_key_name] : '');
     if (!empty($reset)) {
         $this->reset();
     }
     $db = $this->getDBO();
     // initialize the query
     $query = new DSCQuery();
     $query->select('*');
     $query->from($this->getTableName());
     if ($load_eav) {
         Citruscart::load("CitruscartHelperBase", 'helpers._base');
         $eav_helper = CitruscartHelperBase::getInstance('Eav');
         $k = $this->_tbl_key;
         $id = $this->{$k};
         // Get the custom fields for this entities
         Citruscart::load('CitruscartHelperEav', 'helpers.eav');
         $eavs = CitruscartHelperEav::getAttributes($this->get('_suffix'), $id, true, $editable_by);
         // Is this a mirrored table (see decription at the beginning of this file)
         if (strlen($this->_linked_table) && $this->_linked_table_key) {
             // Copy the custom field value to this table
             $mirrored_eavs = $eav_helper->getAttributes($this->_linked_table, $this->_linked_table_key, true, $editable_by);
             $eavs = array_merge($eavs, $mirrored_eavs);
         }
     }
     foreach ($oid as $key => $value) {
         // Check that $key is field in table
         if (!in_array($key, array_keys($this->getProperties()))) {
             // Check if it is a eav field
             if ($load_eav) {
                 // loop through until the key is found or the eav are finished
                 $found = false;
                 $i = 0;
                 while (!$found && $i < count($eavs)) {
                     // Does the key exists?
                     if ($key == $eavs[$i]->eavattribute_alias) {
                         $found = true;
                     } else {
                         $i++;
                     }
                 }
                 // Was the key found?
                 if (!$found) {
                     // IF not return an error
                     $this->setError(get_class($this) . ' does not have the field ' . $key);
                     return false;
                 }
                 // key was found -> add this EAV field
                 $value_tbl_name = 'value_' . $eavs[$i]->eavattribute_alias;
                 // for some reason MySQL makes spaces around '-' charachter
                 // (which is often charachter in aliases) that's why we replace it with '_'
                 $value_tbl_name = str_replace("-", "_", $value_tbl_name);
                 // Join the table based on the type of the value
                 $table_type = $eav_helper->getType($eavs[$i]->eavattribute_alias);
                 // Join the tables
                 $query->join('LEFT', '#__citruscart_eavvalues' . $table_type . ' AS ' . $value_tbl_name . ' ON ( ' . $value_tbl_name . '.eavattribute_id = ' . $eavs[$i]->eavattribute_id . ' AND ' . $value_tbl_name . '.eaventity_id =  ' . $this->_tbl_key . ' )');
                 // Filter using '='
                 $query->where($value_tbl_name . ".eavvalue_value = '" . $value . "'");
                 // else let the store() method worry over this
             } else {
                 $this->setError(get_class($this) . ' does not have the field ' . $key);
                 return false;
             }
         } else {
             // add the key=>value pair to the query
             $value = $db->q($db->escape(trim(strtolower($value))));
             $query->where($key . ' = ' . $value);
         }
     }
     $db->setQuery((string) $query);
     if ($result = $db->loadAssoc()) {
         $result = $this->bind($result);
         if ($result) {
             // Only now load the eav, in necessary
             // Check if it is a eav field
             if ($load_eav) {
                 $k = $this->_tbl_key;
                 $id = $this->{$k};
                 // Get the custom fields for this entities
                 Citruscart::load('CitruscartHelperEav', 'helpers.eav');
                 $eavs = CitruscartHelperEav::getAttributes($this->get('_suffix'), $id, false, $editable_by);
                 // Is this a mirrored table (see decription at the beginning of this file)
                 if (strlen($this->_linked_table) && $this->_linked_table_key) {
                     // Copy the custom field value to this table
                     $mirrored_eavs = $eav_helper->getAttributes($this->_linked_table, $this->_linked_table_key);
                     $eavs = array_merge($eavs, $mirrored_eavs);
                 }
                 if (count($eavs)) {
                     foreach ($eavs as $eav) {
                         $key = $eav->eavattribute_alias;
                         $value = $eav_helper->getAttributeValue($eav, $this->get('_suffix'), $id);
                         $this->{$key} = $value;
                     }
                 }
             }
             $app->triggerEvent('onLoad' . $this->get('_suffix'), array(&$this));
         }
         return $result;
     } else {
         $this->setError($db->getErrorMsg());
         return false;
     }
 }