File manager - Edit - /home/c14075/dragmet-ural.ru/www/bitrix/components/webdebug/seo.admin.list/class.php
Back
<?php use \WD\Seo\Helper, \WD\Seo\LogTable as Log; class CWDSeoAdminList extends \CBitrixComponent { const LANG = 'WD_SEO_ADMIN_LIST_'; protected $strRights = 'D'; // D - denied, R - read, W - write protected $arTableFields = []; protected $arFilterFields = []; protected $arFilterResult = []; protected $obItems = null; // Modify input params public function onPrepareComponentParams($arParams){ # Allow $arParams['ALLOW_EDIT'] = $arParams['ALLOW_EDIT'] == 'Y'; $arParams['ALLOW_DELETE'] = $arParams['ALLOW_DELETE'] == 'Y'; $arParams['ALLOW_ALL'] = $arParams['ALLOW_ALL'] == 'Y'; $arParams['ALLOW_COPY'] = $arParams['ALLOW_COPY'] == 'Y'; # Prepare script paths (urls) for list and edit $arParams['SCRIPT_LIST'] = $GLOBALS['APPLICATION']->getCurPage(); if(!isset($arParams['SCRIPT_EDIT']) || !Helper::strlen($arParams['SCRIPT_EDIT'])){ $arParams['SCRIPT_EDIT'] = preg_replace('#_list\.php$#', '_edit.php', $arParams['SCRIPT_LIST']); if($arParams['SCRIPT_EDIT'] == $arParams['SCRIPT_LIST']){ unset($arParams['SCRIPT_EDIT']); } } return $arParams; } // Entry point public function executeComponent(){ $this->includeJsCss(); $this->checkUserRights(); $this->collectTableFields(); $this->createAdminList(); $this->initFilter(); $this->addGroupActions(); $this->handleEditAction(); $this->handleGroupAction(); $this->createContextMenu(); $this->addHeaders(); $this->getItems(); $this->writeItems(); $this->display(); } // Get $arTableFields from $this public function getTableFields(){ return $this->arTableFields; } // Get $arFilterFields from $this public function getFilterFields(){ return $this->arFilterFields; } // Get $arFilterResult from $this public function getFilterResult(){ return $this->arFilterResult; } // Collect fields from class map public function collectTableFields(){ $this->arTableFields = array_map(function($obItem){ $arItem = [ #'CLASS' => $obItem, 'NAME' => $obItem->getTitle(), 'TYPE' => $obItem->getDataType(), 'AVAILABLE' => $obItem->getParameter('wd_list_available') !== false, 'EDITABLE' => $obItem->getParameter('wd_list_editable') !== false, 'DEFAULT' => $obItem->getParameter('wd_list_default') !== false, 'SORTABLE' => $obItem->getParameter('wd_list_sortable') !== false, 'FILTERABLE' => $obItem->getParameter('wd_list_filterable') !== false, 'IS_LAMP' => $obItem->getParameter('wd_list_lamp') === true, // Boolean lamps: green/red ]; # Special cases if($arItem['TYPE'] == 'enum'){ $arItem['VALUES'] = Helper::getEnumValues($this->arParams['CLASS'], $obItem->getColumnName()); sort($arItem['VALUES']); if($arItem['VALUES'] == ['N', 'Y']){ $arItem['IS_BOOLEAN'] = true; } } if(is_callable($this->arParams['CALLBACK']['MODIFY_TABLE_FIELD'])){ call_user_func_array($this->arParams['CALLBACK']['MODIFY_TABLE_FIELD'], [ $this, $this->arResult['ADMIN_LIST'], $obItem, &$arItem, ]); } return $arItem; }, $this->arParams['CLASS']::getMap()); } // Check field is allowed for edit public function isFieldEditable(string $strField){ if(!$this->arParams['ALLOW_EDIT']){ return false; } return is_array($this->arTableFields[$strField]) && $this->arTableFields[$strField]['EDITABLE']; } // Add JS and CSS to page public function includeJsCss(){ \CJSCore::init('jquery3'); $GLOBALS['APPLICATION']->setAdditionalCss($this->getPath().'/style.css'); } // Check user rights public function checkUserRights(){ $this->strRights = $GLOBALS['APPLICATION']->getGroupRight($strModuleId); if($this->strRights < 'R') { $GLOBALS['APPLICATION']->authForm(Helper::getMessage('ACCESS_DENIED')); } } // Get list (table) id public function getListId(){ return $this->arParams['CLASS']::getTableName(); } // Set initial ListAdminPage & table public function createAdminList(){ $obSort = new \CAdminUiSorting($this->getListId(), $this->arParams['DEFAULT_SORT_FIELD'], $this->arParams['DEFAULT_SORT_ORDER']); $this->arResult['ADMIN_LIST'] = new \CAdminUiList($this->getListId(), $obSort);// Start output $this->arResult['ADMIN_LIST']->checkListMode(); } // Add filter public function initFilter(){ $this->arFilterFields = []; foreach($this->arTableFields as $strField => $arField){ $strType = $arField['TYPE']; $arItems = null; $strFilterable = in_array($arField['TYPE'], ['string', 'text']) ? '%' : ''; if($strField == 'ACTIVE'){ $strType = 'list'; $arItems = Helper::getSelectBooleanValues(); $strFilterable = ''; } $arFilterItem = [ 'id' => $strField, 'name' => $arField['NAME'], 'filterable' => $strFilterable, 'type' => $strType, 'items' => $arItems, 'default' => $arField['DEFAULT'], ]; $bHideFilterItem = null; if(is_callable($this->arParams['CALLBACK']['MODIFY_FILTER_ITEM'])){ $bHideFilterItem = call_user_func_array($this->arParams['CALLBACK']['MODIFY_FILTER_ITEM'], [ $this, $this->arResult['ADMIN_LIST'], $strField, $arField, &$arFilterItem, ]); } if($bHideFilterItem === false){ unset($this->arTableFields[$strField]); continue; } $this->arFilterFields[] = $arFilterItem; } $this->arFilterResult = []; $this->arResult['ADMIN_LIST']->addFilter($this->arFilterFields, $this->arFilterResult); $arCustomFilter = is_array($this->arParams['FILTER']) ? $this->arParams['FILTER'] : $GLOBALS[$this->arParams['FILTER']]; if(is_array($arCustomFilter)){ $this->arFilterResult = array_merge($this->arFilterResult, $arCustomFilter); } if(is_callable($this->arParams['CALLBACK']['MODIFY_FILTER'])){ $bHideFilterItem = call_user_func_array($this->arParams['CALLBACK']['MODIFY_FILTER'], [ $this, $this->arResult['ADMIN_LIST'], &$this->arFilterResult, ]); } } // Declare group actions public function addGroupActions(){ $arActions = []; if($this->arParams['ALLOW_EDIT']){ $arActions['edit'] = true; } if($this->arParams['ALLOW_DELETE']){ $arActions['delete'] = true; } if($this->arParams['ALLOW_ALL']){ $arActions['for_all'] = true; } if(isset($this->arTableFields['ACTIVE']) && $this->isFieldEditable('ACTIVE')){ $arActions['activate'] = Helper::getMessage('MAIN_ADMIN_LIST_ACTIVATE'); $arActions['deactivate'] = Helper::getMessage('MAIN_ADMIN_LIST_DEACTIVATE'); } $this->arResult['ADMIN_LIST']->addGroupActionTable($arActions); } // Processing with single row change table data actions public function handleEditAction(){ if($this->arResult['ADMIN_LIST']->editAction() && $this->strRights == 'W') { Helper::obRestart(); @set_time_limit(0); foreach($this->getSaveFields() as $intRowId => $arFields) { $intRowId = intVal($intRowId); if(!$this->arResult['ADMIN_LIST']->isUpdated($intRowId)) { continue; } $obRow = $this->arParams['CLASS']::getList([ 'filter' => ['ID' => $intRowId], 'select' => ['ID'], 'limit' => 1, ]); if($arRow = $obRow->fetch()){ Helper::startTransaction(); $obResult = $this->arParams['CLASS']::update($intRowId, $arFields); if($obResult->isSuccess()) { Helper::commitTransaction(); } else{ $this->arResult['ADMIN_LIST']->addGroupError(Helper::getMessage(static::LANG.'ERROR_EDIT_ACTION_UPDATE', ['#ID#' => $intRowId]), $intRowId); Helper::rollbackTransaction(); } } else { $this->arResult['ADMIN_LIST']->addGroupError(Helper::getMessage(static::LANG.'ERROR_EDIT_ACTION_NOT_FOUND', ['#ID#' => $intRowId]), $intRowId); } } } } // Processing with group change table data actions public function handleGroupAction(){ if($this->arResult['ADMIN_LIST']->groupAction() && $this->strRights == 'W') { Helper::obRestart(); @set_time_limit(0); $strAction = $this->getGroupAction(); $arId = $this->getGroupItemsId(); foreach($arId as $intId) { $obRow = $this->arParams['CLASS']::getList([ 'filter' => ['ID' => $intId], 'select' => ['ID'], 'limit' => 1, ]); if($arRow = $obRow->fetch()){ Helper::startTransaction(); $bSuccess = false; $strError = ''; switch($strAction) { case 'delete': $obResult = $this->arParams['CLASS']::delete($intId); if(!($bSuccess = $obResult->isSuccess())){ $strError = Helper::getMessage(static::LANG.'ERROR_GROUP_ACTION_DELETE', ['#ID#' => $intId]); } break; case 'activate': $arUpdateFields = [ 'ACTIVE' => 'Y', ]; $obResult = $this->arParams['CLASS']::update($intId, $arUpdateFields); if(!($bSuccess = $obResult->isSuccess())) { $strError = Helper::getMessage(static::LANG.'ERROR_GROUP_ACTION_ACTIVATE', ['#ID#' => $intId]); } break; case 'deactivate': $arUpdateFields = [ 'ACTIVE' => 'N', ]; $obResult = $this->arParams['CLASS']::update($intId, $arUpdateFields); if(!($bSuccess = $obResult->isSuccess())) { $strError = Helper::getMessage(static::LANG.'ERROR_GROUP_ACTION_DEACTIVATE', ['#ID#' => $intId]); } break; } if($bSuccess){ Helper::commitTransaction(); } else { Helper::rollbackTransaction(); if($this->arParams['CLASS'] != \WD\Seo\LogTable::class){ if(Helper::strlen($strError)){ $this->arResult['ADMIN_LIST']->addGroupError($strError); } Log::error('ADMIN_LIST_GROUP_ERROR_1', $strError); } } } elseif($this->arParams['CLASS'] != \WD\Seo\LogTable::class) { $strError = Helper::getMessage(static::LANG.'ERROR_GROUP_ACTION_NOT_FOUND', ['#ID#' => $intId]); $this->arResult['ADMIN_LIST']->addGroupError($strError); Log::warning('ADMIN_LIST_GROUP_ERROR_2', $strError); } } } } // Get group action public function getGroupAction(){ $strResult = ''; if(method_exists($this->arResult['ADMIN_LIST'], 'getAction')){ $strResult = $this->arResult['ADMIN_LIST']->getAction(); } elseif(Helper::strlen($_REQUEST['action_button'])) { $strResult = $_REQUEST['action_button']; } elseif(Helper::strlen($_REQUEST['action'])) { $strResult = $_REQUEST['action']; } return $strResult; } // Get saved (on group action) IDs public function getGroupItemsId(){ $arResult = []; if($this->arResult['ADMIN_LIST']->isGroupActionToAll()){ $resItems = $this->arParams['CLASS']::getList([ 'filter' => $this->arFilterResult, 'select' => ['ID'], ]); while($arItem = $resItems->fetch()) { $arResult[] = intVal($arItem['ID']); } } elseif(is_array($_POST['ID'])){ foreach($_POST['ID'] as $intId){ $arResult[] = intVal($intId); } } elseif($_POST['ID']){ $arResult[] = intVal($_POST['ID']); } return $arResult; } // Add panel menu button public function createContextMenu(){ if(is_array($this->arParams['CONTEXT_MENU'])){ # Replace some macros $arReplace = [ '#PAGE_EDIT#' => $this->getEditUrl($intRowId), '#PAGE_COPY#' => $this->getEditUrl($intRowId, ['copy' => 'Y']), ]; foreach($this->arParams['CONTEXT_MENU'] as &$arItem){ if(Helper::strlen($arItem['LINK'])){ $arItem['LINK'] = str_replace(array_keys($arReplace), array_values($arReplace), $arItem['LINK']); } } unset($arItem); # Attach to list $this->arResult['ADMIN_LIST']->addAdminContextMenu($this->arParams['CONTEXT_MENU']); } } // Add columns headers public function addHeaders(){ $arHeaders = []; foreach($this->arTableFields as $strField => $arField){ if($arField['AVAILABLE']){ $arHeaders[] = [ 'id' => $strField, 'content' => $arField['NAME'], 'sort' => $arField['SORTABLE'] ? $strField : false, 'align' => isset($this->arParams['CUSTOM_SORT'][$strField]) ? $this->arParams['CUSTOM_SORT'][$strField] : (in_array($arField['TYPE'], ['integer', 'float']) ? 'right' : 'left'), 'default' => $arField['DEFAULT'], ]; } } $this->arResult['ADMIN_LIST']->addHeaders($arHeaders); } // Process items public function getItems(){ // Get sort params: $by and $order $by = $this->arResult['ADMIN_LIST']->sort->getField(); $order = $this->arResult['ADMIN_LIST']->sort->getOrder(); # Prepare get full list $arQuery = [ 'filter' => $this->arFilterResult, 'order' => [$by => $order], ]; # Get count $arCountQuery = $arQuery; $arCountQuery['order'] = []; $arCountQuery['select'][] = 'CNT'; if(($key = array_search('*', $arCountQuery['select'])) !== false){ unset($arCountQuery['select'][$key]); } $arCountQuery['runtime']['CNT'] = new \Bitrix\Main\Entity\ExpressionField('CNT', 'COUNT(*)'); $intTotalCount = $this->arParams['CLASS']::getList($arCountQuery)->fetch()['CNT']; # Get items list $arNavParams = \CDBResult::getNavParams(\CAdminUiResult::getNavSize($this->getListId())); if(is_array($arNavParams) && !empty($arNavParams) && !$arNavParams['SHOW_ALL']){ $intPage = max(1, intVal($arNavParams['PAGEN'])); $intPageMax = max(1, ceil($intTotalCount / intVal($arNavParams['SIZEN']))); if($intPage > $intPageMax){ $intPage = $arNavParams['PAGEN'] = $intPageMax; } $intPage = min($intPage, $intPageMax); $intPageSize = max(1, intVal($arNavParams['SIZEN'])); $arQuery['limit'] = $intPageSize; $arQuery['offset'] = $intPageSize * ($intPage - 1); } $this->obItems = $this->arParams['CLASS']::getList($arQuery); $this->obItems = new \CAdminUiResult($this->obItems, $this->getListId()); # Nav if(is_array($arNavParams) && !empty($arNavParams) && !$arNavParams['SHOW_ALL']){ $this->obItems->navStart($arNavParams['SIZEN'], $arNavParams['SHOW_ALL'], $arNavParams['PAGEN']); $this->obItems->NavRecordCount = $intTotalCount; $this->obItems->NavPageCount = ceil($intTotalCount / $arNavParams['SIZEN']); $this->obItems->NavPageNomer = $arNavParams['PAGEN']; $this->obItems->nSelectedCount = $intTotalCount; } else{ $this->obItems->navStart(); } $this->obItems->bShowAll = false; // Set page navigation $this->arResult['ADMIN_LIST']->setNavigationParams($this->obItems, []); } // Write items to list public function writeItems(){ // Build items list while ($arRow = $this->obItems->fetch()) { $this->buildRow($arRow); } } // Process single item public function buildRow(array $arRow){ $intRowId = intVal($arRow['ID']); $strEditUrl = $this->getEditUrl($intRowId); $obRow = &$this->arResult['ADMIN_LIST']->addRow($intRowId, $arRow, $strEditUrl); # Apply view fields && edit fiels (from $arParams) if(is_callable($this->arParams['CALLBACK']['ROW'])){ call_user_func_array($this->arParams['CALLBACK']['ROW'], [ $this, $this->arResult['ADMIN_LIST'], $obRow, $arRow, $intRowId, ]); } # Special cases foreach($this->arTableFields as $strField => $arField){ # Boolean field (Y/ N) if($arField['IS_BOOLEAN']){ if($this->noEdit($obRow, $strField)){ # (first edit, then view) $view = $obRow->aFields[$strField]['view']; $obRow->addCheckField($strField, $arRow[$strField]); $obRow->aFields[$strField]['view'] = $view; } if($this->noView($obRow, $strField)){ # (first edit, then view) $bTrue = $arRow[$strField] == 'Y'; $strHtml = Helper::ucFirst(Helper::getMessage('MAIN_'.($bTrue ? 'YES' : 'NO'))); if($arField['IS_LAMP']){ $strHtml = sprintf('<img src="/bitrix/themes/.default/images/lamp/%s.gif" alt="" width="14" height="14">', $bTrue ? 'green' : 'red'); } $obRow->addViewField($strField, $strHtml); } } # Enums if($arField['TYPE'] == 'enum'){ if($this->noEdit($obRow, $strField) || $this->noView($obRow, $strField)){ $mEnumsParams = $this->arParams['ENUMS'][$strField]; $arEnums = []; if(is_string($mEnumsParams) && strlen($mEnumsParams)){ $arEnums = Helper::getEnumValuesLang($this->arParams['CLASS'], $strField, $mEnumsParams); } elseif(is_array($mEnumsParams) && !empty($mEnumsParams)){ $arEnums = $mEnumsParams; } if(is_array($arEnums) && !empty($arEnums)){ if($this->noEdit($obRow, $strField)){ # (first edit, then view) $view = $obRow->aFields[$strField]['view']; $obRow->addSelectField($strField, $arEnums); $obRow->aFields[$strField]['view'] = $view; } if($this->noView($obRow, $strField)){ # (first edit, then view) $obRow->addViewField($strField, $arEnums[$arRow[$strField]]); } } } } # Add another edit fields (first edit, then view) if($this->noEdit($obRow, $strField)){ $view = $obRow->aFields[$strField]['view']; $obRow->addInputField($strField); $obRow->aFields[$strField]['view'] = $view; } # Add another view fields (first edit, then view) if($this->noView($obRow, $strField)){ $strView = $arRow[$strField]; if(is_array($this->arParams['EDIT_URL_FIELDS']) && in_array($strField, $this->arParams['EDIT_URL_FIELDS'])){ $strView = sprintf('<a href="%s">%s</a>', $strEditUrl, $arRow[$strField]); } elseif(is_array($this->arParams['ENUMS']) && isset($this->arParams['ENUMS'][$strField])){ $strView = $this->arParams['ENUMS'][$strField][$arRow[$strField]]; } $obRow->addViewField($strField, $strView); } } # ACTIONS if(!empty($arRowActions = $this->buildRowActions($arRow))){ $obRow->addActions($arRowActions); } } // Check row has no view field with code $strField public function noView($obRow, $strField){ return !isset($obRow->aFields[$strField]['view']); } // Check row has no edit field with code $strField public function noEdit($obRow, $strField){ return !isset($obRow->aFields[$strField]['edit']) && $this->isFieldEditable($strField); } // Process actions for single item public function buildRowActions(array $arRow){ // Add row actions $arRowActions = []; if($intRowId = intVal($arRow['ID'])){ if($this->arParams['ALLOW_EDIT'] && Helper::strlen($strUrl = $this->getEditUrl($intRowId))){ $arResult[] = [ 'TEXT' => Helper::getMessage(static::LANG.'ROW_CONTEXT_EDIT'), 'LINK' => $strUrl, 'className' => $this->getIconClass('edit'), ]; $arResult[] = [ 'delimiter' => true, ]; } if($this->arParams['ALLOW_COPY'] && Helper::strlen($strCopyUrl = $this->getEditUrl($intRowId, ['copy' => 'Y']))){ $arResult[] = [ 'TEXT' => Helper::getMessage(static::LANG.'ROW_CONTEXT_COPY'), 'LINK' => $strCopyUrl, 'className' => $this->getIconClass('copy'), ]; $arResult[] = [ 'delimiter' => true, ]; } if($this->arParams['ALLOW_DELETE']){ $arResult[] = [ 'TEXT' => Helper::getMessage(static::LANG.'ROW_CONTEXT_DELETE'), 'ONCLICK' => sprintf('if(confirm(\'%s\')){%s;}', Helper::getMessage(static::LANG.'CONFIRM_DELETE', [ '#ID#' => $intRowId, ]), $this->arResult['ADMIN_LIST']->actionDoGroup($intRowId, 'delete')), 'className' => $this->getIconClass('delete'), ]; } } if(isset($this->arTableFields['ACTIVE']) && $this->isFieldEditable('ACTIVE')){ if($arRow['ACTIVE'] == 'N') { $arResult[] = [ 'TEXT' => Helper::getMessage(static::LANG.'ROW_CONTEXT_ACTIVATE'), 'ONCLICK' => $this->arResult['ADMIN_LIST']->actionDoGroup($intRowId, 'activate'), 'className' => $this->getIconClass('activate'), ]; } else { $arResult[] = [ 'TEXT' => Helper::getMessage(static::LANG.'ROW_CONTEXT_DEACTIVATE'), 'ONCLICK' => $this->arResult['ADMIN_LIST']->actionDoGroup($intRowId, 'deactivate'), 'className' => $this->getIconClass('deactivate'), ]; } } # Use callback from $arParams if(is_callable($this->arParams['CALLBACK']['ACTIONS'])){ call_user_func_array($this->arParams['CALLBACK']['ACTIONS'], [ $this, $this->arResult['ADMIN_LIST'], $arRow, $intRowId, &$arResult, ]); } # Set default $bDefault = false; foreach($arResult as $arItem){ if($arItem['DEFAULT']){ $bDefault = true; break; } } if(!$bDefault){ foreach($arResult as &$arItem){ $arItem['DEFAULT'] = true; break; } unset($arItem); } # return $arResult; } // Get url for edit page (for each row) public function getEditUrl($intId, array $arQuery=[]){ $strUrl = ''; if(Helper::strlen($this->arParams['SCRIPT_EDIT'])){ $arMoreParams = is_array($this->arParams['EDIT_URL_PARAMS']) ? $this->arParams['EDIT_URL_PARAMS'] : []; $arQuery = array_merge([ 'ID' => $intId, ], $arMoreParams, $arQuery, [ 'lang' => LANGUAGE_ID, ]); $strUrl = $this->arParams['SCRIPT_EDIT'].'?'.http_build_query($arQuery); } return $strUrl; } // Wrapper for global $FIELDS; return $FIELDS; public function getSaveFields(){ $arResult = []; $arResultTmp = is_array($GLOBALS['FIELDS']) ? $GLOBALS['FIELDS'] : []; foreach($arResultTmp as $strField => $mValue){ if(preg_match('#^(\w+)_custom$#', $strField, $arMatch)){ $arResult[$arMatch[1]] = $mValue[0]['value']; } else{ $arResult[$strField] = $mValue; } } return $arResult; } // Get full css-class for icon code public function getIconClass(string $strIconCode){ return sprintf('wd_seo_admin_list_icon_%s', $strIconCode); } // Display list protected function display(){ # Apply view fields && edit fiels (from $arParams) if(is_callable($this->arParams['CALLBACK']['END'])){ call_user_func_array($this->arParams['CALLBACK']['END'], [ $this, $this->arResult['ADMIN_LIST'], ]); } $this->arResult['ADMIN_LIST']->displayFilter($this->arFilterFields); $this->arResult['ADMIN_LIST']->displayList(); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.25 |
proxy
|
phpinfo
|
Settings