Index: branches/RC/core/units/general/cat_event_handler.php =================================================================== diff -u -N -r11693 -r11724 --- branches/RC/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 11693) +++ branches/RC/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 11724) @@ -13,13 +13,13 @@ { parent::mapPermissions(); $permissions = Array( - 'OnSaveSettings' => Array('self' => 'add|edit|advanced:import'), - 'OnResetSettings' => Array('self' => 'add|edit|advanced:import'), - 'OnBeforeDeleteOriginal' => Array('self' => 'edit|advanced:approve'), + 'OnSaveSettings' => Array ('self' => 'add|edit|advanced:import'), + 'OnResetSettings' => Array ('self' => 'add|edit|advanced:import'), + 'OnBeforeDeleteOriginal' => Array ('self' => 'edit|advanced:approve'), - 'OnDownloadFile' => Array('self' => 'view'), - 'OnCancelAction' => Array('self' => true), - 'OnItemBuild' => Array('self' => true), + 'OnDownloadFile' => Array ('self' => 'view'), + 'OnCancelAction' => Array ('self' => true), + 'OnItemBuild' => Array ('self' => true), 'OnMakeVote' => Array ('self' => true), ); @@ -68,43 +68,40 @@ $this->Application->LinkVar('m_cat_id'); } - $check_events = Array ('OnEdit', 'OnSave', 'OnMassDelete'); + $check_events = Array ( + 'OnEdit', 'OnSave', 'OnMassDelete', 'OnMassApprove', + 'OnMassDecline', 'OnMassMoveUp', 'OnMassMoveDown' + ); if (in_array($event->Name, $check_events)) { - // check each id from selected individually and only if all are allowed proceed next - if ($event->Name == 'OnSave') { - $selected_ids = implode(',', $this->getSelectedIDs($event, true)); - if (!$selected_ids) { - $selected_ids = 0; // when saving newly created item (OnPreCreate -> OnPreSave -> OnSave) - } - } - else { - $selected_ids = implode(',', $this->StoreSelectedIDs($event)); - } + $items = $this->_getPermissionCheckInfo($event); - $perm_value = true; - if (strlen($selected_ids)) { - $perm_helper =& $this->Application->recallObject('PermissionsHelper'); - /* @var $perm_helper kPermissionsHelper */ + $perm_helper =& $this->Application->recallObject('PermissionsHelper'); + /* @var $perm_helper kPermissionsHelper */ - $items = $perm_helper->GetCategoryItemData($event->Prefix, $selected_ids); + if (($event->Name == 'OnSave') && array_key_exists(0, $items)) { + // adding new item (ID = 0) + $perm_value = $perm_helper->AddCheckPermission($items[0]['CategoryId'], $event->Prefix) > 0; + } + else { + // leave only items, that can be edited + $ids = Array (); $check_method = ($event->Name == 'OnMassDelete') ? 'DeleteCheckPermission' : 'ModifyCheckPermission'; foreach ($items as $item_id => $item_data) { - if ($perm_helper->$check_method($item_data['CreatedById'], $item_data['CategoryId'], $event->Prefix) == 0) { - // one of items selected has no permission - $perm_value = false; - break; + if ($perm_helper->$check_method($item_data['CreatedById'], $item_data['CategoryId'], $event->Prefix) > 0) { + $ids[] = $item_id; } } - if (!$perm_value) { - $event->status = erPERM_FAIL; + if (!$ids) { + // no items left for editing -> no permission + return $perm_helper->finalizePermissionCheck($event, false); } + + $perm_value = true; + $event->setEventParam('ids', $ids); // will be used later by "kDBEventHandler::StoreSelectedIDs" method } - else { - trigger_error('IDs not passed to '.$event->getPrefixSpecial().':CheckPermission', E_USER_WARNING); - } - return $perm_value; + return $perm_helper->finalizePermissionCheck($event, $perm_value); } $export_events = Array ('OnSaveSettings', 'OnResetSettings', 'OnExportBegin'); @@ -124,6 +121,64 @@ } /** + * Returns category item IDs, that require permission checking + * + * @param kEvent $event + * @return string + */ + function _getPermissionCheckIDs(&$event) + { + if ($event->Name == 'OnSave') { + $selected_ids = implode(',', $this->getSelectedIDs($event, true)); + if (!$selected_ids) { + $selected_ids = 0; // when saving newly created item (OnPreCreate -> OnPreSave -> OnSave) + } + } + else { + // OnEdit, OnMassDelete events, when items are checked in grid + $selected_ids = implode(',', $this->StoreSelectedIDs($event)); + } + + return $selected_ids; + } + + /** + * Returns information used in permission checking + * + * @param kEvent $event + * @return Array + */ + function _getPermissionCheckInfo(&$event) + { + $perm_helper =& $this->Application->recallObject('PermissionsHelper'); + /* @var $perm_helper kPermissionsHelper */ + + // when saving data from temp table to live table check by data from temp table + $item_ids = $this->_getPermissionCheckIDs($event); + $items = $perm_helper->GetCategoryItemData($event->Prefix, $item_ids, $event->Name == 'OnSave'); + + if (!$items) { + // when item not present in temp table, then permission is not checked, because there are no data in db to check + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); + list ($id, $fields_hash) = each($items_info); + + if (array_key_exists('CategoryId', $fields_hash)) { + $item_category = $fields_hash['CategoryId']; + } + else { + $item_category = $this->Application->GetVar('m_cat_id'); + } + + $items[$id] = Array ( + 'CreatedById' => $this->Application->RecallVar('use_id'), + 'CategoryId' => $item_category, + ); + } + + return $items; + } + + /** * Add selected items to clipboard with mode = COPY (CLONE) * * @param kEvent $event @@ -676,8 +731,7 @@ $object->addCalculatedField('CachedNavbar', 'l'.$this->Application->GetVar('m_lang').'_CachedNavbar'); - if ($event->Special == 'export' || $event->Special == 'import') - { + if ($event->Special == 'export' || $event->Special == 'import') { $export_helper =& $this->Application->recallObject('CatItemExportHelper'); $export_helper->prepareExportColumns($event); } @@ -750,26 +804,36 @@ } /** - * Enter description here... + * Moves item to preferred category, updates item hits * * @param kEvent $event */ function OnBeforeItemUpdate(&$event) { + parent::OnBeforeItemUpdate($event); + + // update hits field $property_map = $this->Application->getUnitOption($event->Prefix, 'ItemPropertyMappings'); - if (!$property_map) { - return; + if ($property_map) { + $click_field = $property_map['ClickField']; + + $object =& $event->getObject(); + /* @var $object kCatDBItem */ + + if( $this->Application->IsAdmin() && ($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); + } } - $click_field = $property_map['ClickField']; - $object =& $event->getObject(); - if( $this->Application->IsAdmin() && ($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) { + $object->MoveToCat($target_category); } } @@ -2425,14 +2489,16 @@ { parent::OnAfterConfigRead($event); - if ($event->Prefix != 'cms') { - $file_helper =& $this->Application->recallObject('FileHelper'); - /* @var $file_helper FileHelper */ - - $file_helper->createItemFiles($event->Prefix, true); // create image fields - $file_helper->createItemFiles($event->Prefix, false); // create file fields + if (defined('IS_INSTALL') && IS_INSTALL) { + return ; } + $file_helper =& $this->Application->recallObject('FileHelper'); + /* @var $file_helper FileHelper */ + + $file_helper->createItemFiles($event->Prefix, true); // create image fields + $file_helper->createItemFiles($event->Prefix, false); // create file fields + // add EditorsPick to ForcedSorting if needed $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); if (array_key_exists('ForceEditorPick', $config_mapping) && $this->Application->ConfigValue($config_mapping['ForceEditorPick'])) { @@ -2459,6 +2525,17 @@ $grids[$process_grid . 'ShowAll'] = $grid_data; } $this->Application->setUnitOption($this->Prefix, 'Grids', $grids); + + // add options for CategoryId field (quick way to select item's primary category) + $category_helper =& $this->Application->recallObject('CategoryHelper'); + /* @var $category_helper CategoryHelper */ + + $virtual_fields = $this->Application->getUnitOption($event->Prefix, 'VirtualFields'); + + $virtual_fields['CategoryId']['default'] = (int)$this->Application->GetVar('m_cat_id'); + $virtual_fields['CategoryId']['options'] = $category_helper->getStructureTreeAsOptions(); + + $this->Application->setUnitOption($event->Prefix, 'VirtualFields', $virtual_fields); } /**