Index: branches/5.2.x/core/kernel/db/cat_dbitem.php =================================================================== diff -u -N -r14585 -r14596 --- branches/5.2.x/core/kernel/db/cat_dbitem.php (.../cat_dbitem.php) (revision 14585) +++ branches/5.2.x/core/kernel/db/cat_dbitem.php (.../cat_dbitem.php) (revision 14596) @@ -1,6 +1,6 @@ usePendingEditing = $this->Application->getUnitOption($this->Prefix, 'UsePendingEditing'); } - function Create($force_id = false, $system_create = false) + /** + * 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! + * @param bool $system_create + * @return bool + * @access public + */ + public function Create($force_id = false, $system_create = false) { $ret = parent::Create($force_id, $system_create); @@ -84,33 +92,43 @@ $this->assignToCategory($this->GetDBField('CategoryId'), true); } - function Update($id=null, $system_update=false) + /** + * Updates previously loaded record with current item' values + * + * @access public + * @param int $id Primary Key Id to update + * @param bool $system_update + * @return bool + * @access public + */ + public function Update($id = null, $system_update = false) { - $this->VirtualFields['ResourceId'] = Array(); + $this->VirtualFields['ResourceId'] = Array (); - if ($this->GetChangedFields()) { + if ( $this->GetChangedFields() ) { $now = adodb_mktime(); $this->SetDBField('Modified_date', $now); $this->SetDBField('Modified_time', $now); $this->SetDBField('ModifiedById', $this->Application->RecallVar('user_id')); } - if ($this->useFilenames) { + if ( $this->useFilenames ) { $this->checkFilename(); $this->generateFilename(); } $ret = parent::Update($id, $system_update); - if ($ret) { + if ( $ret ) { $filename = $this->useFilenames ? (string)$this->GetDBField('Filename') : ''; - $sql = 'UPDATE '.$this->CategoryItemsTable().' - SET Filename = '.$this->Conn->qstr($filename).' - WHERE ItemResourceId = '.$this->GetDBField('ResourceId'); + $sql = 'UPDATE ' . $this->CategoryItemsTable() . ' + SET Filename = ' . $this->Conn->qstr($filename) . ' + WHERE ItemResourceId = ' . $this->GetDBField('ResourceId'); $this->Conn->Query($sql); } unset($this->VirtualFields['ResourceId']); + return $ret; } @@ -228,18 +246,19 @@ * * @param int $id * @return bool + * @access public */ - function Delete($id = null) + public function Delete($id = null) { - if( isset($id) ) { + if ( isset($id) ) { $this->setID($id); } $this->Load($this->GetID()); $ret = parent::Delete(); - if ($ret) { + if ( $ret ) { // TODO: move to OnAfterItemDelete method $query = ' DELETE FROM ' . $this->CategoryItemsTable() . ' WHERE ItemResourceId = ' . $this->GetDBField('ResourceId'); @@ -259,32 +278,43 @@ { $id_field = $this->Application->getUnitOption($this->Prefix, 'IDField'); // because item was loaded before by ResourceId - $ci_table = $this->Application->getUnitOption($this->Prefix.'-ci', 'TableName'); + $ci_table = $this->Application->getUnitOption($this->Prefix . '-ci', 'TableName'); $resource_id = $this->GetDBField('ResourceId'); - $item_cats_sql = 'SELECT CategoryId FROM %s WHERE ItemResourceId = %s'; - $delete_category_items_sql = 'DELETE FROM %s WHERE ItemResourceId = %s AND CategoryId IN (%s)'; + $item_cats_sql = ' SELECT CategoryId + FROM %s + WHERE ItemResourceId = %s'; + $delete_category_items_sql = ' DELETE FROM %s + WHERE ItemResourceId = %s AND CategoryId IN (%s)'; + $category_ids = $this->Conn->GetCol( sprintf($item_cats_sql, $ci_table, $resource_id) ); $cats_left = array_diff($category_ids, $delete_category_ids); - if(!$cats_left) - { - $sql = 'SELECT %s FROM %s WHERE ResourceId = %s'; - $ids = $this->Conn->GetCol( sprintf($sql, $id_field, $this->TableName, $resource_id) ); - $temp =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $temp->DeleteItems($this->Prefix, $this->Special, $ids); + if ( !$cats_left ) { + $sql = 'SELECT %s + FROM %s + WHERE ResourceId = %s'; + $ids = $this->Conn->GetCol(sprintf($sql, $id_field, $this->TableName, $resource_id)); + + $temp_handler =& $this->Application->recallObject($this->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + + $temp_handler->DeleteItems($this->Prefix, $this->Special, $ids); } - else - { - $this->Conn->Query( sprintf($delete_category_items_sql, $ci_table, $resource_id, implode(',', $delete_category_ids) ) ); + else { + $this->Conn->Query( sprintf($delete_category_items_sql, $ci_table, $resource_id, implode(',', $delete_category_ids)) ); - $sql = 'SELECT CategoryId FROM %s WHERE PrimaryCat = 1 AND ItemResourceId = %s'; - $primary_cat_id = $this->Conn->GetCol( sprintf($sql, $ci_table, $resource_id) ); - if( count($primary_cat_id) == 0 ) - { - $sql = 'UPDATE %s SET PrimaryCat = 1 WHERE (CategoryId = %s) AND (ItemResourceId = %s)'; - $this->Conn->Query( sprintf($sql, $ci_table, reset($cats_left), $resource_id ) ); + $sql = 'SELECT CategoryId + FROM %s + WHERE PrimaryCat = 1 AND ItemResourceId = %s'; + $primary_cat_id = $this->Conn->GetCol(sprintf($sql, $ci_table, $resource_id)); + + if ( count($primary_cat_id) == 0 ) { + $sql = 'UPDATE %s + SET PrimaryCat = 1 + WHERE (CategoryId = %s) AND (ItemResourceId = %s)'; + $this->Conn->Query( sprintf($sql, $ci_table, reset($cats_left), $resource_id) ); } } } @@ -298,6 +328,8 @@ function stripDisallowed($filename) { $filenames_helper =& $this->Application->recallObject('FilenamesHelper'); + /* @var $filenames_helper kFilenamesHelper */ + $table = $this->IsTempTable() ? $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems', 'prefix:'.$this->Prefix) : TABLE_PREFIX.'CategoryItems'; return $filenames_helper->stripDisallowed($table, 'ItemResourceId', $this->GetDBField('ResourceId'), $filename); @@ -314,85 +346,80 @@ /** * Generate item's filename based on it's title field value * - * @return string + * @return void + * @access protected */ - function generateFilename() + protected function generateFilename() { - if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) return false; + if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) { + return ; + } $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); - if (preg_match('/l([\d]+)_(.*)/', $title_field, $regs)) { + + if ( preg_match('/l([\d]+)_(.*)/', $title_field, $regs) ) { // if title field is multilingual, then use it's name from primary language - $title_field = 'l'.$this->Application->GetDefaultLanguageId().'_'.$regs[2]; + $title_field = 'l' . $this->Application->GetDefaultLanguageId() . '_' . $regs[2]; } + $name = $this->stripDisallowed( $this->GetDBField($title_field) ); - if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name); - } - - /** - * Check if value is set for required field - * - * @param string $field field name - * @param Array $params field options from config - * @return bool - * @access private - */ - function ValidateRequired($field, $params) - { - $res = true; - if (getArrayValue($params, 'required')) { - $res = ( (string) $this->FieldValues[$field] != ''); + if ( $name != $this->GetDBField('Filename') ) { + $this->SetDBField('Filename', $name); } - if (!$res) { - $this->SetError($field, 'required'); - } - return $res; } /** * Adds item to other category * * @param int $category_id * @param bool $is_primary + * @return void + * @access public */ - function assignToCategory($category_id, $is_primary = false) + public function assignToCategory($category_id, $is_primary = false) { $table = $this->CategoryItemsTable(); - $key_clause = '(ItemResourceId = '.$this->GetDBField('ResourceId').')'; + $key_clause = '(ItemResourceId = ' . $this->GetDBField('ResourceId') . ')'; - // get all cateories, where item is in - $sql = 'SELECT PrimaryCat, CategoryId FROM '.$table.' WHERE '.$key_clause; + // get all categories, where item is in + $sql = 'SELECT PrimaryCat, CategoryId + FROM ' . $table . ' + WHERE ' . $key_clause; $item_categories = $this->Conn->GetCol($sql, 'CategoryId'); - if (!$item_categories) { - $item_categories = Array(); - $primary_found = false; - } - // find primary category - foreach ($item_categories as $item_category_id => $primary_found) { - if ($primary_found) { - break; + $primary_found = $item_category_id = false; + + if ( $item_categories ) { + // find primary category + foreach ($item_categories as $item_category_id => $primary_found) { + if ( $primary_found ) { + break; + } } } - if ($primary_found && ($item_category_id == $category_id) && !$is_primary) { + if ( $primary_found && ($item_category_id == $category_id) && !$is_primary ) { // want to make primary category as non-primary :( - return true; + return; } - else if (!$primary_found) { + elseif ( !$primary_found ) { $is_primary = true; } - if ($is_primary && $item_categories) { + if ( $is_primary && $item_categories ) { // reset primary mark from all other categories - $sql = 'UPDATE '.$table.' SET PrimaryCat = 0 WHERE '.$key_clause; + $sql = 'UPDATE ' . $table . ' + SET PrimaryCat = 0 + WHERE ' . $key_clause; $this->Conn->Query($sql); } // UPDATE & INSERT instead of REPLACE because CategoryItems table has no primary key defined in database - if (isset($item_categories[$category_id])) { - $sql = 'UPDATE '.$table.' SET PrimaryCat = '.($is_primary ? 1 : 0).' WHERE '.$key_clause.' AND (CategoryId = '.$category_id.')'; + if ( isset($item_categories[$category_id]) ) { + $sql = 'UPDATE ' . $table . ' + SET PrimaryCat = ' . ($is_primary ? 1 : 0) . ' + WHERE ' . $key_clause . ' AND (CategoryId = ' . $category_id . ')'; $this->Conn->Query($sql); } else { @@ -403,8 +430,10 @@ 'ItemPrefix' => $this->Prefix, 'Filename' => $this->useFilenames ? (string)$this->GetDBField('Filename') : '', // because some prefixes does not use filenames, ); + $this->Conn->doInsert($fields_hash, $table); } + // to ensure filename update after adding to another category // this is critical since there may be an item with same filename in newly added category! $this->Update(); @@ -493,16 +522,18 @@ { $original_id = $this->GetDBField('OrgId'); - if (!($this->usePendingEditing && $original_id)) { + if ( !($this->usePendingEditing && $original_id) ) { // non-pending copy of original link return $this->ChangeStatus(STATUS_ACTIVE); } - if ($this->raiseEvent('OnBeforeDeleteOriginal', null, Array('original_id' => $original_id))) { + if ( $this->raiseEvent('OnBeforeDeleteOriginal', null, Array ('original_id' => $original_id)) ) { // delete original item, because changes made in pending copy (this item) got to be approved in this method - $temp_handler =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $temp_handler->DeleteItems($this->Prefix, $this->Special, Array($original_id)); + $temp_handler =& $this->Application->recallObject($this->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + $temp_handler->DeleteItems($this->Prefix, $this->Special, Array ($original_id)); + $this->SetDBField('OrgId', 0); return $this->ChangeStatus(STATUS_ACTIVE, true); } @@ -519,14 +550,16 @@ { $original_id = $this->GetDBField('OrgId'); - if (!($this->usePendingEditing && $original_id)) { + if ( !($this->usePendingEditing && $original_id) ) { // non-pending copy of original link return $this->ChangeStatus(STATUS_DISABLED); } // delete this item, because changes made in pending copy (this item) will be declined in this method - $temp_handler =& $this->Application->recallObject($this->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $temp_handler->DeleteItems($this->Prefix, $this->Special, Array($this->GetID())); + $temp_handler =& $this->Application->recallObject($this->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + + $temp_handler->DeleteItems($this->Prefix, $this->Special, Array ($this->GetID())); $this->sendEmailEvents(STATUS_DISABLED, true); // original item is not changed here, because it is already enabled (thrus pending copy is visible to item's owner or admin with permission) @@ -566,17 +599,17 @@ } /** - * 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() - */ - function GetKeyClause($method = null, $keys_hash = null) + * Returns part of SQL WHERE clause identifying the record, ex. id = 25 + * + * @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 + * @see kDBItem::Load() + * @see kDBItem::Update() + * @see kDBItem::Delete() + * @return string + * @access protected + */ + protected function GetKeyClause($method = null, $keys_hash = null) { if ($method == 'load') { // for item with many categories makes primary to load