Index: trunk/core/kernel/kbase.php =================================================================== diff -u -r932 -r1339 --- trunk/core/kernel/kbase.php (.../kbase.php) (revision 932) +++ trunk/core/kernel/kbase.php (.../kbase.php) (revision 1339) @@ -31,18 +31,42 @@ */ var $Special=''; + var $OriginalParams; + /** + * Set's application + * + * @return kBase + * @access public + */ + function kBase() + { + $this->Application =& kApplication::Instance(); + } + + /** + * Create new instance of object + * + * @return kBase + */ + function &makeClass() + { + return new kBase(); + } + + /** * Set's prefix and special * * @param string $prefix * @param string $special * @access public */ - function Init($prefix,$special) + function Init($prefix,$special,$event_params=null) { $prefix=explode('_',$prefix,2); $this->Prefix=$prefix[0]; $this->Special=$special; + $this->OriginalParams = $event_params; } /** @@ -57,16 +81,15 @@ return rtrim($this->Prefix.'.'.$this->Special,'.'); } - /** - * Set's application - * - * @return kBase - * @access public - */ - function kBase() + function &getProperty($property_name) { - $this->Application =& kApplication::Instance(); + return $this->$property_name; } + + function setProperty($property_name, &$property_value) + { + $this->$property_name =& $property_value; + } } class kDBBase extends kBase { @@ -103,22 +126,45 @@ var $DisplayQueries = false; /** - * Object that holds all - * formatters created + * Fields allowed to be set (from table + virtual) + * + * @var Array + * @access private + */ + var $Fields=Array(); + + /** + * All virtual field names * - * @var kItemFormatter + * @var Array * @access private */ - var $ItemFormatter; + var $VirtualFields=Array(); /** + * Fields that need to be queried using custom expression, e.g. IF(...) AS value + * + * @var Array + * @access private + */ + var $CalculatedFields = Array(); + + /** * Description * * @var string Item' database table name, without prefix * @access public */ var $TableName; + /** + * Allows to determine object's table status ('temp' - temp table, '' - live table) + * + * @var string + * @access public + */ + var $mode=''; + function kDBBase() { parent::kBase(); @@ -138,6 +184,28 @@ } /** + * Set object' TableName to Live table from config + * + * @access public + */ + function SwitchToLive() + { + $this->TableName = $this->Application->getUnitOption($this->Prefix, 'TableName'); + } + + /** + * Set object' TableName to Temp table from config + * + * @access public + */ + function SwitchToTemp() + { + $this->TableName = $this->Application->getUnitOption($this->Prefix, 'TableName'); + $this->SetTableName( kTempTablesHandler::GetTempName($this->TableName) ); + $this->mode = 't'; + } + + /** * Sets SELECT part of list' query * * @access public @@ -149,12 +217,38 @@ $this->SelectClause = $sql; } - function GetSelectSQL() + function GetSelectSQL($base_query=null) { - return sprintf($this->SelectClause,$this->TableName); + if( !isset($base_query) ) $base_query = $this->SelectClause; + return $q = str_replace( Array('%1$s','%s'), $this->TableName, $base_query); } /** + * Insert calculated fields sql into query in place of %2$s, + * return processed query. + * + * @param string $query + * @return string + */ + function addCalculatedFields($query) + { + if($this->CalculatedFields) + { + $sql = Array(); + foreach($this->CalculatedFields as $field_name => $field_expression) + { + $sql[] = '('.$field_expression.') AS '.$field_name; + } + $sql = implode(',',$sql); + return $this->Application->ReplaceLanguageTags( str_replace('%2$s', ','.$sql, $query) ); + } + else + { + return str_replace('%2$s', '', $query); + } + } + + /** * Sets ID Field name used as primary key for loading items * * @access public @@ -167,18 +261,123 @@ $this->IDField = $field_name; } + function Configure() + { + $this->setTableName( $this->Application->getUnitOption($this->Prefix, 'TableName') ); + $this->setIDField( $this->Application->getUnitOption($this->Prefix, 'IDField') ); + $this->setConfigFields( $this->Application->getUnitOption($this->Prefix, 'Fields') ); + $this->setVirtualFields( $this->Application->getUnitOption($this->Prefix, 'VirtualFields') ); + $this->setCalculatedFields( $this->Application->getUnitOption($this->Prefix, 'CalculatedFields') ); + $this->prepareConfigOptions(); // this should go last, but before setDefaultValues, order is significant! + $this->SetDefaultValues(); + } + + function setCalculatedFields($fields) + { + $this->CalculatedFields = isset($fields[$this->Special]) ? $fields[$this->Special] : (isset($fields['']) ? $fields[''] : false); + } + /** + * Set's field names from table + * from config + * + * @param Array $fields + * @access public + */ + function setConfigFields($fields) + { + $this->Fields=$fields; + } + + /** + * Set fields (+options) for fields that physically doesn't exist in database + * + * @param Array $fields + * @access public + */ + function setVirtualFields($fields) + { + if($fields) + { + $this->VirtualFields = $fields; + $this->Fields = array_merge_recursive2($this->VirtualFields, $this->Fields); + } + } + + function SetDefaultValues() + { + + } + + function SetFieldOptions($field, $options) + { + $this->Fields[$field] = $options; + } + + function GetFieldOptions($field) + { + return isset($this->Fields[$field]) ? $this->Fields[$field] : Array(); + } + + /** * Returns formatted field value * * @param string $field * @return string * @access public */ - function GetField($field) + function GetField($name, $format=null) { + $options = $this->GetFieldOptions($name); + $val = $this->GetDBField($name); + $res = $val; + if (isset($options['formatter'])) { + $formatter =& $this->Application->recallObject($options['formatter']); + $res = $formatter->Format($val, $name, $this, $format ); + } + return $res; + } + + function HasField($name) + { + + } + + function GetFieldValues() + { } + function UpdateFormattersSubFields() + { + foreach ($this->Fields as $field => $options) { + if (isset($options['formatter'])) { + $formatter =& $this->Application->recallObject($options['formatter']); + $formatter->UpdateSubFields($field, $this->GetDBField($field), $options, $this); + } + } + } + + function prepareConfigOptions() + { + foreach (array_keys($this->Fields) as $field_name) + { + $field_options =& $this->Fields[$field_name]; + if( isset($field_options['options_sql']) ) + { + // replace with query result + $select_clause = $field_options['option_title_field'].','.$field_options['option_key_field']; + $sql = sprintf($field_options['options_sql'], $select_clause); + $field_options['options'] = $this->Conn->GetCol($sql, $field_options['option_key_field']); + unset($field_options['options_sql']); + } + if ( $formatter_class = getArrayValue($field_options, 'formatter') ) { + $formatter =& $this->Application->recallObject($formatter_class); + $formatter->PrepareOptions($field_name, $field_options, $this); + } + } + } + /** * Returns unformatted field value * @@ -201,6 +400,33 @@ { return $this->GetDBField($this->IDField); } + + /** + * Returns parent table information + * + * @param bool $from_temp load parent item from temp table + * @return Array + */ + function getLinkedInfo() + { + $parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix'); + if ( $parent_prefix ) + { + // if this is linked table, then set id from main table + $table_info = Array( + 'TableName' => $this->Application->getUnitOption($this->Prefix,'TableName'), + 'IdField' => $this->Application->getUnitOption($this->Prefix,'IDField'), + 'ForeignKey' => $this->Application->getUnitOption($this->Prefix,'ForeignKey'), + 'ParentTableKey' => $this->Application->getUnitOption($this->Prefix,'ParentTableKey'), + 'ParentPrefix' => $parent_prefix + ); + + $main_object = $this->Application->recallObject($parent_prefix); + return array_merge($table_info, Array('ParentId'=> $main_object->GetDBField( $table_info['ParentTableKey'] ) ) ); + } + return false; + } + } ?> \ No newline at end of file