Редактирование названия товара в списке товаров

Иногда требуется переименовать товар из списка товаров не заходя внутрь формы редактирования, но такой вариант не предусмотрен движком.

Для реализации такой хотелки нужно воспользоваться хуками системы.

<?php
class Shop_Item_Name_Observer {

	static public function onBeforeExecute (Admin_Form_Controller $oController, $args = []) {
		$oAdminForm = $oController->getAdminForm();
		if ($oAdminForm->id == 65) {
			$oDataSet = $oController->getDataset(1);
			$oDataSet->changeField('name', 'type', '1');
			$oDataSet->changeField('name', 'editable', '1');
			$oDataSet->changeField('name', 'name', 'namesBackend');
		}
	}

	static public function onCallnamesBackend ($object, $args) {
		return $object->name;
	}

	static public function onBeforeShowField (Admin_Form_Controller $oController, $args) {
		$oAdminForm = $oController->getAdminForm();
		if ($oAdminForm->id == 65) {
			if ($args[0] instanceof Shop_Item_Model && $args[1]->name == 'name') {
				$object = $args[0]->shortcut_id
					? $args[0]->Shop_Item
					: $args[0];
				if (!$object->active || !$args[0]->active) {
					?>
					<div class="d-flex align-items-center line-through inactive">
				<?php
				}
			}
		}
	}

	static public function onAfterShowField (Admin_Form_Controller $oController, $args) {
		$oAdminForm = $oController->getAdminForm();
		if ($oAdminForm->id == 65) {
			if ($args[0] instanceof Shop_Item_Model && $args[1]->name == 'name') {
				$object = $args[0]->shortcut_id
					? $args[0]->Shop_Item
					: $args[0];
				
				$oCore_Html_Entity_Div = Core_Html_Entity::factory('Div')
					->class('d-flex align-items-center');
				
				if ($args[0]->closed) {
					$oCore_Html_Entity_Div
						->add(
							Core_Html_Entity::factory('I')
								->class('fa fa-lock darkorange locked-item order-first')
								->title(Core::_('Shop_Item.closed'))
						);
				}
				
				if (is_null(Core_Array::getGet('shop_item_id')) && $object->modification_id) {
					$oCore_Html_Entity_Div
						->add(
							Core_Html_Entity::factory('I')->class('fa fa-code-fork margin-right-5 order-first')
						);
				}
				
				if ($object->modification_id) {
					$oCore_Html_Entity_Div->value .= '<span class="small darkgray margin-left-5"> → ' . htmlspecialchars($object->Modification->name) . '</span>';
				} elseif ($object->shortcut_id) {
					$oCore_Html_Entity_Div->value .= '<span class="small darkgray margin-left-5"> → ' . htmlspecialchars($object->Shop_Item->name) . '</span>';
				}
				
				// Barcodes
				$aShop_Item_Barcodes = $args[0]->Shop_Item_Barcodes->findAll(FALSE);
				foreach ($aShop_Item_Barcodes as $oShop_Item_Barcode) {
					$oCore_Html_Entity_Div->add(
						Core_Html_Entity::factory('Span')
							// ->class('label label-sm darkgray bordered-1 bordered-gray margin-left-5')
							->class('badge badge-gray inverted margin-left-5')
							->value($oShop_Item_Barcode->value)
					);
				}
				
				$bRightTime = ($args[0]->start_datetime == '0000-00-00 00:00:00' || time() > Core_Date::sql2timestamp($args[0]->start_datetime))
					&& ($args[0]->end_datetime == '0000-00-00 00:00:00' || time() < Core_Date::sql2timestamp($args[0]->end_datetime));
				
				!$bRightTime && $oCore_Html_Entity_Div->class('wrongTime');
				
				// Зачеркнут в зависимости от статуса родительского товара или своего статуса
				if (!$object->active || !$args[0]->active) {
					echo '</div>';
					$oCore_Html_Entity_Div->class('inactive');
				} elseif ($bRightTime) {
					$oCurrentAlias = $object->Shop->Site->getCurrentAlias();
					
					if ($oCurrentAlias) {
						$href = ($object->Shop->Structure->https ? 'https://' : 'http://')
							. $oCurrentAlias->name
							. $object->Shop->Structure->getPath()
							. $object->getPath();
						
						$oCore_Html_Entity_Div->add(
							Core_Html_Entity::factory('A')
								->href($href)
								->target('_blank')
								->add(
									Core_Html_Entity::factory('I')->class('fa fa-external-link margin-left-5')
								)
						);
					}
				} elseif (!$bRightTime) {
					$oCore_Html_Entity_Div
						->add(
							Core_Html_Entity::factory('I')->class('fa fa-clock-o black margin-left-5')
						);
				}
				
				$oCore_Html_Entity_Div->execute();
				
				$args[0]->showSetBadges();
			}
		}
	}
}
Core_Event::attach('Admin_Form_Controller.onBeforeExecute', 'Shop_Item_Name_Observer::onBeforeExecute');
Core_Event::attach('shop_item.onCallnamesBackend', 'Shop_Item_Name_Observer::onCallnamesBackend');
Core_Event::attach('Admin_Form_Controller.onBeforeShowField', 'Shop_Item_Name_Observer::onBeforeShowField');
Core_Event::attach('Admin_Form_Controller.onAfterShowField', 'Shop_Item_Name_Observer::onAfterShowField');

02.11.2024