Files
grzanieplus.pl/plugins/stAdminGeneratorPlugin/lib/stAdminGeneratorActions.class.php
2025-03-12 17:06:23 +01:00

408 lines
12 KiB
PHP

<?php
/**
* @property stAdminGeneratorUserConfiguration $adminGeneratorUserConfiguration
*/
class stAdminGeneratorActions extends stActions
{
/**
* Instancja klasy do obsługi "okruszków chleba"
*
* @var stNavigationBreadcrumbs
*/
protected $breadcrumbs = null;
/**
* Konfiguracja użytkownika
*
* @var stAdminGeneratorUserConfiguration
*/
protected $adminGeneratorUserConfiguration;
/**
* Lista uploaderow plikow
*
* @var stAdminFileUpload[]
*/
protected $fileUploader = array();
/**
*
* @param sfContext $context
* @return bool
*/
public function initialize($context)
{
$ret = parent::initialize($context);
$this->adminGeneratorUserConfiguration = new stAdminGeneratorUserConfiguration($context);
$this->admin_configuration = $this->adminGeneratorUserConfiguration;
$context->getI18N()->setFallbackCatalogue('stAdminGeneratorPlugin');
return $ret;
}
public function __destruct()
{
if ($this->adminGeneratorUserConfiguration)
{
$this->adminGeneratorUserConfiguration->save();
}
}
public function postExecute()
{
$this->admin_user_configuration = $this->adminGeneratorUserConfiguration;
return parent::postExecute();
}
public function checkSubscriptionTypes(string $applicatioName, array $types, array $options = [])
{
$options = stConfigHelper::mergeOptions($options, [
'block_actions' => [],
'install_version' => null,
]);
if (!empty($options['install_version']) && !stSoteshop::checkInstallVersion($options['install_version']))
{
return;
}
$valid = stCommunication::hasSubscriptionTypes($types);
$expiredApps = stConfig::getInstance('stUpdate')->get('expired_apps', []);
if (isset($expiredApps[$applicatioName]))
{
$valid = $expiredApps[$applicatioName] > time();
$messageParams = stUpdateNotification::generateMessageParams($applicatioName, $types, [
'expire_date' => $expiredApps[$applicatioName],
]);
$message = stUpdateNotification::expiredApplicationAlertMessageCallback($messageParams, $this->getContext(), true);
$this->setFlash('warning', $message, false);
}
elseif (!$valid)
{
$message = stCommunication::getSubscriptionTypeMessage($types);
$this->setFlash('warning', $message, false);
}
if (!$valid && ($this->getRequest()->getMethod() == sfRequest::POST || in_array($this->getContext()->getActionName(), $options['block_actions'])))
{
$referer = $this->getRequest()->getReferer();
return $this->redirect(!empty($referer) ? $referer : '@'.sfRouting::getInstance()->getCurrentRouteName());
}
}
public function executeSetConfiguration()
{
$action = $this->getRequestParameter('for_action');
$module = $this->getRequestParameter('for_module', $this->getModuleName());
$params = $this->getRequestParameter('params');
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
foreach ($params as $name)
{
$value = $this->getRequest()->getParameter($name, array());
$this->adminGeneratorUserConfiguration->setParameter($name, $value, $module, $action);
}
}
else
{
foreach ($params as $name => $value)
{
$this->adminGeneratorUserConfiguration->setParameter($name, $value, $module, $action);
}
}
$this->adminGeneratorUserConfiguration->save();
if ($this->getRequest()->isXmlHttpRequest())
{
return sfView::HEADER_ONLY;
}
return $this->redirect($this->getRequest()->getReferer());
}
public function executeSetFilter()
{
$action = $this->getRequestParameter('for_action');
$filter = $this->getRequestParameter('id');
$adminGeneratorFilter = AdminGeneratorFilterPeer::retrieveByPk($filter);
$this->clearFilter($action);
if ($adminGeneratorFilter)
{
$this->setFilter($adminGeneratorFilter, $action);
}
return $this->redirect($this->getRequest()->getReferer());
}
public function executeSaveFilter()
{
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
$action = $this->getRequestParameter('for_action');
$filters = $this->getRequestParameter('filters', array());
$filter_profile = $this->getRequestParameter('filter_profile');
$user = $this->getUser()->getGuardUser();
$namespace = stAdminGenerator::getModuleNamespace($this->getModuleName(), $action, 'filter');
$adminGeneratorFilter = AdminGeneratorFilterPeer::retrieveByPK($this->getRequestParameter('id'));
if (!$adminGeneratorFilter)
{
$adminGeneratorFilter = AdminGeneratorFilterPeer::create($user, $filter_profile['name'], $namespace);
}
$adminGeneratorFilter->setIsGlobal(isset($filter_profile['is_global']));
$adminGeneratorFilter->getAdminGeneratorFilterData()->setData($filters);
$adminGeneratorFilter->save();
$this->setFilter($adminGeneratorFilter, $action);
}
return $this->redirect($this->getRequest()->getReferer());
}
public function executeRemoveFilter()
{
$action = $this->getRequestParameter('for_action');
$filter = $this->getRequestParameter('id');
$adminGeneratorFilter = AdminGeneratorFilterPeer::retrieveByPk($filter);
if ($adminGeneratorFilter)
{
$adminGeneratorFilter->delete();
}
$this->clearFilter($action);
return $this->redirect($this->getRequest()->getReferer());
}
public function setConfigurationParameter($name, $value, $params = array())
{
if (!isset($params['action']))
{
$params['action'] = $this->getActionName();
}
if (!isset($params['module']))
{
$params['module'] = $this->getModuleName();
}
$this->adminGeneratorUserConfiguration->setParameter($name, $value, $params['module'], $params['action']);
}
public function getConfigurationParameter($name, $default = null, $params = array())
{
if (!isset($params['action']))
{
$params['action'] = $this->getActionName();
}
if (!isset($params['module']))
{
$params['module'] = $this->getModuleName();
}
return $this->adminGeneratorUserConfiguration->getParameter($name, $default, $params['module'], $params['action']);
}
/**
* Usuwa pole z listy/edycji/konfiguracji
*
* @param string $name
* @return void
*/
public function removeField($name)
{
$this->getUser()->setParameter($name, true, 'soteshop/stAdminGenerator/removed');
}
/**
* Ukrywa pole w widoku listy/edycji/konfiguracji
* @param string $name
* @return void
*/
public function hideField($name)
{
$this->getUser()->setParameter($name, true, 'soteshop/stAdminGenerator/hidden');
}
/**
* Pokazuje pole w widoku listy/edycji/konfiguracji
* @param mixed $name
* @return void
*/
public function showField($name)
{
$this->getUser()->setParameter($name, false, 'soteshop/stAdminGenerator/hidden');
}
/**
* Sprawdza czy pole jest ukryte w widoku listy/edycji/konfiguracji
* @param string $name
* @return bool
*/
public function isFieldHidden($name)
{
return $this->getUser()->getParameter($name, false, 'soteshop/stAdminGenerator/hidden') || $this->getUser()->getParameter($name, false, 'soteshop/stAdminGenerator/removed');
}
/**
* Zwraca instancje klasy do obsługi "okruszków chleba"
*
* @return stNavigationBreadcrumbs
*/
protected function getBreadcrumbs()
{
if (null === $this->breadcrumbs)
{
$i18n = $this->getContext()->getI18N();
$this->breadcrumbs = stNavigationBreadcrumbs::getInstance()->add($i18n->__('Pulpit'), '@homepage');
}
return $this->breadcrumbs;
}
protected function loadViewFields(array $fields)
{
$view_fields = $this->getConfigurationParameter('view_fields', null);
if (null !== $view_fields)
{
foreach (array_keys($fields) as $name)
{
if (!isset($view_fields[$name]))
{
$this->hideField($name);
}
}
}
else
{
foreach ($fields as $name => $params)
{
if (is_array($params) && $params['hidden'])
{
$this->hideField($name);
}
}
}
}
protected function clearFilter($action)
{
$namespace = stAdminGenerator::getModuleNamespace($this->getModuleName(), $action, 'filters');
$this->getUser()->getAttributeHolder()->removeNamespace($namespace);
$this->setConfigurationParameter('filter', null, array('action' => $action));
}
protected function setFilter(AdminGeneratorFilter $filter, $action)
{
$namespace = stAdminGenerator::getModuleNamespace($this->getModuleName(), $action, 'filters');
$this->getUser()->getAttributeHolder()->add($filter->getAdminGeneratorFilterData()->getData(), $namespace);
$this->setConfigurationParameter('filter', $filter->getId(), array('action' => $action));
}
protected function renderView($viewName = "Success")
{
$this->getController()->setRenderMode(sfView::RENDER_VAR);
$view = $this->getController()->getView($this->getModuleName(), $this->getActionName(), 'sfPHP');
$view->initialize($this->getContext(), $this->getModuleName(), $this->getActionName(), $viewName);
return $view->render();
}
protected function hideAction($name)
{
$this->getUser()->setParameter($name, true, 'soteshop/stAdminGenerator/actions/hidden');
}
protected function processForwardParameters(array $parameters, $moduleName, $actionName)
{
$forwardParameters = array();
$namespace = "sf_admin/{$moduleName}/{$actionName}forward_parameters";
foreach ($parameters as $parameter)
{
$forwardParameters[$parameter] = $this->getRequestParameter($parameter);
}
if (!empty($forwardParameters))
{
$this->getUser()->getAttributeHolder()->removeNamespace($namespace);
$this->getUser()->getAttributeHolder()->add($forwardParameters, $namespace);
}
$forwardParameters = $this->getUser()->getAttributeHolder()->getAll($namespace);
foreach ($parameters as $parameter)
{
if (!isset($forwardParameters[$parameter]))
{
$forwardParameters[$parameter] = null;
}
}
return $forwardParameters;
}
/**
* Tworzy nową instancję uploader'a plików
*
* @param string $fieldName
* @param mixed $currentFieldValue
* @param array $options
* @return \stAdminFileUpload
*/
protected function createFileUpoader($fieldName, $currentFieldValue = null, array $options = array()): \stAdminFileUpload
{
$fileUpload = new stAdminFileUpload($fieldName, $currentFieldValue, $options);
$this->fileUploader[$fieldName] = $fileUpload;
return $fileUpload;
}
/**
* Zwraca listę upoaderów
*
* @return \stAdminFileUpload[]
*/
protected function getFileUploaders(): array
{
return $this->fileUploader;
}
/**
* Przekierowuje żądanie z dodatkowymi parametrami
*
* @param string|array $url
* @return void
* @throws sfStopException
*/
protected function redirectWithForwardParameters($url)
{
$url = stAdminGeneratorHelper::applyParametersToUrl($url, $this->forward_parameters);
return $this->redirect($url);
}
}