diff --git a/lib/EntityManager.php b/lib/EntityManager.php index fab6214..23c754e 100644 --- a/lib/EntityManager.php +++ b/lib/EntityManager.php @@ -244,7 +244,7 @@ public function delete() $model = $this->modelClass; - $result = $model::delete($this->itemId); // удаляем основную сущность + $result = $model::delete($this->helper->getPk()); // удаляем основную сущность if(!$result->isSuccess()){ // если не удалилась $db->rollbackTransaction(); // то восстанавливаем зависимые сущности return $result; // возвращаем ошибку diff --git a/lib/helper/AdminBaseHelper.php b/lib/helper/AdminBaseHelper.php index f71ca7c..d1524c6 100644 --- a/lib/helper/AdminBaseHelper.php +++ b/lib/helper/AdminBaseHelper.php @@ -6,6 +6,7 @@ use Bitrix\Main\LoaderException; use Bitrix\Main\Localization\Loc; use Bitrix\Main\ModuleManager; +use DigitalWand\AdminHelper\EntityManager; use DigitalWand\AdminHelper\Widget\HelperWidget; use Bitrix\Main\Entity\DataManager; use Bitrix\Highloadblock as HL; @@ -108,6 +109,15 @@ abstract class AdminBaseHelper */ static protected $model; + /** + * @var string + * Имя класса используемого менеджера сущностей. Используется для выполнения CRUD-операций. + * + * @see DataManager + * @api + */ + static protected $entityManager = '\DigitalWand\AdminHelper\EntityManager'; + /** * @var string * Назвние модуля данной модели. @@ -598,6 +608,17 @@ public function pk() return 'ID'; } + /** + * Возвращает значение первичного ключа таблицы используемой модели + * @return array|int|null + * + * @api + */ + public function getPk() + { + return isset($_REQUEST['FIELDS'][$this->pk()]) ? $_REQUEST['FIELDS'][$this->pk()] : $_REQUEST[$this->pk()]; + } + /** * Возвращает первичный ключ таблицы используемой модели разделов. Для HL-инфоблоков битрикс - всегда ID. * Но может поменяться для какой-либо другой сущности. diff --git a/lib/helper/AdminEditHelper.php b/lib/helper/AdminEditHelper.php index 518a54a..1affd4a 100644 --- a/lib/helper/AdminEditHelper.php +++ b/lib/helper/AdminEditHelper.php @@ -119,7 +119,7 @@ public function __construct(array $fields, array $tabs = array()) if ($this->editAction()) { if (isset($_REQUEST['apply'])) { $id = $this->data[$this->pk()]; - $url = $this->app->GetCurPageParam($this->pk() . '=' . $id); + $url = $this->app->GetCurPageParam($this->pk() . '=' . (is_array($id) ? $id[$this->pk()] : $id), array('ID')); } else { if (isset($_REQUEST['save'])) { @@ -430,7 +430,7 @@ protected function editAction() if ($success) { $this->setContext(AdminEditHelper::OP_EDIT_ACTION_AFTER); $existing = false; - $id = isset($_REQUEST['FIELDS'][$this->pk()]) ? $_REQUEST['FIELDS'][$this->pk()] : $_REQUEST[$this->pk()]; + $id = $this->getPk(); if ($id) { /** @var DataManager $className */ @@ -487,9 +487,9 @@ protected function editAction() */ protected function loadElement($select = array()) { - if (isset($_REQUEST[$this->pk()])) { + if ($this->getPk() !== null) { $className = static::getModel(); - $result = $className::getById($_REQUEST[$this->pk()]); + $result = $className::getById($this->getPk()); return $result->fetch(); } @@ -515,9 +515,8 @@ protected function loadElement($select = array()) */ protected function saveElement($id = null) { - $className = static::getModel(); - $entityManager = new EntityManager($className, $this->data, $id, $this); - + /** @var EntityManager $entityManager */ + $entityManager = new static::$entityManager(static::getModel(), empty($this->data) ? array() : $this->data, $id, $this); $saveResult = $entityManager->save(); $this->addNotes($entityManager->getNotes()); @@ -542,9 +541,9 @@ protected function deleteElement($id) return false; } - - $className = static::getModel(); - $entityManager = new EntityManager($className, array(), $id, $this); + + /** @var EntityManager $entityManager */ + $entityManager = new static::$entityManager(static::getModel(), empty($this->data) ? array() : $this->data, $id, $this); $deleteResult = $entityManager->delete(); $this->addNotes($entityManager->getNotes()); diff --git a/lib/helper/AdminListHelper.php b/lib/helper/AdminListHelper.php index 7b4d1da..1913458 100644 --- a/lib/helper/AdminListHelper.php +++ b/lib/helper/AdminListHelper.php @@ -172,7 +172,7 @@ public function __construct(array $fields, $isPopup = false) $this->prepareAdminVariables(); $className = static::getModel(); - $oSort = new \CAdminSorting($this->getListTableID(), static::pk(), "desc"); + $oSort = new \CAdminSorting($this->getListTableID(), $this->pk(), "desc"); $this->list = new \CAdminList($this->getListTableID(), $oSort); $this->list->InitFilter($this->arFilterFields); @@ -419,7 +419,7 @@ protected function getContextMenu() $params = $this->additionalUrlParams; $sectionModel = $sectionEditHelper::getModel(); $sectionField = $sectionEditHelper::getSectionField(); - $section = $sectionModel::getById($_GET['ID'])->Fetch(); + $section = $sectionModel::getById($this->getPk())->Fetch(); if ($this->isPopup()) { $params = array_merge($_GET); } @@ -516,14 +516,15 @@ protected function groupActions($IDs, $action) if ($sectionEditHelperClass) { $sectionField = !isset($_REQUEST['model']) ? static::getSectionField() : $sectionEditHelperClass::getSectionField(); - $element = $className::getById($IDs[0])->Fetch(); + $element = $className::getById($this->getPk())->Fetch(); if ($element[$sectionField]) { $params['ID'] = $element[$sectionField]; } } foreach ($IDs as $id) { - $entityManager = new EntityManager($className, array(), $id, $this); + /** @var EntityManager $entityManager */ + $entityManager = new static::$entityManager(static::getModel(), empty($this->data) ? array() : $this->data, $id, $this); $result = $entityManager->delete(); $this->addNotes($entityManager->getNotes()); if(!$result->isSuccess()){ @@ -541,7 +542,7 @@ protected function groupActions($IDs, $action) if ($action == 'delete-section') { if ($this->hasDeleteRights()) { - $section = $sectionClassName::getById($IDs[0])->Fetch(); + $section = $sectionClassName::getById($this->getPk())->Fetch(); $sectionField = $sectionEditHelperClass::getSectionField(); $params = $_GET; unset($params['action']); @@ -550,9 +551,9 @@ protected function groupActions($IDs, $action) if ($section[$sectionField]) { $params['ID'] = $section[$sectionField]; } - foreach ($IDs as $id) { - $sectionClassName::delete($id); - } + + $sectionClassName::delete($this->getPk()); + LocalRedirect($listHelperClass::getUrl($params)); } else { @@ -750,8 +751,8 @@ public function buildList($sort) } $className = static::getModel(); - $visibleColumns[] = static::pk(); - $sectionsVisibleColumns[] = static::sectionPk(); + $visibleColumns[] = $this->pk(); + $sectionsVisibleColumns[] = $this->sectionPk(); $raw = array( 'SELECT' => $visibleColumns, @@ -988,7 +989,7 @@ protected function getMixedData($sectionsVisibleColumns, $elementVisibleColumns, } // добавляем к выборке разделы $rsSections = $sectionModel::getList(array( - 'filter' => $sectionFilter, + 'filter' => $this->getSectionsFilter($sectionFilter), 'select' => $sectionsVisibleColumns, 'order' => $sectionSort, 'limit' => $limitData[1], @@ -1033,7 +1034,7 @@ protected function getMixedData($sectionsVisibleColumns, $elementVisibleColumns, } $elementParams = array( - 'filter' => $elementFilter, + 'filter' => $this->getElementsFilter($elementFilter), 'select' => $elementVisibleColumns, 'order' => $elementSort, ); @@ -1156,7 +1157,7 @@ protected function getRow($data, $class = false) else { $query = array_merge($this->additionalUrlParams, array( 'lang' => LANGUAGE_ID, - static::pk() => $data[static::pk()] + $this->pk() => $data[$this->pk()] )); return array($class::getUrl($query)); @@ -1303,7 +1304,7 @@ protected function addRowCell($row, $code, $data, $virtualCode = false) protected function getData($className, $filter, $select, $sort, $raw) { $parameters = array( - 'filter' => $filter, + 'filter' => $this->getElementsFilter($filter), 'select' => $select, 'order' => $sort ); @@ -1435,4 +1436,24 @@ public static function getUrl(array $params = array()) { return static::getViewURL(static::getViewName(), static::$listPageUrl, $params); } + + /** + * Кастомизация фильтра разделов + * @param $filter + * @return mixed + */ + protected function getSectionsFilter(array $filter) + { + return $filter; + } + + /** + * Кастомизация фильтра элементов + * @param $filter + * @return mixed + */ + protected function getElementsFilter($filter) + { + return $filter; + } } diff --git a/lib/widget/ComboBoxWidget.php b/lib/widget/ComboBoxWidget.php index 4157e46..ca32e79 100644 --- a/lib/widget/ComboBoxWidget.php +++ b/lib/widget/ComboBoxWidget.php @@ -161,14 +161,14 @@ protected function getVariants() { $variants = $this->getSettings('VARIANTS'); - if (is_array($variants) AND !empty($variants)) { - return $this->formatVariants($variants); - } elseif (is_callable($variants)) { + if (is_callable($variants)) { $var = $variants(); if (is_array($var)) { return $this->formatVariants($var); } + }elseif (is_array($variants) AND !empty($variants)) { + return $this->formatVariants($variants); } return array(); diff --git a/lib/widget/StringWidget.php b/lib/widget/StringWidget.php index 19ea069..bed3275 100644 --- a/lib/widget/StringWidget.php +++ b/lib/widget/StringWidget.php @@ -157,8 +157,7 @@ public function generateRow(&$row, $data) if ($this->getSettings('MULTIPLE')) { } else { if ($this->getSettings('EDIT_LINK') || $this->getSettings('SECTION_LINK')) { - $entityClass = $this->entityName; - $pk = $entityClass::getEntity()->getPrimary(); + $pk = $this->helper->pk(); if ($this->getSettings('SECTION_LINK')) { $params = $this->helper->isPopup() ? $_GET : array();