Index: branches/5.2.x/core/kernel/db/cat_event_handler.php =================================================================== diff -u -N -r14585 -r14628 --- branches/5.2.x/core/kernel/db/cat_event_handler.php (.../cat_event_handler.php) (revision 14585) +++ branches/5.2.x/core/kernel/db/cat_event_handler.php (.../cat_event_handler.php) (revision 14628) @@ -1,6 +1,6 @@ getObject(); + /* @var $object kDBItem */ + $id = $this->getPassedID($event); - if ($object->Load($id)) { + + if ( $object->Load($id) ) { $actions =& $this->Application->recallObject('kActions'); - $actions->Set($event->getPrefixSpecial().'_id', $object->GetID() ); + /* @var $actions Params */ + $actions->Set($event->getPrefixSpecial() . '_id', $object->GetID()); + $use_pending_editing = $this->Application->getUnitOption($event->Prefix, 'UsePendingEditing'); - if ($use_pending_editing && $event->Special != 'original') { - $this->Application->SetVar($event->Prefix.'.original_id', $object->GetDBField('OrgId')); + + if ( $use_pending_editing && $event->Special != 'original' ) { + $this->Application->SetVar($event->Prefix . '.original_id', $object->GetDBField('OrgId')); } } else { @@ -62,11 +70,13 @@ } /** - * Checks permissions of user + * Checks user permission to execute given $event * * @param kEvent $event + * @return bool + * @access public */ - function CheckPermission(&$event) + public function CheckPermission(&$event) { if (!$this->Application->isAdmin) { if ($event->Name == 'OnSetSortingDirect') { @@ -204,11 +214,16 @@ * Add selected items to clipboard with mode = COPY (CLONE) * * @param kEvent $event + * @return void + * @access protected */ - function OnCopy(&$event) + protected function OnCopy(&$event) { $this->Application->RemoveVar('clipboard'); + $clipboard_helper =& $this->Application->recallObject('ClipboardHelper'); + /* @var $clipboard_helper kClipboardHelper */ + $clipboard_helper->setClipboard($event, 'copy', $this->StoreSelectedIDs($event)); $this->clearSelectedIDs($event); } @@ -217,11 +232,15 @@ * Add selected items to clipboard with mode = CUT * * @param kEvent $event + * @return void + * @access protected */ - function OnCut(&$event) + protected function OnCut(&$event) { $this->Application->RemoveVar('clipboard'); $clipboard_helper =& $this->Application->recallObject('ClipboardHelper'); + /* @var $clipboard_helper kClipboardHelper */ + $clipboard_helper->setClipboard($event, 'cut', $this->StoreSelectedIDs($event)); $this->clearSelectedIDs($event); } @@ -250,30 +269,33 @@ * Performs category item paste * * @param kEvent $event + * @return void + * @access protected */ - function OnPaste(&$event) + protected function OnPaste(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) || !$this->_checkPastePermission($event)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) || !$this->_checkPastePermission($event) ) { $event->status = kEvent::erFAIL; return; } $clipboard_data = $event->getEventParam('clipboard_data'); - if (!$clipboard_data['cut'] && !$clipboard_data['copy']) { - return false; + if ( !$clipboard_data['cut'] && !$clipboard_data['copy'] ) { + return; } - if ($clipboard_data['copy']) { - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + if ( $clipboard_data['copy'] ) { + $temp =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); /* @var $temp kTempTablesHandler */ $this->Application->SetVar('ResetCatBeforeClone', 1); // used in "kCatDBEventHandler::OnBeforeClone" $temp->CloneItems($event->Prefix, $event->Special, $clipboard_data['copy']); } - if ($clipboard_data['cut']) { - $object =& $this->Application->recallObject($event->getPrefixSpecial().'.item', $event->Prefix, Array('skip_autoload' => true)); + if ( $clipboard_data['cut'] ) { + $object =& $this->Application->recallObject($event->getPrefixSpecial() . '.item', $event->Prefix, Array ('skip_autoload' => true)); + /* @var $object kCatDBItem */ foreach ($clipboard_data['cut'] as $id) { $object->Load($id); @@ -288,44 +310,54 @@ * by calling its Delete method if sub-item has AutoDelete set to true in its config file * * @param kEvent $event + * @return void + * @access protected */ - function OnMassDelete(&$event) + protected function OnMassDelete(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; return; } - $event->status=kEvent::erSUCCESS; - $ids = $this->StoreSelectedIDs($event); - $to_delete = array(); - if ($recycle_bin = $this->Application->ConfigValue('RecycleBinFolder')) { - $rb =& $this->Application->recallObject('c.recycle', null, array('skip_autoload' => true)); + $to_delete = Array (); + + if ( $recycle_bin = $this->Application->ConfigValue('RecycleBinFolder') ) { + $rb =& $this->Application->recallObject('c.recycle', null, array ('skip_autoload' => true)); + /* @var $rb CategoriesItem */ + $rb->Load($recycle_bin); - $object =& $this->Application->recallObject($event->Prefix.'.recycleitem', null, Array ('skip_autoload' => true)); + + $object =& $this->Application->recallObject($event->Prefix . '.recycleitem', null, Array ('skip_autoload' => true)); + /* @var $object kCatDBItem */ + foreach ($ids as $id) { $object->Load($id); - if (preg_match('/^'.preg_quote($rb->GetDBField('ParentPath'),'/').'/', $object->GetDBField('ParentPath'))) { + + if ( preg_match('/^' . preg_quote($rb->GetDBField('ParentPath'), '/') . '/', $object->GetDBField('ParentPath')) ) { $to_delete[] = $id; continue; } + $object->MoveToCat($recycle_bin); } + $ids = $to_delete; } - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ $event->setEventParam('ids', $ids); $this->customProcessing($event, 'before'); $ids = $event->getEventParam('ids'); - if($ids) - { - $temp->DeleteItems($event->Prefix, $event->Special, $ids); + if ( $ids ) { + $temp_handler->DeleteItems($event->Prefix, $event->Special, $ids); } + $this->clearSelectedIDs($event); } @@ -464,6 +496,8 @@ } $p_item =& $this->Application->recallObject($related_prefix.'.current', null, Array('skip_autoload' => true)); + /* @var $p_item kCatDBItem */ + $p_item->Load( (int)$id ); $p_resource_id = $p_item->GetDBField('ResourceId'); @@ -542,11 +576,14 @@ } /** - * Apply filters to list + * Apply any custom changes to list's sql query * * @param kEvent $event + * @return void + * @access protected + * @see kDBEventHandler::OnListBuild() */ - function SetCustomQuery(&$event) + protected function SetCustomQuery(&$event) { parent::SetCustomQuery($event); @@ -697,17 +734,21 @@ /** * Adds calculates fields for item statuses * - * @param kCatDBItem $object + * @param kDBItem|kDBList $object * @param kEvent $event + * @return void + * @access protected */ - function prepareObject(&$object, &$event) + protected function prepareObject(&$object, &$event) { $this->prepareItemStatuses($event); $object->addCalculatedField('CachedNavbar', 'l'.$this->Application->GetVar('m_lang').'_CachedNavbar'); if ($event->Special == 'export' || $event->Special == 'import') { $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + /* @var $export_helper kCatDBItemExportHelper */ + $export_helper->prepareExportColumns($event); } } @@ -763,24 +804,32 @@ } - function CalculateHotLimit(&$event) + /** + * Calculates hot limit for current item's table + * + * @param kEvent $event + * @return float + * @access protected + */ + protected function CalculateHotLimit(&$event) { $property_map = $this->Application->getUnitOption($event->Prefix, 'ItemPropertyMappings'); - if (!$property_map) { - return; + if ( !$property_map ) { + return 0.00; } $click_field = $property_map['ClickField']; $last_hot = $this->Application->ConfigValue($property_map['MaxHotNumber']) - 1; - $sql = 'SELECT '.$click_field.' FROM '.$this->Application->getUnitOption($event->Prefix, 'TableName').' - ORDER BY '.$click_field.' DESC - LIMIT '.$last_hot.', 1'; + $sql = 'SELECT ' . $click_field . ' + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + ORDER BY ' . $click_field . ' DESC + LIMIT ' . $last_hot . ', 1'; $res = $this->Conn->GetCol($sql); $hot_limit = (double)array_shift($res); - if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) { + if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) { $serial_name = $this->Application->incrementCacheSerial($event->Prefix, null, false); $this->Application->setCache($property_map['HotLimit'] . '[%' . $serial_name . '%]', $hot_limit); } @@ -795,8 +844,10 @@ * Moves item to preferred category, updates item hits * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeItemUpdate(&$event) + protected function OnBeforeItemUpdate(&$event) { parent::OnBeforeItemUpdate($event); @@ -805,62 +856,74 @@ // update hits field $property_map = $this->Application->getUnitOption($event->Prefix, 'ItemPropertyMappings'); - if ($property_map) { + if ( $property_map ) { $click_field = $property_map['ClickField']; - if( $this->Application->isAdminUser && ($this->Application->GetVar($click_field.'_original') !== false) && - floor($this->Application->GetVar($click_field.'_original')) != $object->GetDBField($click_field) ) - { - $sql = 'SELECT MAX('.$click_field.') FROM '.$this->Application->getUnitOption($event->Prefix, 'TableName').' - WHERE FLOOR('.$click_field.') = '.$object->GetDBField($click_field); - $hits = ( $res = $this->Conn->GetOne($sql) ) ? $res + 0.000001 : $object->GetDBField($click_field); + if ( $this->Application->isAdminUser && ($this->Application->GetVar($click_field . '_original') !== false) && floor($this->Application->GetVar($click_field . '_original')) != $object->GetDBField($click_field) ) { + $sql = 'SELECT MAX(' . $click_field . ') + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + WHERE FLOOR(' . $click_field . ') = ' . $object->GetDBField($click_field); + $hits = ($res = $this->Conn->GetOne($sql)) ? $res + 0.000001 : $object->GetDBField($click_field); + $object->SetDBField($click_field, $hits); } } // change category $target_category = $object->GetDBField('CategoryId'); - if ($object->GetOriginalField('CategoryId') != $target_category) { + if ( $object->GetOriginalField('CategoryId') != $target_category ) { $object->MoveToCat($target_category); } } /** - * Load price from temp table if product mode is temp table + * Occurs after loading item, 'id' parameter + * allows to get id of item that was loaded * * @param kEvent $event + * @return void + * @access protected */ - function OnAfterItemLoad(&$event) + protected function OnAfterItemLoad(&$event) { + parent::OnAfterItemLoad($event); + $special = substr($event->Special, -6); $object =& $event->getObject(); /* @var $object kCatDBItem */ - if ($special == 'import' || $special == 'export') { + if ( $special == 'import' || $special == 'export' ) { $image_data = $object->getPrimaryImageData(); - if ($image_data) { + if ( $image_data ) { $thumbnail_image = $image_data[$image_data['LocalThumb'] ? 'ThumbPath' : 'ThumbUrl']; - if ($image_data['SameImages']) { + if ( $image_data['SameImages'] ) { $full_image = ''; } else { $full_image = $image_data[$image_data['LocalImage'] ? 'LocalPath' : 'Url']; } + $object->SetDBField('ThumbnailImage', $thumbnail_image); $object->SetDBField('FullImage', $full_image); $object->SetDBField('ImageAlt', $image_data['AltName']); } } - // substituiting pending status value for pending editing - if ($object->HasField('OrgId') && $object->GetDBField('OrgId') > 0 && $object->GetDBField('Status') == -2) { - $options = $object->GetFieldOption('Status', 'options'); + // substituting pending status value for pending editing + if ( $object->HasField('OrgId') && $object->GetDBField('OrgId') > 0 && $object->GetDBField('Status') == -2 ) { + $new_options = Array (); + $options = $object->GetFieldOption('Status', 'options', false, Array ()); + foreach ($options as $key => $val) { - if ($key == 2) $key = -2; + if ( $key == 2 ) { + $key = -2; + } + $new_options[$key] = $val; } + $object->SetFieldOption('Status', 'options', $new_options); } @@ -878,23 +941,29 @@ $file_helper->LoadItemFiles($object); } - if ( array_key_exists('MoreCategories', $object->VirtualFields) ) { + if ( $object->isVirtualField('MoreCategories') ) { // set item's additional categories to virtual field (used in editing) $item_categories = $this->getItemCategories($object->GetDBField('ResourceId')); - $object->SetDBField('MoreCategories', $item_categories ? '|'.implode('|', $item_categories).'|' : ''); + $object->SetDBField('MoreCategories', $item_categories ? '|' . implode('|', $item_categories) . '|' : ''); } } + /** + * Occurs after updating item + * + * @param kEvent $event + * @access public + */ function OnAfterItemUpdate(&$event) { $this->CalculateHotLimit($event); - if ( substr($event->Special, -6) == 'import') { + if ( substr($event->Special, -6) == 'import' ) { $this->setCustomExportColumns($event); } $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ if ( !$this->Application->isAdmin ) { $image_helper =& $this->Application->recallObject('ImageHelper'); @@ -909,40 +978,44 @@ // process file upload in virtual fields $file_helper->SaveItemFiles($object); - if ($event->Special != '-item') { + if ( $event->Special != '-item' ) { // don't touch categories during cloning $this->processAdditionalCategories($object, 'update'); } } $recycle_bin = $this->Application->ConfigValue('RecycleBinFolder'); - if ($this->Application->isAdminUser && $recycle_bin) { + if ( $this->Application->isAdminUser && $recycle_bin ) { $sql = 'SELECT CategoryId FROM ' . $this->Application->getUnitOption('ci', 'TableName') . ' WHERE ItemResourceId = ' . $object->GetDBField('ResourceId') . ' AND PrimaryCat = 1'; $primary_category = $this->Conn->GetOne($sql); - if ($primary_category == $recycle_bin) { + if ( $primary_category == $recycle_bin ) { $event->CallSubEvent('OnAfterItemDelete'); } } } /** - * sets values for import process + * Sets values for import process * * @param kEvent $event + * @return void + * @access protected */ - function OnAfterItemCreate(&$event) + protected function OnAfterItemCreate(&$event) { - if ( substr($event->Special, -6) == 'import') { + parent::OnAfterItemCreate($event); + + if ( substr($event->Special, -6) == 'import' ) { $this->setCustomExportColumns($event); } if ( !$this->Application->isAdmin ) { $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $image_helper =& $this->Application->recallObject('ImageHelper'); /* @var $image_helper ImageHelper */ @@ -956,7 +1029,7 @@ // process file upload in virtual fields $file_helper->SaveItemFiles($object); - if ($event->Special != '-item') { + if ( $event->Special != '-item' ) { // don't touch categories during cloning $this->processAdditionalCategories($object, 'create'); } @@ -1001,6 +1074,8 @@ $keywords = kUtil::unhtmlentities( trim($this->Application->GetVar('keywords')) ); $query_object =& $this->Application->recallObject('HTTPQuery'); + /* @var $query_object kHTTPQuery */ + $sql = 'SHOW TABLES LIKE "'.$search_table.'"'; if(!isset($query_object->Get['keywords']) && @@ -1054,12 +1129,11 @@ $search_config_map = Array(); foreach ($field_list as $key => $field) { - $options = $object->getFieldOptions($field); $local_table = TABLE_PREFIX.$search_config[$field]['TableName']; $weight_sum += $search_config[$field]['Priority']; // counting weight sum; used when making relevance clause // processing multilingual fields - if (getArrayValue($options, 'formatter') == 'kMultiLanguage') { + if ( $object->GetFieldOption($field, 'formatter') == 'kMultiLanguage' ) { $field_list[$key.'_primary'] = 'l'.$this->Application->GetDefaultLanguageId().'_'.$field; $field_list[$key] = 'l'.$lang.'_'.$field; @@ -1289,9 +1363,11 @@ function OnAdvancedSearch(&$event) { $query_object =& $this->Application->recallObject('HTTPQuery'); - if(!isset($query_object->Post['andor'])) - { - return; // used when navigating by pages or changing sorting in search results + /* @var $query_object kHTTPQuery */ + + if ( !isset($query_object->Post['andor']) ) { + // used when navigating by pages or changing sorting in search results + return; } $this->Application->RemoveVar('keywords'); @@ -1305,7 +1381,10 @@ $search_config = $this->Conn->Query($sql); $lang = $this->Application->GetVar('m_lang'); + $object =& $event->getObject(); + /* @var $object kDBList */ + $object->SetPage(1); $items_table = $this->Application->getUnitOption($event->Prefix, 'TableName'); @@ -1342,13 +1421,11 @@ $condition_mode = 'WHERE'; // field processing - - $options = $object->getFieldOptions($field); $local_table = TABLE_PREFIX.$record['TableName']; $weight_sum += $record['Priority']; // counting weight sum; used when making relevance clause // processing multilingual fields - if (getArrayValue($options, 'formatter') == 'kMultiLanguage') { + if ( $object->GetFieldOption($field, 'formatter') == 'kMultiLanguage' ) { $field_name = 'l'.$lang.'_'.$field; } else { @@ -1644,8 +1721,20 @@ return $condition; } - function getHuman($type, $search_data) + /** + * Returns human readable representation of searched data to be placed in search log + * @param string $type + * @param Array $search_data + * @return string + * @access protected + */ + protected function getHuman($type, $search_data) { + // all 3 variables are retrieved from $search_data array + /* @var $search_config Array */ + /* @var $verb string */ + /* @var $value string */ + $type = ucfirst(strtolower($type)); extract($search_data); @@ -1686,10 +1775,10 @@ return '"'.$ret.'"'; break; } + + return ''; } - - /** * Set's correct page for list * based on data provided with event @@ -1864,20 +1953,25 @@ * Create/Update primary image record in info found in imported data * * @param kEvent $event + * @return void + * @access protected */ - function restorePrimaryImage(&$event) + protected function restorePrimaryImage(&$event) { $object =& $event->getObject(); + /* @var $object kCatDBItem */ $has_image_info = $object->GetDBField('ImageAlt') && ($object->GetDBField('ThumbnailImage') || $object->GetDBField('FullImage')); - if (!$has_image_info) { - return false; + if ( !$has_image_info ) { + return ; } $image_data = $object->getPrimaryImageData(); - $image =& $this->Application->recallObject('img', null, Array('skip_autoload' => true)); - if ($image_data) { + $image =& $this->Application->recallObject('img', null, Array ('skip_autoload' => true)); + /* @var $image kDBItem */ + + if ( $image_data ) { $image->Load($image_data['ImageId']); } else { @@ -1889,23 +1983,23 @@ $image->SetDBField('AltName', $object->GetDBField('ImageAlt')); - if ($object->GetDBField('ThumbnailImage')) { - $thumbnail_field = $this->isURL( $object->GetDBField('ThumbnailImage') ) ? 'ThumbUrl' : 'ThumbPath'; - $image->SetDBField($thumbnail_field, $object->GetDBField('ThumbnailImage') ); + if ( $object->GetDBField('ThumbnailImage') ) { + $thumbnail_field = $this->isURL($object->GetDBField('ThumbnailImage')) ? 'ThumbUrl' : 'ThumbPath'; + $image->SetDBField($thumbnail_field, $object->GetDBField('ThumbnailImage')); $image->SetDBField('LocalThumb', $thumbnail_field == 'ThumbPath' ? 1 : 0); } - if (!$object->GetDBField('FullImage')) { + if ( !$object->GetDBField('FullImage') ) { $image->SetDBField('SameImages', 1); } else { $image->SetDBField('SameImages', 0); - $full_field = $this->isURL( $object->GetDBField('FullImage') ) ? 'Url' : 'LocalPath'; - $image->SetDBField($full_field, $object->GetDBField('FullImage') ); + $full_field = $this->isURL($object->GetDBField('FullImage')) ? 'Url' : 'LocalPath'; + $image->SetDBField($full_field, $object->GetDBField('FullImage')); $image->SetDBField('LocalImage', $full_field == 'LocalPath' ? 1 : 0); } - if ($image->isLoaded()) { + if ( $image->isLoaded() ) { $image->Update(); } else { @@ -1927,8 +2021,10 @@ { parent::OnNew($event); - if ($event->Special == 'import' || $event->Special == 'export') { + if ( $event->Special == 'import' || $event->Special == 'export' ) { $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + /* @var $export_helper kCatDBItemExportHelper */ + $export_helper->setRequiredFields($event); } } @@ -1940,13 +2036,14 @@ */ function OnProcessSelected(&$event) { + $dst_field = $this->Application->RecallVar('dst_field'); $selected_ids = $this->Application->GetVar('selected_ids'); - $dst_field = $this->Application->RecallVar('dst_field'); - - if ($dst_field == 'ItemCategory') { + if ( $dst_field == 'ItemCategory' ) { // Item Edit -> Categories Tab -> New Categories $object =& $event->getObject(); + /* @var $object kCatDBItem */ + $category_ids = explode(',', $selected_ids['c']); foreach ($category_ids as $category_id) { $object->assignToCategory($category_id); @@ -1956,12 +2053,10 @@ if ($dst_field == 'ImportCategory') { // Tools -> Import -> Item Import -> Select Import Category $this->Application->StoreVar('ImportCategory', $selected_ids['c']); -// $this->Application->StoreVar($event->getPrefixSpecial().'_ForceNotValid', 1); // not to loose import/export values on form refresh $url_params = Array ( $event->getPrefixSpecial() . '_id' => 0, $event->getPrefixSpecial() . '_event' => 'OnExportBegin', -// 'm_opener' => 's', ); $this->Application->EventManager->openerStackChange($url_params); @@ -1978,18 +2073,21 @@ function OnSaveSettings(&$event) { $event->redirect = false; - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if ($items_info) { + $items_info = $this->Application->GetVar($event->getPrefixSpecial(true)); + + if ( $items_info ) { list($id, $field_values) = each($items_info); - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ + $object->SetFieldsFromHash($field_values); $field_values['ImportFilename'] = $object->GetDBField('ImportFilename'); //if upload formatter has renamed the file during moving !!! $field_values['ImportSource'] = 2; $field_values['ImportLocalFilename'] = $object->GetDBField('ImportFilename'); $items_info[$id] = $field_values; - $this->Application->StoreVar($event->getPrefixSpecial().'_ItemsInfo', serialize($items_info)); + $this->Application->StoreVar($event->getPrefixSpecial() . '_ItemsInfo', serialize($items_info)); } } @@ -2003,9 +2101,15 @@ $this->Application->StoreVar('ImportCategory', $this->Application->getBaseCategory()); } + /** + * Cancels item editing + * @param kEvent $event + * @return void + * @todo Used? + */ function OnCancelAction(&$event) { - $event->setRedirectParams(Array('pass' => 'all,'.$event->GetPrefixSpecial()), true); + $event->setRedirectParams(Array ('pass' => 'all,' . $event->getPrefixSpecial()), true); $event->redirect = $this->Application->GetVar('cancel_template'); } @@ -2021,101 +2125,120 @@ function cacheItemOwner(&$event, $id_field, $cached_field) { $object =& $event->getObject(); + /* @var $object kDBItem */ $user_id = $object->GetDBField($id_field); $options = $object->GetFieldOptions($id_field); - if (isset($options['options'][$user_id])) { + + if ( isset($options['options'][$user_id]) ) { $object->SetDBField($cached_field, $options['options'][$user_id]); } else { $id_field = $this->Application->getUnitOption('u', 'IDField'); $table_name = $this->Application->getUnitOption('u', 'TableName'); $sql = 'SELECT Login - FROM '.$table_name.' - WHERE '.$id_field.' = '.$user_id; + FROM ' . $table_name . ' + WHERE ' . $id_field . ' = ' . $user_id; $object->SetDBField($cached_field, $this->Conn->GetOne($sql)); } } /** - * Saves item beeing edited into temp table + * Saves edited item into temp table + * If there is no id, new item is created in temp table * * @param kEvent $event + * @return void + * @access protected */ - function OnPreSave(&$event) + protected function OnPreSave(&$event) { parent::OnPreSave($event); + $use_pending_editing = $this->Application->getUnitOption($event->Prefix, 'UsePendingEditing'); - if ($event->status == kEvent::erSUCCESS && $use_pending_editing) { + + if ( $event->status == kEvent::erSUCCESS && $use_pending_editing ) { // decision: clone or not clone $object =& $event->getObject(); - if ($object->GetID() == 0 || $object->GetDBField('OrgId') > 0) { + /* @var $object kCatDBItem */ + + if ( $object->GetID() == 0 || $object->GetDBField('OrgId') > 0 ) { // new items or cloned items shouldn't be cloned again - return true; + return ; } + $perm_helper =& $this->Application->recallObject('PermissionsHelper'); + /* @var $perm_helper kPermissionsHelper */ + $owner_field = $this->getOwnerField($event->Prefix); - if ($perm_helper->ModifyCheckPermission($object->GetDBField($owner_field), $object->GetDBField('CategoryId'), $event->Prefix) == 2) { + if ( $perm_helper->ModifyCheckPermission($object->GetDBField($owner_field), $object->GetDBField('CategoryId'), $event->Prefix) == 2 ) { // 1. clone original item - $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($object->GetID()), null, null, null, true); - $ci_table = $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems'); + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array ($object->GetID()), null, null, null, true); + $ci_table = $this->Application->GetTempName(TABLE_PREFIX . 'CategoryItems'); + // 2. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem $sql = 'SELECT ResourceId - FROM '.$object->TableName.' - WHERE '.$object->IDField.' = '.$cloned_ids[0]; + FROM ' . $object->TableName . ' + WHERE ' . $object->IDField . ' = ' . $cloned_ids[0]; $clone_resource_id = $this->Conn->GetOne($sql); - $sql = 'DELETE FROM '.$ci_table.' - WHERE ItemResourceId = '.$clone_resource_id.' AND PrimaryCat = 1'; + $sql = 'DELETE FROM ' . $ci_table . ' + WHERE ItemResourceId = ' . $clone_resource_id . ' AND PrimaryCat = 1'; $this->Conn->Query($sql); // 3. copy main item categoryitems to cloned item - $sql = ' INSERT INTO '.$ci_table.' (CategoryId, ItemResourceId, PrimaryCat, ItemPrefix, Filename) - SELECT CategoryId, '.$clone_resource_id.' AS ItemResourceId, PrimaryCat, ItemPrefix, Filename - FROM '.$ci_table.' - WHERE ItemResourceId = '.$object->GetDBField('ResourceId'); + $sql = ' INSERT INTO ' . $ci_table . ' (CategoryId, ItemResourceId, PrimaryCat, ItemPrefix, Filename) + SELECT CategoryId, ' . $clone_resource_id . ' AS ItemResourceId, PrimaryCat, ItemPrefix, Filename + FROM ' . $ci_table . ' + WHERE ItemResourceId = ' . $object->GetDBField('ResourceId'); $this->Conn->Query($sql); // 4. put cloned id to OrgId field of item being cloned - $sql = 'UPDATE '.$object->TableName.' - SET OrgId = '.$object->GetID().' - WHERE '.$object->IDField.' = '.$cloned_ids[0]; + $sql = 'UPDATE ' . $object->TableName . ' + SET OrgId = ' . $object->GetID() . ' + WHERE ' . $object->IDField . ' = ' . $cloned_ids[0]; $this->Conn->Query($sql); // 5. substitute id of item being cloned with clone id - $this->Application->SetVar($event->getPrefixSpecial().'_id', $cloned_ids[0]); + $this->Application->SetVar($event->getPrefixSpecial() . '_id', $cloned_ids[0]); $selected_ids = $this->getSelectedIDs($event, true); $selected_ids[ array_search($object->GetID(), $selected_ids) ] = $cloned_ids[0]; $this->StoreSelectedIDs($event, $selected_ids); // 6. delete original item from temp table - $temp_handler->DeleteItems($event->Prefix, $event->Special, Array($object->GetID())); + $temp_handler->DeleteItems($event->Prefix, $event->Special, Array ($object->GetID())); } } } /** - * Sets default expiration based on module setting + * Sets item's owner field * * @param kEvent $event + * @return void + * @access protected */ - function OnPreCreate(&$event) + protected function OnPreCreate(&$event) { parent::OnPreCreate($event); - if ($event->status == kEvent::erSUCCESS) { - $object =& $event->getObject(); - $owner_field = $this->getOwnerField($event->Prefix); - - $object->SetDBField($owner_field, $this->Application->RecallVar('user_id')); + if ( $event->status != kEvent::erSUCCESS ) { + return ; } + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $owner_field = $this->getOwnerField($event->Prefix); + $object->SetDBField($owner_field, $this->Application->RecallVar('user_id')); } /** @@ -2129,16 +2252,21 @@ } /** - * Occures before an item is cloneded - * Id of ORIGINAL item is passed as event' 'id' param - * Do not call object' Update method in this event, just set needed fields! + * 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 */ - function OnBeforeClone(&$event) + protected function OnBeforeClone(&$event) { - if ($this->Application->GetVar('ResetCatBeforeClone')) { + parent::OnBeforeClone($event); + + if ( $this->Application->GetVar('ResetCatBeforeClone') ) { $object =& $event->getObject(); + /* @var $object kDBItem */ + $object->SetDBField('CategoryId', null); } } @@ -2147,16 +2275,20 @@ * Set status for new category item based on user permission in category * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeItemCreate(&$event) + protected function OnBeforeItemCreate(&$event) { + parent::OnBeforeItemCreate($event); + $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $is_admin = $this->Application->isAdminUser; $owner_field = $this->getOwnerField($event->Prefix); - if ((!$object->IsTempTable() && !$is_admin) || ($is_admin && !$object->GetDBField($owner_field))) { + if ( (!$object->IsTempTable() && !$is_admin) || ($is_admin && !$object->GetDBField($owner_field)) ) { // Front-end OR owner not specified -> set to currently logged-in user $object->SetDBField($owner_field, $this->Application->RecallVar('user_id')); } @@ -2165,7 +2297,7 @@ $object->SetDBField('ResourceId', $this->Application->NextResourceId()); } - if (!$this->Application->isAdmin) { + if ( !$this->Application->isAdmin ) { $this->setItemStatusByPermission($event); } } @@ -2184,7 +2316,7 @@ } $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $perm_helper =& $this->Application->recallObject('PermissionsHelper'); /* @var $perm_helper kPermissionsHelper */ @@ -2402,32 +2534,34 @@ } /** - * Apply same processing to each item beeing selected in grid + * Apply same processing to each item being selected in grid * * @param kEvent $event - * @access private + * @return void + * @access protected */ - function iterateItems(&$event) + protected function iterateItems(&$event) { - if ($event->Name != 'OnMassApprove' && $event->Name != 'OnMassDecline') { - return parent::iterateItems($event); + if ( $event->Name != 'OnMassApprove' && $event->Name != 'OnMassDecline' ) { + parent::iterateItems($event); } - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; - return; + return ; } - $object =& $event->getObject( Array('skip_autoload' => true) ); - /* @var $object kCatDBItem */ + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kCatDBItem */ $ids = $this->StoreSelectedIDs($event); - if ($ids) { + if ( $ids ) { foreach ($ids as $id) { + $ret = true; $object->Load($id); - switch ($event->Name) { + switch ( $event->Name ) { case 'OnMassApprove': $ret = $object->ApproveChanges(); break; @@ -2437,7 +2571,7 @@ break; } - if (!$ret) { + if ( !$ret ) { $event->status = kEvent::erFAIL; $event->redirect = false; break; @@ -2580,11 +2714,11 @@ { $remove_sortings = Array (); - if (!$this->Application->isAdmin) { + if ( !$this->Application->isAdmin ) { // remove Pick sorting on Front-end, when not required - $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); + $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping', Array ()); - if (!isset($config_mapping['ForceEditorPick']) || !$this->Application->ConfigValue($config_mapping['ForceEditorPick'])) { + if ( !isset($config_mapping['ForceEditorPick']) || !$this->Application->ConfigValue($config_mapping['ForceEditorPick']) ) { $remove_sortings[] = 'EditorsPick'; } } @@ -2593,11 +2727,12 @@ $remove_sortings = array_merge($remove_sortings, Array ('Priority', 'EditorsPick')); } - if (!$remove_sortings) { - return ; + if ( !$remove_sortings ) { + return; } $list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ()); + /* @var $list_sortings Array */ foreach ($list_sortings as $special => $sorting_fields) { foreach ($remove_sortings as $sorting_field) { @@ -2616,7 +2751,7 @@ function OnDownloadFile(&$event) { $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $event->status = kEvent::erSTOP; @@ -2650,7 +2785,7 @@ /* @var $rating_helper RatingHelper */ $object =& $event->getObject( Array ('skip_autoload' => true) ); - /* @var $object kDBItem */ + /* @var $object kCatDBItem */ $object->Load( $this->Application->GetVar('id') );