Index: trunk/kernel/units/general/cat_dbitem.php =================================================================== diff -u -N -r5558 -r6093 --- trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 5558) +++ trunk/kernel/units/general/cat_dbitem.php (.../cat_dbitem.php) (revision 6093) @@ -16,20 +16,40 @@ */ var $useFilenames = true; + /** + * Use pending editing abilities during item (delegated by permissions) + * + * @var bool + */ + var $usePendingEditing = false; + function Clear() { parent::Clear(); $this->CategoryPath = Array(); } + /** + * Set's prefix and special + * + * @param string $prefix + * @param string $special + * @access public + */ + function Init($prefix, $special, $event_params = null) + { + parent::Init($prefix, $special, $event_params); + $this->usePendingEditing = $this->Application->getUnitOption($this->Prefix, 'UsePendingEditing'); + } + function Create($force_id=false, $system_create=false) { if (!$this->Validate()) return false; $this->SetDBField('ResourceId', $this->Application->NextResourceId()); $this->SetDBField('Modified', adodb_mktime() ); - if ($this->mode != 't') { + if ($this->mode != 't' && !$this->Application->IsAdmin()) { $this->SetDBField('CreatedById', $this->Application->GetVar('u_id')); } @@ -39,18 +59,15 @@ } $ret = parent::Create(); - if($ret) - { - if ( $this->Application->IsTempTable($this->TableName) ) { - $table = $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems'); - } - else { - $table = TABLE_PREFIX.'CategoryItems'; - } - $cat_id = $this->Application->GetVar('m_cat_id'); - $query = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat,ItemPrefix,Filename) - VALUES ('.$cat_id.','.$this->GetField('ResourceId').',1,'.$this->Conn->qstr($this->Prefix).','.$this->Conn->qstr($this->GetDBField('Filename')).')'; - $this->Conn->Query($query); + if ($ret) { + $fields_hash = Array( + 'CategoryId' => $this->Application->GetVar('m_cat_id'), + 'ItemResourceId' => $this->GetField('ResourceId'), + 'PrimaryCat' => 1, + 'ItemPrefix' => $this->Prefix, + 'Filename' => $this->GetDBField('Filename'), + ); + $this->Conn->doInsert($fields_hash, $this->CategoryItemsTable()); } return $ret; } @@ -69,14 +86,33 @@ $ret = parent::Update($id, $system_update); if ($ret) { - $table = $this->Application->IsTempTable($this->TableName) ? $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems') : TABLE_PREFIX.'CategoryItems'; - $this->Conn->Query('UPDATE '.$table.' SET Filename = '.$this->Conn->qstr($this->GetDBField('Filename')).' WHERE ItemResourceId = '.$this->GetDBField('ResourceId')); + $filename = $this->useFilenames ? $this->GetDBField('Filename') : ''; + $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; } + /** + * Returns CategoryItems table based on current item mode (temp/live) + * + * @return string + */ + function CategoryItemsTable() + { + $table = TABLE_PREFIX.'CategoryItems'; + if ($this->Application->IsTempTable($this->TableName)) { + $table = $this->Application->GetTempName($table); + } + + return $table; + } + + function checkFilename() { if( !$this->GetDBField('AutomaticFilename') ) @@ -146,7 +182,7 @@ } // We need to delete CategoryItems record when deleting product - function Delete($id=null) + function Delete($id = null) { if( isset($id) ) { $this->setID($id); @@ -155,7 +191,8 @@ $ret = parent::Delete(); if ($ret) { - $query = 'DELETE FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$this->GetDBField('ResourceId'); + $query = ' DELETE FROM '.$this->CategoryItemsTable().' + WHERE ItemResourceId = '.$this->GetDBField('ResourceId'); $this->Conn->Query($query); } @@ -234,6 +271,10 @@ if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) return false; $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); + 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]; + } $name = $this->stripDisallowed( $this->GetDBField($title_field) ); if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name); @@ -306,7 +347,8 @@ } else { $sql = 'INSERT INTO '.$table.' (CategoryId,ItemResourceId,PrimaryCat,ItemPrefix,Filename) VALUES (%s,%s,%s,%s,%s)'; - $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0, $this->Conn->qstr($this->Prefix), $this->Conn->qstr($this->GetDBField('Filename'))) ); + $filename = $this->useFilenames ? $this->GetDBField('Filename') : ''; // because some prefixes does not use filenames + $this->Conn->Query( sprintf($sql, $category_id, $this->GetDBField('ResourceId'), $is_primary ? 1 : 0, $this->Conn->qstr($this->Prefix), $this->Conn->qstr($filename)) ); } // 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! @@ -358,6 +400,61 @@ } return $image_data; } + + function ChangeStatus($new_status) + { + $status_field = array_shift( $this->Application->getUnitOption($this->Prefix,'StatusField') ); + $this->SetDBField($status_field, $new_status); + return $this->Update(); + } + + /** + * Approves changes made to category item + * + * @return bool + */ + function ApproveChanges() + { + $original_id = $this->GetDBField('OrgId'); + + 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))) { + // 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)); + + $this->SetDBField('OrgId', 0); + return $this->ChangeStatus(STATUS_ACTIVE); + } + + return false; + } + + /** + * Decline changes made to category item + * + * @return bool + */ + function DeclineChanges() + { + $original_id = $this->GetDBField('OrgId'); + + 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())); + + // original item is not changed here, because it is already enabled (thrus pending copy is visible to item's owner or admin with permission) + return true; + } } ?> \ No newline at end of file