Index: branches/5.2.x/core/kernel/db/dbitem.php =================================================================== diff -u -N -r14092 -r14095 --- branches/5.2.x/core/kernel/db/dbitem.php (.../dbitem.php) (revision 14092) +++ branches/5.2.x/core/kernel/db/dbitem.php (.../dbitem.php) (revision 14095) @@ -1,6 +1,6 @@ ErrorMsgs['required'] = '!la_err_required!'; //'Field is required'; $this->ErrorMsgs['unique'] = '!la_err_unique!'; //'Field value must be unique'; $this->ErrorMsgs['value_out_of_range'] = '!la_err_value_out_of_range!'; //'Field is out of range, possible values from %s to %s'; @@ -88,17 +106,17 @@ $this->ErrorMsgs['primary_lang_required'] = '!la_err_primary_lang_required!'; } - function SetDirtyField($field_name, $field_value) + public function SetDirtyField($field_name, $field_value) { $this->DirtyFieldValues[$field_name] = $field_value; } - function GetDirtyField($field_name) + public function GetDirtyField($field_name) { return $this->DirtyFieldValues[$field_name]; } - function GetOriginalField($field_name, $formatted = false, $format=null) + public function GetOriginalField($field_name, $formatted = false, $format=null) { if (array_key_exists($field_name, $this->OriginalFieldValues)) { // item was loaded before @@ -129,32 +147,27 @@ * * @param string $field_name */ - function SetOriginalField($field_name, $field_value) + public function SetOriginalField($field_name, $field_value) { $this->OriginalFieldValues[$field_name] = $field_value; } /** * Set's default values for all fields * - * @param bool $populate_ml_fields create all ml fields from db in config or not - * * @access public */ - function SetDefaultValues($populate_ml_fields = false) + public function SetDefaultValues() { - parent::SetDefaultValues($populate_ml_fields); - if ($populate_ml_fields) { + parent::SetDefaultValues(); + + if ($this->populateMultiLangFields) { $this->PopulateMultiLangFields(); } - foreach ($this->Fields as $field => $params) { - if ( isset($params['default']) ) { - $this->SetDBField($field, $params['default']); - } - else { - $this->SetDBField($field, NULL); - } + foreach ($this->Fields as $field => $field_options) { + $default_value = isset($field_options['default']) ? $field_options['default'] : NULL; + $this->SetDBField($field, $default_value); } } @@ -167,7 +180,7 @@ * @param mixed $value Value to set the field to * @return void */ - function SetField($name,$value) + public function SetField($name,$value) { $options = $this->GetFieldOptions($name); $parsed = $value; @@ -192,7 +205,7 @@ * @param mixed $value Value to set the field to * @return void */ - function SetDBField($name,$value) + public function SetDBField($name,$value) { $this->FieldValues[$name] = $value; /*if (isset($this->Fields[$name]['formatter'])) { @@ -211,7 +224,7 @@ * * @return bool */ - function SetError($field, $pseudo, $error_label = null, $error_params = null) + public function SetError($field, $pseudo, $error_label = null, $error_params = null) { $error_field = isset($this->Fields[$field]['error_field']) ? $this->Fields[$field]['error_field'] : $field; if (isset($this->FieldErrors[$error_field]['pseudo'])) { @@ -240,14 +253,40 @@ } /** + * Removes error on field + * + * @param string $field + * @access public + */ + public function RemoveError($field) + { + unset( $this->FieldErrors[$field] ); + } + + /** + * Returns error pseudo + * + * @param string $field + * @return string + */ + public function GetErrorPseudo($field) + { + if ( !array_key_exists($field, $this->FieldErrors) ) { + return ''; + } + + return array_key_exists('pseudo', $this->FieldErrors[$field]) ? $this->FieldErrors[$field]['pseudo'] : ''; + } + + /** * Return current item' field value by field name * (doesn't apply formatter) * - * @access public * @param string $name field name to return * @return mixed + * @access public */ - function GetDBField($name) + public function GetDBField($name) { /*if (!array_key_exists($name, $this->FieldValues) && defined('DEBUG_MODE') && DEBUG_MODE) { $this->Application->Debugger->appendTrace(); @@ -256,12 +295,12 @@ return $this->FieldValues[$name]; } - function HasField($name) + public function HasField($name) { - return isset($this->FieldValues[$name]); + return array_key_exists($name, $this->FieldValues); } - function GetFieldValues() + public function GetFieldValues() { return $this->FieldValues; } @@ -272,12 +311,11 @@ * The function sets current item fields to values passed in $hash, by matching $hash keys with field names * of current item. If current item' fields are unknown {@link kDBItem::PrepareFields()} is called before acutally setting the fields * - * @access public * @param Array $hash * @param Array $set_fields Optional param, field names in target object to set, other fields will be skipped - * @return void + * @access public */ - function SetFieldsFromHash($hash, $set_fields = null) + public function SetFieldsFromHash($hash, $set_fields = null) { // used in formatter which work with multiple fields together foreach($hash as $field_name => $field_value) { @@ -307,7 +345,7 @@ } } - function SetDBFieldsFromHash($hash, $set_fields = null) + public function SetDBFieldsFromHash($hash, $set_fields = null) { foreach ($hash as $field_name => $field_value) { if (is_numeric($field_name) || !array_key_exists($field_name, $this->Fields)) { @@ -325,15 +363,15 @@ /** * Returns part of SQL WHERE clause identifing the record, ex. id = 25 * - * @access public * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method * @param Array $keys_hash alternative, then item id, keys hash to load item by * @return void * @see kDBItem::Load() * @see kDBItem::Update() * @see kDBItem::Delete() + * @access protected */ - function GetKeyClause($method=null, $keys_hash = null) + protected function GetKeyClause($method=null, $keys_hash = null) { if (!isset($keys_hash)) { $keys_hash = Array ($this->IDField => $this->ID); @@ -362,10 +400,10 @@ * @param bool $cachable cache this query result based on it's prefix serial * @return bool True if item has been loaded, false otherwise */ - function Load($id, $id_field_name = null, $cachable = false) + public function Load($id, $id_field_name = null, $cachable = false) { if ( isset($id_field_name) ) { - $this->SetIDField($id_field_name); // set new IDField + $this->IDField = $id_field_name; // set new IDField } $keys_sql = ''; @@ -379,7 +417,7 @@ if ( isset($id_field_name) ) { // restore original IDField from unit config - $this->setIDField( $this->Application->getUnitOption($this->Prefix, 'IDField') ); + $this->IDField = $this->Application->getUnitOption($this->Prefix, 'IDField'); } if (($id === false) || !$keys_sql) { @@ -411,7 +449,7 @@ } if ($field_values) { - $this->FieldValues = array_merge_recursive2($this->FieldValues, $field_values); + $this->FieldValues = array_merge($this->FieldValues, $field_values); $this->OriginalFieldValues = $this->FieldValues; } else { @@ -436,7 +474,7 @@ * @param Array $fields_hash * @param string $id_field */ - function LoadFromHash($fields_hash, $id_field = null) + public function LoadFromHash($fields_hash, $id_field = null) { if (!isset($id_field)) { $id_field = $this->IDField; @@ -455,7 +493,7 @@ return false; } - $this->FieldValues = array_merge_recursive2($this->FieldValues, $fields_hash); + $this->FieldValues = array_merge($this->FieldValues, $fields_hash); $this->OriginalFieldValues = $this->FieldValues; $this->setID($id); @@ -472,13 +510,27 @@ * @access public * @return string */ - function GetSelectSQL() + + /** + * Returns SELECT part of list' query + * + * @param string $base_query + * @param bool $replace_table + * @return string + * @access public + */ + public function GetSelectSQL($base_query = null, $replace_table = true) { - $sql = $this->addCalculatedFields($this->SelectClause); - return parent::GetSelectSQL($sql); + if (!isset($base_query)) { + $base_query = $this->SelectClause; + } + + $base_query = $this->addCalculatedFields($base_query); + + return parent::GetSelectSQL($base_query, $replace_table); } - function UpdateFormattersMasterFields() + public function UpdateFormattersMasterFields() { foreach ($this->Fields as $field => $options) { if (isset($options['formatter'])) { @@ -495,7 +547,7 @@ * @param mixed $force_id * @return bool */ - function skipField($field_name, $force_id = false) + public function skipField($field_name, $force_id = false) { $skip = false; @@ -528,7 +580,7 @@ * @param int Primery Key Id to update * @return bool */ - function Update($id = null, $system_update = false) + public function Update($id = null, $system_update = false) { if (isset($id)) { $this->setID($id); @@ -583,22 +635,22 @@ $affected_rows = $this->Conn->getAffectedRows(); if (!$system_update && ($affected_rows > 0)) { - $this->setModifiedFlag(clUPDATE); + $this->setModifiedFlag(ChangeLog::UPDATE); } $this->saveCustomFields(); $this->raiseEvent('OnAfterItemUpdate'); $this->OriginalFieldValues = $this->FieldValues; $this->Loaded = true; - if ($this->mode != 't') { + if (!$this->IsTempTable()) { $this->Application->resetCounters($this->TableName); } return true; } - function ValidateField($field) + public function ValidateField($field) { $options = $this->Fields[$field]; @@ -627,7 +679,7 @@ * @return bool * @access private */ - function Validate() + public function Validate() { $this->UpdateFormattersMasterFields(); //order is critical - should be called BEFORE checking errors @@ -669,7 +721,7 @@ * @param Array $params field options from config * @return bool */ - function CustomValidation($field, $params) + protected function CustomValidation($field, $params) { return true; } @@ -680,7 +732,7 @@ * @param Array $skip_fields fields to skip during error checking * @return bool */ - function HasErrors($skip_fields) + public function HasErrors($skip_fields = Array ()) { $global_res = false; @@ -701,7 +753,7 @@ * @param Array $params field options from config * @return bool */ - function ValidateType($field, $params) + protected function ValidateType($field, $params) { $res = true; $val = $this->FieldValues[$field]; @@ -732,9 +784,9 @@ * @param string $field field name * @param Array $params field options from config * @return bool - * @access private + * @access public */ - function ValidateRequired($field, $params) + public function ValidateRequired($field, $params) { $res = true; if (isset($params['required']) && $params['required']) { @@ -760,7 +812,7 @@ * @return bool * @access private */ - function ValidateUnique($field, $params) + protected function ValidateUnique($field, $params) { $res = true; $unique_fields = getArrayValue($params,'unique'); @@ -807,7 +859,7 @@ * @return bool * @access private */ - function ValidateRange($field, $params) + protected function ValidateRange($field, $params) { $res = true; $val = $this->FieldValues[$field]; @@ -858,12 +910,14 @@ * @return string * @access public */ - function GetErrorMsg($field, $force_escape = null) + public function GetErrorMsg($field, $force_escape = null) { - if( !isset($this->FieldErrors[$field]) ) return ''; + $err = $this->GetErrorPseudo($field); - $err = getArrayValue($this->FieldErrors[$field], 'pseudo'); - if (!$err) return ''; + if (!$err) { + return ''; + } + // if special error msg defined in config if( isset($this->Fields[$field]['error_msgs'][$err]) ) { @@ -883,17 +937,29 @@ { return vsprintf($msg, $this->FieldErrors[$field]['params']); } + return $msg; } /** + * Returns field errors + * + * @return Array + * @access public + */ + public function GetFieldErrors() + { + return $this->FieldErrors; + } + + /** * Creates a record in the database table with current item' values * * @param mixed $force_id Set to TRUE to force creating of item's own ID or to value to force creating of passed id. Do not pass 1 for true, pass exactly TRUE! * @access public * @return bool */ - function Create($force_id = false, $system_create = false) + public function Create($force_id = false, $system_create = false) { if (!$this->raiseEvent('OnBeforeItemCreate')) { return false; @@ -960,11 +1026,11 @@ $this->OriginalFieldValues = $this->FieldValues; if (!$system_create){ - $this->setModifiedFlag(clCREATE); + $this->setModifiedFlag(ChangeLog::CREATE); } $this->saveCustomFields(); - if ($this->mode != 't') { + if (!$this->IsTempTable()) { $this->Application->resetCounters($this->TableName); } @@ -985,7 +1051,7 @@ * @access public * @return bool */ - function Delete($id = null) + public function Delete($id = null) { if (isset($id)) { $this->setID($id); @@ -1002,20 +1068,20 @@ $affected_rows = $this->Conn->getAffectedRows(); if ($affected_rows > 0) { - $this->setModifiedFlag(clDELETE); // will change affected rows, so get it before this line + $this->setModifiedFlag(ChangeLog::DELETE); // will change affected rows, so get it before this line // something was actually deleted $this->raiseEvent('OnAfterItemDelete'); } - if ($this->mode != 't') { + if (!$this->IsTempTable()) { $this->Application->resetCounters($this->TableName); } return $ret; } - function PopulateMultiLangFields() + public function PopulateMultiLangFields() { foreach ($this->Fields as $field => $options) { // master field is set only for CURRENT language @@ -1037,13 +1103,13 @@ * @param int $foreign_key ForeignKey value to filter name check query by * @param string $title_field FieldName to alter, by default - TitleField of the prefix * @param string $format sprintf-style format of renaming pattern, by default Copy %1$s of %2$s which makes it Copy [Number] of Original Name - * @access private + * @access public */ - function NameCopy($master=null, $foreign_key=null, $title_field=null, $format='Copy %1$s of %2$s') + public function NameCopy($master=null, $foreign_key=null, $title_field=null, $format='Copy %1$s of %2$s') { if (!isset($title_field)) { - $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); - if (!$title_field || isset($this->CalculatedFields[$title_field]) ) return; + $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); + if (!$title_field || isset($this->CalculatedFields[$title_field]) ) return; } $new_name = $this->GetDBField($title_field); @@ -1083,7 +1149,7 @@ $this->SetDBField($title_field, $new_name); } - function raiseEvent($name, $id = null, $additional_params = Array()) + protected function raiseEvent($name, $id = null, $additional_params = Array()) { if( !isset($id) ) $id = $this->GetID(); $event = new kEvent( Array('name'=>$name,'prefix'=>$this->Prefix,'special'=>$this->Special) ); @@ -1096,7 +1162,7 @@ } $this->Application->HandleEvent($event); - return $event->status == erSUCCESS ? true : false; + return $event->status == kEvent::erSUCCESS ? true : false; } /** @@ -1105,7 +1171,7 @@ * @param int $new_id * @access public */ - function setID($new_id) + public function setID($new_id) { $this->ID = $new_id; $this->SetDBField($this->IDField, $new_id); @@ -1116,7 +1182,7 @@ * * @access private */ - function setTempID() + public function setTempID() { $new_id = (int)$this->Conn->GetOne('SELECT MIN('.$this->IDField.') FROM '.$this->TableName); if($new_id > 0) $new_id = 0; @@ -1175,7 +1241,7 @@ * @access private * @author Alexey */ - function setModifiedFlag($mode = null) + public function setModifiedFlag($mode = null) { $main_prefix = $this->Application->GetTopmostPrefix($this->Prefix); $this->Application->StoreVar($main_prefix . '_modified', '1', !$this->Application->isAdmin); @@ -1199,7 +1265,7 @@ * @param bool $log_changes * @return bool */ - function ShouldLogChanges($log_changes = null) + public function ShouldLogChanges($log_changes = null) { if (!isset($log_changes)) { // specific logging mode no forced -> use global logging settings @@ -1209,7 +1275,7 @@ return $log_changes && !$this->Application->getUnitOption($this->Prefix, 'ForceDontLogChanges'); } - function LogChanges($main_prefix, $mode) + protected function LogChanges($main_prefix, $mode) { if (!$mode) { return ; @@ -1267,15 +1333,15 @@ } switch ($mode) { - case clUPDATE: + case ChangeLog::UPDATE: $to_save = array_merge($this->GetTitleField(), $this->GetChangedFields()); break; - case clCREATE: + case ChangeLog::CREATE: $to_save = $this->GetTitleField(); break; - case clDELETE: + case ChangeLog::DELETE: $to_save = array_merge($this->GetTitleField(), $this->GetRealFields()); break; } @@ -1292,7 +1358,7 @@ * @param bool $top_most return topmost parent, when used * @return int */ - function getParentId($parent_prefix, $top_most = false) + protected function getParentId($parent_prefix, $top_most = false) { $current_id = $this->GetID(); $current_prefix = $this->Prefix; @@ -1366,7 +1432,7 @@ * * @return Array */ - function GetTitleField() + public function GetTitleField() { $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); @@ -1383,7 +1449,7 @@ * * @return Array */ - function GetRealFields() + public function GetRealFields() { if (function_exists('array_diff_key')) { $db_fields = array_diff_key($this->FieldValues, $this->VirtualFields, $this->CalculatedFields); @@ -1409,7 +1475,7 @@ * @param bool $include_virtual_fields * @return Array */ - function GetChangedFields($include_virtual_fields = false) + public function GetChangedFields($include_virtual_fields = false) { $changes = Array (); $fields = $include_virtual_fields ? $this->FieldValues : $this->GetRealFields(); @@ -1434,7 +1500,7 @@ * @return int * @access public */ - function GetID() + public function GetID() { return $this->ID; } @@ -1445,7 +1511,7 @@ * @return int * @access private */ - function generateID() + protected function generateID() { return 0; } @@ -1455,7 +1521,7 @@ * * @return bool */ - function isLoaded() + public function isLoaded() { return $this->Loaded; } @@ -1466,9 +1532,9 @@ * @param string $field * @return bool */ - function isRequired($field) + public function isRequired($field) { - return getArrayValue( $this->Fields[$field], 'required' ); + return isset($this->Fields[$field]['required']) && $this->Fields[$field]['required']; } /** @@ -1477,12 +1543,12 @@ * @param string $field * @param bool $is_required */ - function setRequired($field, $is_required = true) + public function setRequired($field, $is_required = true) { $this->Fields[$field]['required'] = $is_required; } - function Clear($new_id = null) + public function Clear($new_id = null) { $this->Loaded = false; $this->FieldValues = Array(); @@ -1495,16 +1561,12 @@ return $this->Loaded; } - function Query($force = false) + public function Query($force = false) { - if ($this->Application->isDebugMode()) { - $this->Application->Debugger->appendTrace(); - } - - trigger_error('Query method is called in class '.get_class($this).' for prefix '.$this->getPrefixSpecial().'', E_USER_ERROR); + throw new Exception('Query method is called in class ' . get_class($this) . ' for prefix ' . $this->getPrefixSpecial() . ''); } - function saveCustomFields() + protected function saveCustomFields() { if (!$this->customFields || $this->inCloning) { return true; @@ -1537,7 +1599,7 @@ * @param string $format * @return Array */ - function GetCol($field, $formatted = false, $format = null) + public function GetCol($field, $formatted = false, $format = null) { if ($formatted) { return Array (0 => $this->GetField($field, $format)); @@ -1546,4 +1608,16 @@ return Array (0 => $this->GetDBField($field)); } + /** + * Set's loaded status of object + * + * @param bool $is_loaded + * @access public + * @todo remove this method, since item can't be marked as loaded externally + */ + public function setLoaded($is_loaded = true) + { + $this->Loaded = $is_loaded; + } + } \ No newline at end of file