Attached Files |
category_item_optimization.patch [^] (13,857 bytes) 2011-11-01 11:04
[Show Content]
Index: kernel/db/cat_dbitem.php
===================================================================
--- kernel/db/cat_dbitem.php (revision 14705)
+++ kernel/db/cat_dbitem.php (working copy)
@@ -37,7 +37,14 @@
*/
var $usePendingEditing = false;
- function Clear($new_id = null)
+ /**
+ * Removes all data from an object
+ *
+ * @param int $new_id
+ * @return bool
+ * @access public
+ */
+ public function Clear($new_id = null)
{
parent::Clear($new_id);
$this->CategoryPath = Array();
@@ -58,31 +65,11 @@
}
/**
- * 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);
-
- if ($ret) {
- // TODO: move to OnAfterItemCreate method
- $this->assignPrimaryCategory();
- }
-
- return $ret;
- }
-
- /**
* Assigns primary category for the item
*
* @access public
*/
- function assignPrimaryCategory()
+ public function assignPrimaryCategory()
{
if ( $this->GetDBField('CategoryId') <= 0 ) {
// set primary category in item object
@@ -103,15 +90,6 @@
*/
public function Update($id = null, $system_update = false)
{
- $this->VirtualFields['ResourceId'] = Array ();
-
- 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 ) {
$this->checkFilename();
$this->generateFilename();
@@ -127,8 +105,6 @@
$this->Conn->Query($sql);
}
- unset($this->VirtualFields['ResourceId']);
-
return $ret;
}
Index: kernel/db/cat_event_handler.php
===================================================================
--- kernel/db/cat_event_handler.php (revision 14705)
+++ kernel/db/cat_event_handler.php (working copy)
@@ -999,6 +999,13 @@
$event->CallSubEvent('OnAfterItemDelete');
}
}
+
+ if ( $object->GetChangedFields() ) {
+ $now = adodb_mktime();
+ $object->SetDBField('Modified_date', $now);
+ $object->SetDBField('Modified_time', $now);
+ $object->SetDBField('ModifiedById', $this->Application->RecallVar('user_id'));
+ }
}
/**
@@ -1012,14 +1019,16 @@
{
parent::OnAfterItemCreate($event);
+ $object =& $event->getObject();
+ /* @var $object kCatDBItem */
+
if ( substr($event->Special, -6) == 'import' ) {
$this->setCustomExportColumns($event);
}
+ $object->assignPrimaryCategory();
+
if ( !$this->Application->isAdmin ) {
- $object =& $event->getObject();
- /* @var $object kCatDBItem */
-
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
@@ -2303,10 +2312,6 @@
$object->SetDBField($owner_field, $this->Application->RecallVar('user_id'));
}
- if ( $object->Validate() ) {
- $object->SetDBField('ResourceId', $this->Application->NextResourceId());
- }
-
if ( !$this->Application->isAdmin ) {
$this->setItemStatusByPermission($event);
}
@@ -2865,4 +2870,23 @@
$this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones);
}
}
+
+ /**
+ * Set's new unique resource id to user
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnAfterItemValidate(kEvent &$event)
+ {
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $resource_id = $object->GetDBField('ResourceId');
+
+ if ( !$resource_id ) {
+ $object->SetDBField('ResourceId', $this->Application->NextResourceId());
+ }
+ }
}
\ No newline at end of file
Index: units/categories/categories_event_handler.php
===================================================================
--- units/categories/categories_event_handler.php (revision 14714)
+++ units/categories/categories_event_handler.php (working copy)
@@ -1286,6 +1286,14 @@
{
parent::OnBeforeItemCreate($event);
+ $object =& $event->getObject();
+ /* @var $object CategoriesItem */
+
+ if ( $object->GetDBField('ParentId') <= 0 ) {
+ // no parent category - use current (happens during import)
+ $object->SetDBField('ParentId', $this->Application->GetVar('m_cat_id'));
+ }
+
$this->_beforeItemChange($event);
if ( $this->Application->isAdminUser || $event->Prefix == 'st' ) {
@@ -1309,9 +1317,6 @@
}
if ( $new_status ) {
- $object =& $event->getObject();
- /* @var $object kDBItem */
-
$object->SetDBField('Status', $new_status);
// don't forget to set Priority for suggested from Front-End categories
@@ -1350,14 +1355,14 @@
{
parent::OnBeforeItemUpdate($event);
+ $this->_beforeItemChange($event);
+
$object =& $event->getObject();
/* @var $object kDBItem */
if ( $object->GetChangedFields() ) {
$object->SetDBField('ModifiedById', $this->Application->RecallVar('user_id'));
}
-
- $this->_beforeItemChange($event);
}
/**
@@ -1620,8 +1625,11 @@
function _beforeItemChange(&$event)
{
$object =& $event->getObject();
- /* @var $object kDBItem */
+ /* @var $object CategoriesItem */
+ $object->checkFilename();
+ $object->generateFilename();
+
$now = adodb_mktime();
if ( !$this->Application->isDebugMode() && strpos($event->Special, 'rebuild') === false ) {
@@ -1636,6 +1644,12 @@
}
}
+ $is_admin = $this->Application->isAdminUser;
+
+ if ( (!$object->IsTempTable() && !$is_admin) || ($is_admin && !$object->GetDBField('CreatedById')) ) {
+ $object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
+ }
+
if ($object->GetChangedFields()) {
$object->SetDBField('Modified_date', $now);
$object->SetDBField('Modified_time', $now);
@@ -2071,6 +2085,31 @@
}
/**
+ * Occurs after creating item
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnAfterItemCreate(&$event)
+ {
+ parent::OnAfterItemCreate($event);
+
+ $object =& $event->getObject();
+ /* @var $object CategoriesItem */
+
+ // need to update path after category is created, so category is included in that path
+ $parent_path = $object->buildParentPath();
+
+ $sql = 'UPDATE ' . $object->TableName . '
+ SET ParentPath = ' . $this->Conn->qstr($parent_path) . '
+ WHERE CategoryId = ' . $object->GetID();
+ $this->Conn->Query($sql);
+
+ $object->SetDBField('ParentPath', $parent_path);
+ }
+
+ /**
* Enter description here...
*
* @param kEvent $event
@@ -2785,4 +2824,23 @@
return $res;
}
+
+ /**
+ * Set's new unique resource id to user
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnAfterItemValidate(kEvent &$event)
+ {
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $resource_id = $object->GetDBField('ResourceId');
+
+ if ( !$resource_id ) {
+ $object->SetDBField('ResourceId', $this->Application->NextResourceId());
+ }
+ }
}
\ No newline at end of file
Index: units/categories/categories_item.php
===================================================================
--- units/categories/categories_item.php (revision 14705)
+++ units/categories/categories_item.php (working copy)
@@ -16,82 +16,27 @@
class CategoriesItem extends kDBItem
{
-
/**
- * Creates a record in the database table with current item' values
+ * Builds parent path for this category
*
- * @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
+ * @return string
* @access public
*/
- public function Create($force_id = false, $system_create = false)
+ public function buildParentPath()
{
- // set parent category first, so filename generation could use it
- $parent_category = $this->GetDBField('ParentId') > 0 ? $this->GetDBField('ParentId') : $this->Application->GetVar('m_cat_id');
- $this->SetDBField('ParentId', $parent_category);
-
- $this->checkFilename();
- $this->generateFilename();
-
- if ($this->Validate()) {
- // TODO: such approach will not respect changes from CategoryEventHandler::OnBeforeItemCreate event
- $this->SetDBField('ResourceId', $this->Application->NextResourceId());
- }
-
- // TODO: move to CategoryEventHandler::OnBeforeItemCreate
- $is_admin = $this->Application->isAdminUser;
-
- if ((!$this->IsTempTable() && !$is_admin) || ($is_admin && !$this->GetDBField('CreatedById'))) {
- $this->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
- }
-
- $ret = parent::Create($force_id, $system_create);
-
- if ($ret) {
- // TODO: move to CategoryEventHandler::OnAfterItemCreate method
- $sql = 'UPDATE %s SET ParentPath = %s WHERE CategoryId = %s';
- $parent_path = $this->buildParentPath();
- $this->Conn->Query( sprintf($sql, $this->TableName, $this->Conn->qstr($parent_path), $this->GetID() ) );
-
- $this->SetDBField('ParentPath', $parent_path);
- }
-
- return $ret;
-
- }
-
- /**
- * 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->checkFilename();
- $this->generateFilename();
-
- return parent::Update($id, $system_update);
- }
-
- function buildParentPath()
- {
$parent_id = $this->GetDBField('ParentId');
- if ($parent_id == 0) {
+ if ( $parent_id == 0 ) {
$parent_path = '|';
}
else {
- $cat_table = $this->Application->getUnitOption($this->Prefix, 'TableName');
- $sql = 'SELECT ParentPath FROM '.$cat_table.' WHERE CategoryId = %s';
- $parent_path = $this->Conn->GetOne( sprintf($sql, $parent_id) );
+ $sql = 'SELECT ParentPath
+ FROM ' . $this->Application->getUnitOption($this->Prefix, 'TableName') . ' WHERE
+ CategoryId = ' . $parent_id;
+ $parent_path = $this->Conn->GetOne($sql);
}
- return $parent_path.$this->GetID().'|';
+ return $parent_path . $this->GetID() . '|';
}
/**
@@ -99,8 +44,9 @@
*
* @param string $string
* @return string
+ * @access protected
*/
- function stripDisallowed($string)
+ protected function stripDisallowed($string)
{
$filenames_helper =& $this->Application->recallObject('FilenamesHelper');
/* @var $filenames_helper kFilenamesHelper */
@@ -110,22 +56,22 @@
return $this->checkAutoFilename($string);
}
- function checkFilename()
+ public function checkFilename()
{
- if ($this->GetDBField('AutomaticFilename')) {
+ if ( $this->GetDBField('AutomaticFilename') ) {
// filename will be generated from scratch, don't check anything here
- return ;
+ return;
}
- elseif ($this->GetDBField('Type') == PAGE_TYPE_TEMPLATE) {
+ elseif ( $this->GetDBField('Type') == PAGE_TYPE_TEMPLATE ) {
// system page with AutomaticFilename checkbox unchecked -> compatibility with Proj-CMS <= 4.3.9 (when "/" were allowed in Filename)
- return ;
+ return;
}
$filename = $this->GetDBField('Filename');
$this->SetDBField('Filename', $this->stripDisallowed($filename));
}
- function checkAutoFilename($filename)
+ protected function checkAutoFilename($filename)
{
static $current_theme = null;
@@ -202,9 +148,9 @@
* Generate item's filename based on it's title field value
*
* @return void
- * @access protected
+ * @access public
*/
- protected function generateFilename()
+ public function generateFilename()
{
if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) {
return ;
@@ -224,25 +170,30 @@
* Allows to detect if root category being edited
*
* @return int
+ * @access public
*/
- function IsRoot()
+ public function IsRoot()
{
return $this->Application->RecallVar('IsRootCategory_'.$this->Application->GetVar('m_wid'));
}
+
/**
* Sets correct name to Home category while editing it
*
* @return bool
+ * @access public
*/
- function IsNewItem()
+ public function IsNewItem()
{
- if ($this->IsRoot() && $this->Prefix == 'c') {
+ if ( $this->IsRoot() && $this->Prefix == 'c' ) {
$title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField');
$category_name = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
$this->SetDBField($title_field, $category_name);
+
return false;
}
+
return parent::IsNewItem();
}
Index: units/users/users_event_handler.php
===================================================================
--- units/users/users_event_handler.php (revision 14705)
+++ units/users/users_event_handler.php (working copy)
@@ -552,20 +552,21 @@
* Set's new unique resource id to user
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemValidate(&$event)
+ protected function OnAfterItemValidate(kEvent &$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$resource_id = $object->GetDBField('ResourceId');
- if (!$resource_id) {
+ if ( !$resource_id ) {
$object->SetDBField('ResourceId', $this->Application->NextResourceId());
}
}
-
/**
* Enter description here...
*
2011-11-04_2053.png [^] (269,099 bytes) 2011-11-07 03:57
category_pasting_produced_sql_error.patch [^] (1,426 bytes) 2011-11-07 03:57
[Show Content]
Index: kernel/db/cat_event_handler.php
===================================================================
--- kernel/db/cat_event_handler.php (revision 14733)
+++ kernel/db/cat_event_handler.php (working copy)
@@ -2282,10 +2282,12 @@
{
parent::OnBeforeClone($event);
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $object->SetDBField('ResourceId', 0); // this will reset it
+
if ( $this->Application->GetVar('ResetCatBeforeClone') ) {
- $object =& $event->getObject();
- /* @var $object kDBItem */
-
$object->SetDBField('CategoryId', null);
}
}
Index: units/categories/categories_event_handler.php
===================================================================
--- units/categories/categories_event_handler.php (revision 14733)
+++ units/categories/categories_event_handler.php (working copy)
@@ -2843,4 +2843,23 @@
$object->SetDBField('ResourceId', $this->Application->NextResourceId());
}
}
+
+ /**
+ * Occurs before an item has been cloned
+ * Id of newly created item is passed as event' 'id' param
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnBeforeClone(&$event)
+ {
+ parent::OnBeforeClone($event);
+
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $object->SetDBField('ResourceId', 0); // this will reset it
+
+ }
}
\ No newline at end of file
duplicate_category_resourceid_on_import_v2.patch [^] (1,471 bytes) 2011-12-30 06:09
[Show Content]
Index: units/helpers/cat_dbitem_export_helper.php
===================================================================
--- units/helpers/cat_dbitem_export_helper.php (revision 14949)
+++ units/helpers/cat_dbitem_export_helper.php (working copy)
@@ -829,13 +829,17 @@
WHERE ('.$lang_prefix.'Name = '.$this->Conn->qstr($category_name).') AND (ParentId = '.(int)$current_category_id.')';
$category_id = $this->Conn->GetOne($sql);
- if ($category_id === false) {
+ if ( $category_id === false ) {
// category not in db -> create
- $category_fields = Array( $lang_prefix.'Name' => $category_name, $lang_prefix.'Description' => $category_name,
- 'Status' => STATUS_ACTIVE, 'ParentId' => $current_category_id, 'AutomaticFilename' => 1
- );
+ $category_fields = Array (
+ $lang_prefix.'Name' => $category_name, $lang_prefix.'Description' => $category_name,
+ 'Status' => STATUS_ACTIVE, 'ParentId' => $current_category_id, 'AutomaticFilename' => 1
+ );
+
+ $this->dummyCategory->Clear();
$this->dummyCategory->SetDBFieldsFromHash($category_fields);
- if ($this->dummyCategory->Create()) {
+
+ if ( $this->dummyCategory->Create() ) {
$category_id = $this->dummyCategory->GetID();
$this->addToCache('category_parent_path', $category_id, $this->dummyCategory->GetDBField('ParentPath'));
$this->addToCache('category_names', $category_key, $category_id);
ProductImportCategoryIssue.png [^] (64,964 bytes) 2011-12-30 06:12
|