Source of file TradeMeAssignGroupController.php
Size: 11,368 Bytes - Last Modified: 2021-12-23T10:41:27+00:00
/var/www/docs.ssmods.com/process/src/src/Control/TradeMeAssignGroupController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 | <?php namespace Sunnysideup\EcommerceTrademe\Control; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\ReadonlyField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\PaginatedList; use SilverStripe\Security\Permission; use SilverStripe\Security\PermissionProvider; use SilverStripe\Security\Security; use SilverStripe\View\ArrayData; use Sunnysideup\Ecommerce\Pages\ProductGroup; use Sunnysideup\EcommerceTrademe\Api\TradeMeCategories; use Sunnysideup\EcommerceTrademe\Tasks\CreateTradeMeCsvTask; class TradeMeAssignGroupController extends Controller implements PermissionProvider { protected $getParams = []; protected $getParamsDefaults = [ 'start' => 0, 'filter' => 0, 'parentid' => 0, ]; /** * @var string */ private static $url_segment = 'tradmeadmin/set-trade-me-categories'; private static $allowed_actions = [ 'index' => 'CMS_ACCESS_TRADE_ME', 'save' => 'CMS_ACCESS_TRADE_ME', 'saveandexport' => 'CMS_ACCESS_TRADE_ME', 'Form' => 'CMS_ACCESS_TRADE_ME', ]; private static $group_filter = []; private static $template = 'TradeMeAssignGroupController_Content'; public function index($request) { return $this->RenderWith($this->Config()->get('template')); } public function getFilter() { return $this->getParams['filter'] ?? ''; } public function getFilterCount(): int { return $this->getListForForm()->count(); } public function Link($action = null) { return Director::absoluteURL($this->RelativeLink($action), $this->getParams); } public function RelativeLink($action = null) { return self::my_link($action, $this->getParams); } public static function my_link($action = null, $getParams = []) { $link = '/' . Controller::join_links(Config::inst()->get(static::class, 'url_segment'), $action); $link .= '?' . http_build_query($getParams); return $link; } public function getListForForm(): PaginatedList { $list = PaginatedList::create( $this->getListForFormInner(), $this->getRequest() ); $list->setPageLength(100); return $list; } public static function base_list(): DataList { $list = ProductGroup::get(); $filter = Config::inst()->get(TradeMeAssignGroupController::class, 'group_filter'); if (! empty($filter)) { $list = $list->filter($filter); } return $list; } public function Form() { $fields = new FieldList(); $fieldListSortable = []; $options = $this->getListProductsOnTradeMeOptions(); foreach ($this->getListForForm() as $group) { $productList = TradeMeAssignProductController::base_list(); $productList = $productList->filter(['ParentID' => $group->ID]); $productCountExists = $productList->exists(); if ($productCountExists) { $productCount = $productList->count(); $productLink = TradeMeAssignProductController::my_link('', ['parentid' => $group->ID]); $name = '___GROUP___' . $group->ID; $breadcrumb = $group->Breadcrumbs(); $breadcrumbRaw = $breadcrumb->RAW(); $breadcrumbClean = strip_tags($breadcrumbRaw); $fieldListSortable[$breadcrumbClean] = new CompositeField(); $fieldListSortable[$breadcrumbClean]->push( OptionsetField::create( 'ListProductsOnTradeMe' . $name, '', $options ) ->setValue($group->ListProductsOnTradeMe) ->addExtraClass('float-left') ); $fieldListSortable[$breadcrumbClean]->push( ReadonlyField::create( 'HEADER' . $name, DBField::create_field('HTMLText', '<a href="' . $group->CMSEditLink() . '">✎</a>'), DBField::create_field('HTMLText', $breadcrumbRaw . ' » <a href="' . $productLink . '">Edit Individual Products (' . $productCount . ')</a>') ) ->setRightTitle( '» ' . TradeMeCategories::get_title_from_id($group->getCalculatedTradeMeCategory()) ) ); $fieldListSortable[$breadcrumbClean]->push( LiteralField::create( 'HR' . $name . 'HR', '<hr />' ) ); } } ksort($fieldListSortable); foreach ($fieldListSortable as $fieldListSortableField) { $fields->push($fieldListSortableField); } foreach ($this->getHiddenFields() as $hiddenField) { $fields->push($hiddenField); } $actions = $this->getFormActions(); return new Form($this, 'Form', $fields, $actions); } public function Title() { return 'Select what Categories go to TradeMe'; } public function saveandexport($data, $form) { $this->saveInner($data, $form); $link = CreateTradeMeCsvTask::my_link(); return $this->redirect($link); } public function save($data, $form) { $this->saveInner($data, $form); return $this->redirectBack(); } public function getMainLinks() { $al = ArrayList::create(); // no filter $al->push(ArrayData::create([ 'Link' => TradeMeAssignGroupController::my_link(), 'Title' => 'Categories', 'IsCurrent' => TradeMeAssignGroupController::class === static::class, ])); $al->push(ArrayData::create([ 'Link' => TradeMeAssignProductController::my_link(), 'Title' => 'Products', 'IsCurrent' => TradeMeAssignProductController::class === static::class, ])); $this->extend('getMainLinksAdditional', $al); return $al; } public function getFilterLinks(): ArrayList { $al = ArrayList::create(); //reset filter first ... $currentStart = $this->getParams['start'] ?? 0; $this->getParams['start'] = 0; $currentFilter = $this->getParams['filter'] ?? ''; $this->getParams['filter'] = ''; // no filter $array = [ 'Link' => $this->Link(), 'LinkingMode' => $currentFilter ? 'link' : 'current', 'Title' => 'No Filter', ]; $al->push(ArrayData::create($array)); //loop through options foreach ($this->getListProductsOnTradeMeOptions() as $option) { $this->getParams['filter'] = $option; $array = [ 'Link' => $this->Link(), 'LinkingMode' => $option === $currentFilter ? 'current' : 'link', 'Title' => ucfirst($option), ]; $al->push(ArrayData::create($array)); } //set filter back $this->getParams['start'] = $currentStart; $this->getParams['filter'] = $currentFilter; return $al; } public function providePermissions(): array { return [ 'CMS_ACCESS_TRADE_ME' => [ 'name' => 'Trade Me', 'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access'), 'help' => 'Export products to TradeMe', ], ]; } /** * ### @@@@ START REPLACEMENT @@@@ ### * OLD: public function init() (ignore case) * NEW: protected function init() (COMPLEX) * EXP: Controller init functions are now protected please check that is a controller. * ### @@@@ STOP REPLACEMENT @@@@ ###. */ protected function init() { parent::init(); if (! Permission::check('CMS_ACCESS_TRADE_ME')) { return Security::permissionFailure($this); } $this->setGetParams(); } protected function getListProductsOnTradeMeOptions(): array { return DataObject::get_one(ProductGroup::class)->dbObject('ListProductsOnTradeMe')->enumValues(); } protected function setGetParams() { $potentials = $this->request->requestVars(); foreach ($this->getParamsDefaults as $key => $defaultValue) { $newValue = $potentials[$key] ?? $defaultValue; $this->getParams[$key] = $newValue; } if (! empty($this->getParams['filter'])) { $options = $this->getListProductsOnTradeMeOptions(); if (! in_array($this->getParams['filter'], $options, true)) { $this->getParams['filter'] = ''; return $this->httpError('404', 'Category does not exist'); } } } protected function getListForFormInner(): DataList { $list = TradeMeAssignGroupController::base_list(); if ($this->getParams['filter']) { $list = $list->filter(['ListProductsOnTradeMe' => $this->getParams['filter']]); } return $list; } protected function getFormActions(): FieldList { return new FieldList( FormAction::create('save', 'Save Changes'), FormAction::create('saveandexport', 'Save and Start Upload Process ...') ); } protected function getHiddenFields(): array { $arrayOfFields = []; foreach ($this->getParams as $key => $value) { $arrayOfFields[] = HiddenField::create($key)->setValue($value); } return $arrayOfFields; } protected function saveInner($data, $form) { $updateCount = 0; foreach ($data as $key => $value) { $array = explode('___', $key); if (3 === count($array)) { $field = $array[0]; $type = $array[1]; $groupId = intval($array[2]); if ('ListProductsOnTradeMe' === $field && 'GROUP' === $type) { $group = ProductGroup::get_by_id($groupId); if ($group) { if ($group->ListProductsOnTradeMe !== $value) { $group->ListProductsOnTradeMe = $value; $group->writeToStage('Stage'); $group->publish('Stage', 'Live'); ++$updateCount; } } else { user_error('Could not find Category based on ' . $key); } } } } if ($updateCount) { $form->sessionMessage('Updated ' . $updateCount . ' fields.', 'good'); } } } |