first
This commit is contained in:
@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Controller;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Nextend\Framework\Acl\Acl;
|
||||
use Nextend\Framework\Application\AbstractApplication;
|
||||
use Nextend\Framework\Application\AbstractApplicationType;
|
||||
use Nextend\Framework\Asset\AssetManager;
|
||||
use Nextend\Framework\Asset\Predefined;
|
||||
use Nextend\Framework\Form\Form;
|
||||
use Nextend\Framework\Notification\Notification;
|
||||
use Nextend\Framework\Pattern\GetPathTrait;
|
||||
use Nextend\Framework\Pattern\MVCHelperTrait;
|
||||
use Nextend\Framework\Plugin;
|
||||
use Nextend\Framework\Request\Request;
|
||||
use Nextend\SmartSlider3\Application\ApplicationSmartSlider3;
|
||||
|
||||
abstract class AbstractController {
|
||||
|
||||
use GetPathTrait;
|
||||
use MVCHelperTrait;
|
||||
|
||||
/**
|
||||
* @var AbstractApplicationType
|
||||
*/
|
||||
protected $applicationType;
|
||||
|
||||
/** @var callback[] */
|
||||
protected $externalActions = array();
|
||||
|
||||
/**
|
||||
* AbstractController constructor.
|
||||
*
|
||||
* @param AbstractApplicationType $applicationType
|
||||
*/
|
||||
public function __construct($applicationType) {
|
||||
|
||||
//PluggableController\Nextend\SmartSlider3\Application\Admin\Slider\ControllerSlider
|
||||
Plugin::doAction('PluggableController\\' . get_class($this), array($this));
|
||||
|
||||
|
||||
$this->applicationType = $applicationType;
|
||||
$this->setMVCHelper($this->applicationType);
|
||||
|
||||
AssetManager::getInstance();
|
||||
|
||||
$this->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $actionName
|
||||
* @param callback $callable
|
||||
*/
|
||||
public function addExternalAction($actionName, $callable) {
|
||||
|
||||
$this->externalActions[$actionName] = $callable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractApplication
|
||||
*/
|
||||
public function getApplication() {
|
||||
return $this->applicationType->getApplication();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractApplicationType
|
||||
*/
|
||||
public function getApplicationType() {
|
||||
return $this->applicationType;
|
||||
}
|
||||
|
||||
public function getRouter() {
|
||||
return $this->applicationType->getRouter();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $actionName
|
||||
* @param array $args
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function doAction($actionName, $args = array()) {
|
||||
|
||||
$originalActionName = $actionName;
|
||||
|
||||
if (method_exists($this, 'action' . $actionName)) {
|
||||
|
||||
call_user_func_array(array(
|
||||
$this,
|
||||
'action' . $actionName
|
||||
), $args);
|
||||
|
||||
} else if (isset($this->externalActions[$actionName]) && is_callable($this->externalActions[$actionName])) {
|
||||
|
||||
call_user_func_array($this->externalActions[$actionName], $args);
|
||||
|
||||
} else {
|
||||
|
||||
$actionName = $this->missingAction($this, $actionName);
|
||||
|
||||
if (method_exists($this, 'action' . $actionName)) {
|
||||
|
||||
call_user_func_array(array(
|
||||
$this,
|
||||
'action' . $actionName
|
||||
), $args);
|
||||
|
||||
} else {
|
||||
throw new Exception(sprintf('Missing action (%s) for controller (%s)', $originalActionName, static::class));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected function missingAction($controllerName, $actionName) {
|
||||
|
||||
return 'index';
|
||||
}
|
||||
|
||||
public function initialize() {
|
||||
Predefined::frontend();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check ACL permissions
|
||||
*
|
||||
* @param $action
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDo($action) {
|
||||
return Acl::canDo($action, $this);
|
||||
}
|
||||
|
||||
public function redirect($url, $statusCode = 302, $terminate = true) {
|
||||
Request::redirect($url, $statusCode, $terminate);
|
||||
}
|
||||
|
||||
public function validatePermission($permission) {
|
||||
|
||||
if (!$this->canDo($permission)) {
|
||||
Notification::error(n2_('You are not authorised to view this resource.'));
|
||||
|
||||
ApplicationSmartSlider3::getInstance()
|
||||
->getApplicationTypeAdmin()
|
||||
->process('sliders', 'index');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validateVariable($condition, $property) {
|
||||
|
||||
if (!$condition) {
|
||||
Notification::error(sprintf(n2_('Missing parameter: %s'), $property));
|
||||
|
||||
ApplicationSmartSlider3::getInstance()
|
||||
->getApplicationTypeAdmin()
|
||||
->process('sliders', 'index');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validateDatabase($condition, $showError = true) {
|
||||
if (!$condition) {
|
||||
if ($showError) {
|
||||
Notification::error(n2_('Database error'));
|
||||
|
||||
ApplicationSmartSlider3::getInstance()
|
||||
->getApplicationTypeAdmin()
|
||||
->process('sliders', 'index');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validateToken() {
|
||||
if (!Form::checkToken()) {
|
||||
Notification::error(n2_('Security token mismatch'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Controller\Admin;
|
||||
|
||||
|
||||
use Nextend\Framework\Asset\Js\Js;
|
||||
use Nextend\Framework\Asset\Predefined;
|
||||
use Nextend\Framework\Controller\AbstractController;
|
||||
|
||||
abstract class AbstractAdminController extends AbstractController {
|
||||
|
||||
public function initialize() {
|
||||
// Prevent browser from cache on backward button.
|
||||
header("Cache-Control: no-store");
|
||||
|
||||
Js::addGlobalInline('window.N2DISABLESCHEDULER=1;');
|
||||
|
||||
parent::initialize();
|
||||
|
||||
Predefined::frontend();
|
||||
Predefined::backend();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Controller\Admin;
|
||||
|
||||
|
||||
use Nextend\Framework\Controller\AjaxController;
|
||||
|
||||
class AdminAjaxController extends AjaxController {
|
||||
|
||||
}
|
@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Nextend\Framework\Controller\Admin;
|
||||
|
||||
use Nextend\Framework\Notification\Notification;
|
||||
use Nextend\Framework\Request\Request;
|
||||
use Nextend\Framework\Visual\ModelVisual;
|
||||
|
||||
abstract class AdminVisualManagerAjaxController extends AdminAjaxController {
|
||||
|
||||
protected $type = '';
|
||||
|
||||
/**
|
||||
* @return ModelVisual
|
||||
*/
|
||||
public abstract function getModel();
|
||||
|
||||
public function actionCreateSet() {
|
||||
$this->validateToken();
|
||||
|
||||
$this->validatePermission('smartslider_edit');
|
||||
|
||||
$name = Request::$REQUEST->getVar('name');
|
||||
$this->validateVariable(!empty($name), 'set name');
|
||||
|
||||
$model = $this->getModel();
|
||||
if (($set = $model->createSet($name))) {
|
||||
$this->response->respond(array(
|
||||
'set' => $set
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Unexpected error'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionRenameSet() {
|
||||
$this->validateToken();
|
||||
|
||||
$this->validatePermission('smartslider_edit');
|
||||
|
||||
$setId = Request::$REQUEST->getInt('setId');
|
||||
$this->validateVariable($setId > 0, 'set');
|
||||
|
||||
$name = Request::$REQUEST->getVar('name');
|
||||
$this->validateVariable(!empty($name), 'set name');
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if (($set = $model->renameSet($setId, $name))) {
|
||||
$this->response->respond(array(
|
||||
'set' => $set
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Set is not editable'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionDeleteSet() {
|
||||
$this->validateToken();
|
||||
|
||||
$this->validatePermission('smartslider_delete');
|
||||
|
||||
$setId = Request::$REQUEST->getInt('setId');
|
||||
$this->validateVariable($setId > 0, 'set');
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if (($set = $model->deleteSet($setId))) {
|
||||
$this->response->respond(array(
|
||||
'set' => $set
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Set is not editable'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionLoadVisualsForSet() {
|
||||
$this->validateToken();
|
||||
|
||||
|
||||
$setId = Request::$REQUEST->getInt('setId');
|
||||
$this->validateVariable($setId > 0, 'set');
|
||||
|
||||
$model = $this->getModel();
|
||||
$visuals = $model->getVisuals($setId);
|
||||
if (is_array($visuals)) {
|
||||
$this->response->respond(array(
|
||||
'visuals' => $visuals
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Unexpected error'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionLoadSetByVisualId() {
|
||||
$this->validateToken();
|
||||
|
||||
$visualId = Request::$REQUEST->getInt('visualId');
|
||||
$this->validateVariable($visualId > 0, 'visual');
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
$set = $model->getSetByVisualId($visualId);
|
||||
|
||||
if (is_array($set) && is_array($set['visuals'])) {
|
||||
$this->response->respond(array(
|
||||
'set' => $set
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Visual do not exists'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionAddVisual() {
|
||||
$this->validateToken();
|
||||
|
||||
$this->validatePermission('smartslider_edit');
|
||||
|
||||
$setId = Request::$REQUEST->getInt('setId');
|
||||
$this->validateVariable($setId > 0, 'set');
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if (($visual = $model->addVisual($setId, Request::$REQUEST->getVar('value')))) {
|
||||
$this->response->respond(array(
|
||||
'visual' => $visual
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Not editable'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionDeleteVisual() {
|
||||
$this->validateToken();
|
||||
|
||||
$this->validatePermission('smartslider_delete');
|
||||
|
||||
$visualId = Request::$REQUEST->getInt('visualId');
|
||||
$this->validateVariable($visualId > 0, 'visual');
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if (($visual = $model->deleteVisual($visualId))) {
|
||||
$this->response->respond(array(
|
||||
'visual' => $visual
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Not editable'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
public function actionChangeVisual() {
|
||||
$this->validateToken();
|
||||
|
||||
$this->validatePermission('smartslider_edit');
|
||||
|
||||
$visualId = Request::$REQUEST->getInt('visualId');
|
||||
$this->validateVariable($visualId > 0, 'visual');
|
||||
|
||||
$model = $this->getModel();
|
||||
|
||||
if (($visual = $model->changeVisual($visualId, Request::$REQUEST->getVar('value')))) {
|
||||
$this->response->respond(array(
|
||||
'visual' => $visual
|
||||
));
|
||||
}
|
||||
|
||||
Notification::error(n2_('Unexpected error'));
|
||||
$this->response->error();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Nextend\Framework\Controller;
|
||||
|
||||
use Nextend\Framework\Form\Form;
|
||||
use Nextend\Framework\Notification\Notification;
|
||||
use Nextend\Framework\PageFlow;
|
||||
use Nextend\Framework\Response\ResponseAjax;
|
||||
|
||||
class AjaxController extends AbstractController {
|
||||
|
||||
/** @var ResponseAjax */
|
||||
protected $response;
|
||||
|
||||
public function __construct($applicationType) {
|
||||
PageFlow::cleanOutputBuffers();
|
||||
|
||||
$this->response = new ResponseAjax($applicationType);
|
||||
parent::__construct($applicationType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseAjax
|
||||
*/
|
||||
public function getResponse() {
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
public function validateToken() {
|
||||
|
||||
if (!Form::checkToken()) {
|
||||
Notification::error(n2_('Security token mismatch. Please refresh the page!'));
|
||||
$this->response->error();
|
||||
}
|
||||
}
|
||||
|
||||
public function validatePermission($permission) {
|
||||
|
||||
if (!$this->canDo($permission)) {
|
||||
|
||||
Notification::error(n2_('You are not authorised to view this resource.'));
|
||||
|
||||
$this->response->error();
|
||||
}
|
||||
}
|
||||
|
||||
public function validateVariable($condition, $property) {
|
||||
|
||||
if (!$condition) {
|
||||
Notification::error(sprintf(n2_('Missing parameter: %s'), $property));
|
||||
$this->response->error();
|
||||
}
|
||||
}
|
||||
|
||||
public function validateDatabase($condition, $showError = true) {
|
||||
if (!$condition) {
|
||||
Notification::error(n2_('Database error'));
|
||||
$this->response->error();
|
||||
}
|
||||
}
|
||||
|
||||
public function redirect($url, $statusCode = 302, $terminate = true) {
|
||||
$this->response->redirect($url);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user