Index: core/install/english.lang IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/english.lang (revision 15739) +++ core/install/english.lang (revision ) @@ -151,6 +151,7 @@ U3dpdGNoIENhdGFsb2cgdGFicyBiYXNlZCBvbiBNb2R1bGU= U2VjdGlvbiBQZXJtaXNzaW9uIFJlYnVpbGQgTW9kZQ== Q2hlY2sgU3RvcCBXb3Jkcw== + RW5hYmxlICJWaWV3IFBlcm1pc3Npb25zIiBDaGVjayBpbiBDYXRhbG9n Q0tGaW5kZXIgTGljZW5zZSBLZXk= Q0tGaW5kZXIgTGljZW5zZSBOYW1l RGVmYXVsdCBDU1YgRXhwb3J0IERlbGltaXRlcg== Index: core/units/categories/categories_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/categories/categories_config.php (revision 15716) +++ core/units/categories/categories_config.php (revision ) @@ -266,7 +266,7 @@ '' => ' SELECT %1$s.* %2$s FROM %1$s LEFT JOIN '.TABLE_PREFIX.'%3$sCatalogImages img ON img.ResourceId = %1$s.ResourceId AND img.DefaultImg = 1 - LEFT JOIN '.TABLE_PREFIX.'CategoryPermissionsCache ON '.TABLE_PREFIX.'CategoryPermissionsCache.CategoryId = %1$s.CategoryId + {PERM_JOIN} LEFT JOIN '.TABLE_PREFIX.'%3$sCategoryCustomData cust ON %1$s.ResourceId = cust.ResourceId', '-virtual' => 'SELECT %1$s.* %2$s FROM %1$s', ), \ No newline at end of file Index: core/kernel/db/cat_tag_processor.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/kernel/db/cat_tag_processor.php (revision 15716) +++ core/kernel/db/cat_tag_processor.php (revision ) @@ -685,36 +685,34 @@ return $count_helper->ItemCount($this->Prefix, $today_only); } - function CategorySelector($params) + /** + * Displays list of allowed categories on "Suggest Link" and similar forms. + * + * @param array $params Tag params. + * + * @return string + * @access protected + */ + protected function CategorySelector($params) { $category_id = isset($params['category_id']) && is_numeric($params['category_id']) ? $params['category_id'] : false; + - if ($category_id === false) { + if ( $category_id === false ) { // if category id not given use module root category $category_id = $this->Application->findModule('Var', $this->Prefix, 'RootCat'); } $id_field = $this->Application->getUnitOption('c', 'IDField'); - $title_field = $this->Application->getUnitOption('c', 'TitleField'); - $table_name = $this->Application->getUnitOption('c', 'TableName'); - $count_helper = $this->Application->recallObject('CountHelper'); - /* @var $count_helper kCountHelper */ - - list ($view_perm, $view_filter) = $count_helper->GetPermissionClause('c', 'perm_cache'); - // get category list (permission based) - $sql = 'SELECT c.'.$title_field.' AS CategoryName, c.'.$id_field.', c.l' . $this->Application->GetVar('m_lang') . '_CachedNavbar AS CachedNavbar - FROM '.$table_name.' c - INNER JOIN '.TABLE_PREFIX.'CategoryPermissionsCache perm_cache ON c.CategoryId = perm_cache.CategoryId - WHERE (ParentId = '.$category_id.') AND ('.$view_filter.') AND (perm_cache.PermId = '.$view_perm.') AND (c.Status = '.STATUS_ACTIVE.') - ORDER BY c.'.$title_field.' ASC'; - $categories = $this->Conn->Query($sql, $id_field); + $categories = $this->Conn->Query($this->getCategorySelectorQuery($category_id), $id_field); $block_params = $this->prepareTagParams($params); $block_params['name'] = $params['render_as']; $block_params['strip_nl'] = 2; $ret = ''; + foreach ($categories as $category_id => $category_data) { // print category $block_params['separator'] = isset($params['category_id']) ? $params['separator'] : ''; // return original separator, remove separator for top level categories @@ -727,11 +725,42 @@ $ret .= $this->Application->ParseBlock($block_params); // print it's children - $block_params['separator'] = '   '.$params['separator']; + $block_params['separator'] = '   ' . $params['separator']; $ret .= $this->CategorySelector($block_params); } return $ret; + } + + /** + * Returns given category sub-categories, that user have rights to view. + * + * @param int $category_id Category. + * + * @return array + * @access protected + */ + protected function getCategorySelectorQuery($category_id) + { + $id_field = $this->Application->getUnitOption('c', 'IDField'); + $title_field = $this->Application->getUnitOption('c', 'TitleField'); + + $where_clause = Array ( + 'c.ParentId = ' . $category_id, + 'c.Status = ' . STATUS_ACTIVE, + ); + + $sql = 'SELECT c.' . $title_field . ' AS CategoryName, + c.' . $id_field . ', + c.l' . $this->Application->GetVar('m_lang') . '_CachedNavbar AS CachedNavbar + FROM ' . $this->Application->getUnitOption('c', 'TableName') . ' c'; + + $count_helper = $this->Application->recallObject('CountHelper'); + /* @var $count_helper kCountHelper */ + + list ($sql, $where_clause) = $count_helper->attachViewPermissionCheck('c', $sql, $where_clause); + + return $sql . ' WHERE (' . implode(') AND (', $where_clause) . ') ORDER BY c.' . $title_field . ' ASC'; } function PrintMoreCategories($params) \ No newline at end of file Index: core/kernel/db/cat_event_handler.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/kernel/db/cat_event_handler.php (revision 15716) +++ core/kernel/db/cat_event_handler.php (revision ) @@ -594,7 +594,7 @@ parent::SetCustomQuery($event); $object = $event->getObject(); - /* @var $object kDBList */ + /* @var $object kCatDBList */ // add category filter if needed if ($event->Special != 'showall' && $event->Special != 'user') { @@ -648,32 +648,48 @@ $object->addFilter('owner_filter', '%1$s.'.$this->getOwnerField($event->Prefix).' = '.$editable_user); } - // add permission filter + $this->applyViewPermissionFilter($object); + + $types = $event->getEventParam('types'); + $this->applyItemStatusFilter($object, $types); + + $except_types = $event->getEventParam('except'); + $type_clauses = $this->getTypeClauses($event); + + $search_helper = $this->Application->recallObject('SearchHelper'); + /* @var $search_helper kSearchHelper */ + + $search_helper->SetComplexFilter($event, $type_clauses, $types, $except_types); + } + + /** + * Adds filter, that uses *.VIEW permissions to determine if an item should be shown to a user. + * + * @param kCatDBList $object Object. + * + * @return void + * @access protected + */ + protected function applyViewPermissionFilter(kCatDBList $object) + { + if ( !$this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { + return; + } + - if ($this->Application->RecallVar('user_id') == USER_ROOT) { + if ( $this->Application->RecallVar('user_id') == USER_ROOT ) { // for "root" CATEGORY.VIEW permission is checked for items lists too $view_perm = 1; } else { - // for any real user itemlist view permission is checked instead of CATEGORY.VIEW + // for any real user item list view permission is checked instead of CATEGORY.VIEW $count_helper = $this->Application->recallObject('CountHelper'); /* @var $count_helper kCountHelper */ - list ($view_perm, $view_filter) = $count_helper->GetPermissionClause($event->Prefix, 'perm'); + list ($view_perm, $view_filter) = $count_helper->GetPermissionClause($object->Prefix, 'perm'); $object->addFilter('perm_filter2', $view_filter); } - $object->addFilter('perm_filter', 'perm.PermId = '.$view_perm); + $object->addFilter('perm_filter', 'perm.PermId = ' . $view_perm); - - $types = $event->getEventParam('types'); - $this->applyItemStatusFilter($object, $types); - - $except_types = $event->getEventParam('except'); - $type_clauses = $this->getTypeClauses($event); - - $search_helper = $this->Application->recallObject('SearchHelper'); - /* @var $search_helper kSearchHelper */ - - $search_helper->SetComplexFilter($event, $type_clauses, $types, $except_types); } /** @@ -2810,6 +2826,8 @@ parent::OnAfterConfigRead($event); if (defined('IS_INSTALL') && IS_INSTALL) { + $this->addViewPermissionJoin($event); + return ; } @@ -2821,7 +2839,7 @@ $file_helper->createItemFiles($event->Prefix, false); // create file fields } - $this->changeSortings($event); + $this->changeSortings($event)->addViewPermissionJoin($event); // add grids for advanced view (with primary category column) $grids = $this->Application->getUnitOption($this->Prefix, 'Grids'); @@ -2845,7 +2863,15 @@ $this->Application->setUnitOption($event->Prefix, 'VirtualFields', $virtual_fields); } - function changeSortings($event) + /** + * Changes default sorting according to system settings. + * + * @param kEvent $event Event. + * + * @return self + * @access protected + */ + protected function changeSortings(kEvent $event) { $remove_sortings = Array (); @@ -2863,7 +2889,7 @@ } if ( !$remove_sortings ) { - return; + return $this; } $list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ()); @@ -2876,6 +2902,37 @@ } $this->Application->setUnitOption($event->Prefix, 'ListSortings', $list_sortings); + + return $this; + } + + /** + * Adds permission table table JOIN clause only, when advanced catalog view permissions enabled. + * + * @param kEvent $event Event. + * + * @return self + * @access protected + */ + protected function addViewPermissionJoin(kEvent $event) + { + if ( $this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { + $join_clause = 'LEFT JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm ON perm.CategoryId = ' . TABLE_PREFIX . '%3$sCategoryItems.CategoryId'; + } + else { + $join_clause = ''; + } + + $list_sqls = $this->Application->getUnitOption($event->Prefix, 'ListSQLs'); + /* @var $list_sqls array */ + + foreach ($list_sqls as $special => $list_sql) { + $list_sqls[$special] = str_replace('{PERM_JOIN}', $join_clause, $list_sql); + } + + $this->Application->setUnitOption($event->Prefix, 'ListSQLs', $list_sqls); + + return $this; } /** \ No newline at end of file Index: core/units/structure/structure_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/structure/structure_config.php (revision 15716) +++ core/units/structure/structure_config.php (revision ) @@ -101,7 +101,7 @@ 'ListSQLs' => Array ( '' => ' SELECT %1$s.* %2$s FROM %1$s - LEFT JOIN '.TABLE_PREFIX.'CategoryPermissionsCache ON '.TABLE_PREFIX.'CategoryPermissionsCache.CategoryId = %1$s.CategoryId', + {PERM_JOIN}', '-virtual' => 'SELECT %1$s.* %2$s FROM %1$s', ), \ No newline at end of file Index: core/install/install_data.sql IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/install_data.sql (revision 15747) +++ core/install/install_data.sql (revision ) @@ -11,11 +11,12 @@ INSERT INTO SystemSettings VALUES(DEFAULT, 'AllowDeleteRootCats', '1', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_AllowDeleteRootCats', 'checkbox', NULL, NULL, 10.08, 0, 0, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'Catalog_PreselectModuleTab', '1', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_CatalogPreselectModuleTab', 'checkbox', NULL, NULL, 10.09, 0, 0, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'RecycleBinFolder', '', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_RecycleBinFolder', 'text', NULL, NULL, 10.10, 0, 0, NULL); -INSERT INTO SystemSettings VALUES(DEFAULT, 'CategoryPermissionRebuildMode', '3', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_CategoryPermissionRebuildMode', 'select', NULL, '1=la_opt_Manual||2=la_opt_Silent||3=la_opt_Automatic', 10.11, 0, 0, 'hint:la_config_CategoryPermissionRebuildMode'); -INSERT INTO SystemSettings VALUES(DEFAULT, 'FilenameSpecialCharReplacement', '-', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_FilenameSpecialCharReplacement', 'select', NULL, '_=+_||-=+-', 10.12, 0, 0, NULL); -INSERT INTO SystemSettings VALUES(DEFAULT, 'Search_MinKeyword_Length', '3', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_Search_MinKeyword_Length', 'text', NULL, NULL, 10.13, 0, 0, NULL); -INSERT INTO SystemSettings VALUES(DEFAULT, 'ExcludeTemplateSectionsFromSearch', '0', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_ExcludeTemplateSectionsFromSearch', 'checkbox', '', '', 10.14, 0, 0, NULL); -INSERT INTO SystemSettings VALUES(DEFAULT, 'UpdateCountersOnFilterChange', '1', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_UpdateCountersOnFilterChange', 'checkbox', '', '', 10.15, 0, 0, NULL); +INSERT INTO SystemSettings VALUES(DEFAULT, 'CheckViewPermissionsInCatalog', '0', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_CheckViewPermissionsInCatalog', 'radio', NULL, '1=la_Yes||0=la_No', 10.11, 0, 1, 'hint:la_config_CheckViewPermissionsInCatalog'); +INSERT INTO SystemSettings VALUES(DEFAULT, 'CategoryPermissionRebuildMode', '3', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_CategoryPermissionRebuildMode', 'select', NULL, '1=la_opt_Manual||2=la_opt_Silent||3=la_opt_Automatic', 10.12, 0, 0, 'hint:la_config_CategoryPermissionRebuildMode'); +INSERT INTO SystemSettings VALUES(DEFAULT, 'FilenameSpecialCharReplacement', '-', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_FilenameSpecialCharReplacement', 'select', NULL, '_=+_||-=+-', 10.13, 0, 0, NULL); +INSERT INTO SystemSettings VALUES(DEFAULT, 'Search_MinKeyword_Length', '3', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_Search_MinKeyword_Length', 'text', NULL, NULL, 10.14, 0, 0, NULL); +INSERT INTO SystemSettings VALUES(DEFAULT, 'ExcludeTemplateSectionsFromSearch', '0', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_ExcludeTemplateSectionsFromSearch', 'checkbox', '', '', 10.15, 0, 0, NULL); +INSERT INTO SystemSettings VALUES(DEFAULT, 'UpdateCountersOnFilterChange', '1', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_UpdateCountersOnFilterChange', 'checkbox', '', '', 10.16, 0, 0, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'Category_MetaKey', '', 'In-Portal', 'in-portal:configure_categories', 'la_Text_MetaInfo', 'la_category_metakey', 'textarea', '', '', 20.01, 0, 1, NULL); INSERT INTO SystemSettings VALUES(DEFAULT, 'Category_MetaDesc', '', 'In-Portal', 'in-portal:configure_categories', 'la_Text_MetaInfo', 'la_category_metadesc', 'textarea', '', '', 20.02, 0, 1, NULL); \ No newline at end of file Index: core/units/helpers/permissions_helper.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/helpers/permissions_helper.php (revision 15716) +++ core/units/helpers/permissions_helper.php (revision ) @@ -627,26 +627,31 @@ if ( preg_match('/(.*)\.VIEW$/', $name) && ($type == 0) ) { // cached view permission of category: begin + if ( $this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { - if ( strpos($cat_id, '|') !== false ) { - $category_path = explode('|', substr($cat_id, 1, -1)); - $cat_id = end($category_path); - } + if ( strpos($cat_id, '|') !== false ) { + $category_path = explode('|', substr($cat_id, 1, -1)); + $cat_id = end($category_path); + } - $sql = 'SELECT PermissionConfigId - FROM ' . TABLE_PREFIX . 'CategoryPermissionsConfig - WHERE PermissionName = ' . $this->Conn->qstr($name); - $perm_id = $this->Conn->GetOne($sql); + $sql = 'SELECT PermissionConfigId + FROM ' . TABLE_PREFIX . 'CategoryPermissionsConfig + WHERE PermissionName = ' . $this->Conn->qstr($name); + $perm_id = $this->Conn->GetOne($sql); - $sql = 'SELECT PermId - FROM ' . TABLE_PREFIX . 'CategoryPermissionsCache - WHERE (PermId = ' . $perm_id . ') AND (CategoryId = ' . (int)$cat_id . ')'; + $sql = 'SELECT PermId + FROM ' . TABLE_PREFIX . 'CategoryPermissionsCache + WHERE (PermId = ' . $perm_id . ') AND (CategoryId = ' . (int)$cat_id . ')'; - $view_filters = Array (); - foreach ($groups as $group) { - $view_filters[] = 'FIND_IN_SET(' . $group . ', ACL)'; - } - $sql .= ' AND (' . implode(' OR ', $view_filters) . ')'; - $perm_value = $this->Conn->GetOne($sql) ? 1 : 0; + $view_filters = Array (); + foreach ($groups as $group) { + $view_filters[] = 'FIND_IN_SET(' . $group . ', ACL)'; + } + $sql .= ' AND (' . implode(' OR ', $view_filters) . ')'; + $perm_value = $this->Conn->GetOne($sql) ? 1 : 0; + } + else { + $perm_value = 1; + } $this->Application->setCache('permissions[%CPermSerial%]:' . $cache_key, $perm_value); return $perm_value; \ No newline at end of file Index: core/units/categories/categories_event_handler.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/categories/categories_event_handler.php (revision 15734) +++ core/units/categories/categories_event_handler.php (revision ) @@ -332,20 +332,8 @@ } } - $object->addFilter('perm_filter', TABLE_PREFIX . 'CategoryPermissionsCache.PermId = 1'); // check for CATEGORY.VIEW permission - if ($this->Application->RecallVar('user_id') != USER_ROOT) { - // apply permission filters to all users except "root" - $view_filters = Array (); - $groups = explode(',',$this->Application->RecallVar('UserGroups')); + $this->applyViewPermissionFilter($object); - foreach ($groups as $group) { - $view_filters[] = 'FIND_IN_SET('.$group.', ' . TABLE_PREFIX . 'CategoryPermissionsCache.ACL)'; - } - - $view_filter = implode(' OR ', $view_filters); - $object->addFilter('perm_filter2', $view_filter); - } - if (!$this->Application->isAdminUser) { // apply status filter only on front $object->addFilter('status_filter', $object->TableName.'.Status = 1'); @@ -555,6 +543,35 @@ } /** + * Adds filter, that uses *.VIEW permissions to determine if an item should be shown to a user. + * + * @param kDBList $object Object. + * + * @return void + * @access protected + */ + protected function applyViewPermissionFilter(kDBList $object) + { + if ( !$this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { + return; + } + + if ( $this->Application->RecallVar('user_id') == USER_ROOT ) { + // for "root" CATEGORY.VIEW permission is checked for items lists too + $view_perm = 1; + } + else { + $count_helper = $this->Application->recallObject('CountHelper'); + /* @var $count_helper kCountHelper */ + + list ($view_perm, $view_filter) = $count_helper->GetPermissionClause($object->Prefix, 'perm'); + $object->addFilter('perm_filter2', $view_filter); + } + + $object->addFilter('perm_filter', 'perm.PermId = ' . $view_perm); // check for CATEGORY.VIEW permission + } + + /** * Returns current theme id * * @return int @@ -2163,6 +2180,8 @@ if (defined('IS_INSTALL') && IS_INSTALL) { // skip any processing, because Categories table doesn't exists until install is finished + $this->addViewPermissionJoin($event); + return ; } @@ -2224,6 +2243,8 @@ $this->Application->setUnitOption($event->Prefix, 'ListSortings', $list_sortings); } + $this->addViewPermissionJoin($event); + // add grids for advanced view (with primary category column) $grids = $this->Application->getUnitOption($this->Prefix, 'Grids'); $process_grids = Array ('Default', 'Radio'); @@ -2233,6 +2254,35 @@ $grids[$process_grid . 'ShowAll'] = $grid_data; } $this->Application->setUnitOption($this->Prefix, 'Grids', $grids); + } + + /** + * Adds permission table table JOIN clause only, when advanced catalog view permissions enabled. + * + * @param kEvent $event Event. + * + * @return self + * @access protected + */ + protected function addViewPermissionJoin(kEvent $event) + { + if ( $this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { + $join_clause = 'LEFT JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm ON perm.CategoryId = %1$s.CategoryId'; + } + else { + $join_clause = ''; + } + + $list_sqls = $this->Application->getUnitOption($event->Prefix, 'ListSQLs'); + /* @var $list_sqls array */ + + foreach ($list_sqls as $special => $list_sql) { + $list_sqls[$special] = str_replace('{PERM_JOIN}', $join_clause, $list_sql); + } + + $this->Application->setUnitOption($event->Prefix, 'ListSQLs', $list_sqls); + + return $this; } /** \ No newline at end of file Index: core/install/upgrades.sql IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/upgrades.sql (revision 15747) +++ core/install/upgrades.sql (revision ) @@ -2884,3 +2884,9 @@ UPDATE Modules SET ClassNamespace = 'Intechnic\\InPortal\\Core' WHERE `Name` IN ('Core', 'In-Portal'); + +UPDATE SystemSettings +SET DisplayOrder = DisplayOrder + 0.01 +WHERE ModuleOwner = 'In-Portal' AND Section = 'in-portal:configure_categories' AND DisplayOrder > 10.10 AND DisplayOrder < 20; + +INSERT INTO SystemSettings VALUES(DEFAULT, 'CheckViewPermissionsInCatalog', '1', 'In-Portal', 'in-portal:configure_categories', 'la_title_General', 'la_config_CheckViewPermissionsInCatalog', 'radio', NULL, '1=la_Yes||0=la_No', 10.11, 0, 1, 'hint:la_config_CheckViewPermissionsInCatalog'); Index: core/units/helpers/count_helper.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/helpers/count_helper.php (revision 15716) +++ core/units/helpers/count_helper.php (revision ) @@ -170,38 +170,41 @@ } /** - * Counts items (of specific type) from all categories + * Counts items (of specific type) from all categories. * - * @param string $prefix - * @param bool $today - * @param string $count_sql + * @param string $prefix Prefix. + * @param boolean $today Count only items added today. + * @param string|null $count_sql Sql. + * * @return int + * @access public */ - function ItemCount($prefix, $today = false, $count_sql = null) + public function ItemCount($prefix, $today = false, $count_sql = null) { - $table_name = $this->Application->getUnitOption($prefix, 'TableName'); + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); - if (!isset($count_sql)) { + if ( !isset($count_sql) ) { - $count_sql = 'COUNT(*)'; - } + $count_sql = 'COUNT(*)'; + } + $where_clause = array( + 'ci.PrimaryCat = 1', + 'c.Status = ' . STATUS_ACTIVE, + ); + - $sql = 'SELECT '.$count_sql.' + $sql = 'SELECT ' . $count_sql . ' - FROM '.$table_name.' item_table + FROM ' . $table_name . ' item_table - INNER JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId + INNER JOIN ' . TABLE_PREFIX . 'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId - INNER JOIN '.TABLE_PREFIX.'Categories c ON c.CategoryId = ci.CategoryId - INNER JOIN '.TABLE_PREFIX.'CategoryPermissionsCache perm_cache ON ci.CategoryId = perm_cache.CategoryId'; + INNER JOIN ' . TABLE_PREFIX . 'Categories c ON c.CategoryId = ci.CategoryId'; - list ($view_perm, $view_filter) = $this->GetPermissionClause($prefix, 'perm_cache'); - $where_clauses = Array ( - $view_filter, 'perm_cache.PermId = '.$view_perm, 'ci.PrimaryCat = 1', 'c.Status = '.STATUS_ACTIVE, - ); + list ($sql, $where_clause) = $this->attachViewPermissionCheck($prefix, $sql, $where_clause, 'ci.CategoryId'); - if ($today) { + if ( $today ) { - $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); + $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); - $where_clauses[] = 'item_table.CreatedOn >= '.$today_date; + $where_clause[] = 'item_table.CreatedOn >= ' . $today_date; - } + } - $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + $sql .= ' WHERE (' . implode(') AND (', $where_clause) . ')'; return (int)$this->Conn->GetOne($sql); } @@ -214,42 +217,66 @@ */ function CategoryCount($today = false) { - $cache_key = 'category_count[%CSerial%]'; + $cache_key = 'category_count[%CSerial%]'; - if ($today) { + if ( $today ) { - $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); - $cache_key .= ':date=' . $today_date; - } + $today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y')); + $cache_key .= ':date=' . $today_date; + } - $count = $this->Application->getCache($cache_key); + $count = $this->Application->getCache($cache_key); - if ($count === false) { + if ( $count === false ) { - $sql = 'SELECT COUNT(*) - FROM ' . $this->Application->getUnitOption('c', 'TableName') . ' c - INNER JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm_cache ON c.CategoryId = perm_cache.CategoryId'; - - list ($view_perm, $view_filter) = $this->GetPermissionClause('c', 'perm_cache'); - - $where_clauses = Array ( - $view_filter, - 'perm_cache.PermId = ' . $view_perm, + $where_clause = Array( - 'c.Status = ' . STATUS_ACTIVE, + 'c.Status = ' . STATUS_ACTIVE, ); + $sql = 'SELECT COUNT(*) + FROM ' . $this->Application->getUnitOption('c', 'TableName') . ' c'; + + list ($sql, $where_clause) = $this->attachViewPermissionCheck('c', $sql, $where_clause); + - if ($today) { + if ( $today ) { - $where_clauses[] = 'c.CreatedOn >= ' . $today_date; + $where_clause[] = 'c.CreatedOn >= ' . $today_date; - } + } - $sql .= ' WHERE ('.implode(') AND (', $where_clauses).')'; + $sql .= ' WHERE (' . implode(') AND (', $where_clause) . ')'; $count = $this->Conn->GetOne($sql); - if ($count !== false) { + if ( $count !== false ) { $this->Application->setCache($cache_key, $count); } - } + } - return $count; + return $count; + } + + /** + * Updates given data to make SQL use view permission check. + * + * @param string $prefix Unit config prefix. + * @param string $sql Sql. + * @param array $where_clause Where clause. + * @param string $category_field Field, where to get Category ID. + * + * @return array + * @access public + */ + public function attachViewPermissionCheck($prefix, $sql, $where_clause, $category_field = 'c.CategoryId') + { + if ( !$this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) { + return array($sql, $where_clause); + } + + list ($view_perm, $view_filter) = $this->GetPermissionClause($prefix, 'perm_cache'); + + $where_clause[] = $view_filter; + $where_clause[] = 'perm_cache.PermId = ' . $view_perm; + + $sql .= ' INNER JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm_cache ON ' . $category_field . ' = perm_cache.CategoryId'; + + return array($sql, $where_clause); } /** \ No newline at end of file