<?php
namespace App\Mvc\Controller\Window\PaginatedListing; abstract class PaginatedListing extends \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract { protected $formSubmitButtonName='save'; protected $insertIdRecord = null; protected $updateIdRecord = null; protected function initialize() { parent::initialize(); $this->actionsCfg['insert'] = [ 'checkOption' => true, ]; $this->actionsCfg['update'] = [ 'checkOption' => true, ]; $this->actionsCfg['delete'] = [ 'checkOption' => true, ]; } protected function actionInsert($R) { $msgs = \App\MessagesCollector::create(); if (!$R[$this->formSubmitButtonName]) { $formData=$this->insert_getDefaultFormData($R); } else { $formData=$R; } $modelOutput = null; if ($R[$this->formSubmitButtonName]) { $modelOutput = $this->insert_doInsert($formData, $msgs); if ($modelOutput and !$this->insertIdRecord) { throw new \Exception('The `insertIdRecord` class attribute is not set although the model returned `true`.'); } } $view = $this->buildViewObjectFromCurrentModuleForAction($this->getAction()); return $this->insert_returnResponse($view, $formData, $modelOutput, $msgs); } protected function insert_returnResponse(\App\Mvc\ViewAbstract $view, array $formData, $modelOutput, \App\MessagesCollector $msgs) { $view->addInput('formData', $formData); if ($modelOutput) { $view->setClosePopup(true); } $view->getMessagesCollector()->appendMessages($msgs); $view->addInput('formSubmitButtonName', $this->formSubmitButtonName); $view->addInput('idRecord', $this->insertIdRecord); return new \Engine\Mvc\Response\Html($view->render()); } protected function insert_getDefaultFormData($R) { return []; } protected function actionUpdate($R) { $msgs = \App\MessagesCollector::create(); $this->updateIdRecord = $R['id']; if (!$R[$this->formSubmitButtonName]) { $formData = $this->update_getDefaultFormData($this->updateIdRecord); if ($formData!==null) { $formData['id']=$this->updateIdRecord; } } else { $formData=$R; } $modelOutput = null; if ($R[$this->formSubmitButtonName]) { $modelOutput = $this->update_doUpdate($formData, $msgs); } $view = $this->buildViewObjectFromCurrentModuleForAction($this->getAction()); $view->addInput('formData', $formData); if ($modelOutput) { $view->setClosePopup(true); } $view->getMessagesCollector()->appendMessages($msgs); $view->addInput('formSubmitButtonName', $this->formSubmitButtonName); $view->addInput('idRecord', $this->updateIdRecord); return new \Engine\Mvc\Response\Html($view->render()); } protected function bulkActionDelete(array $recordsIds, \App\MessagesCollector $msgs, $R) { $this->delete_doDelete($recordsIds, $msgs, $R); $successTexts = []; foreach($msgs->getMessages() as $msg) { if ($msg->getType()=='success') { $successTexts[$msg->getText()]++; } } if (count($successTexts)==1 and current($successTexts)>1) { $msgs->removeSuccess(); $msgs->addSuccess(sprintf($this->globalLang['stdModule']['recordsSuccessfullyDeleted'], current($successTexts))); } } abstract protected function insert_doInsert($formData, \App\MessagesCollector $msgs); abstract protected function update_getDefaultFormData($idEntity); abstract protected function update_doUpdate($formData, \App\MessagesCollector $msgs); abstract protected function delete_doDelete(array $recordsIds, \App\MessagesCollector $msgs, array $R) ; } 