Open Monograph Press  3.3.0
CustomBlockForm.inc.php
1 <?php
2 
17 import('lib.pkp.classes.form.Form');
18 
19 class CustomBlockForm extends Form {
21  var $contextId;
22 
24  var $plugin;
25 
32  function __construct($template, $contextId, $plugin = null) {
33  parent::__construct($template);
34 
35  $this->contextId = $contextId;
36  $this->plugin = $plugin;
37 
38  // Add form checks
39  $this->addCheck(new FormValidatorPost($this));
40  $this->addCheck(new FormValidatorCSRF($this));
41  $this->addCheck(new FormValidator($this, 'blockName', 'required', 'plugins.generic.customBlock.nameRequired'));
42  $this->addCheck(new FormValidatorRegExp($this, 'blockName', 'required', 'plugins.generic.customBlock.nameRegEx', '/^[a-zA-Z0-9_-]+$/'));
43  }
44 
48  function initData() {
51 
52  $templateMgr = TemplateManager::getManager();
53 
54  $blockName = null;
55  $blockContent = null;
56  if ($plugin) {
57  $blockName = $plugin->getName();
58  $blockContent = $plugin->getSetting($contextId, 'blockContent');
59  }
60  $this->setData('blockContent', $blockContent);
61  $this->setData('blockName', $blockName);
62  }
63 
67  function readInputData() {
68  $this->readUserVars(array('blockName', 'blockContent'));
69  }
70 
74  function execute(...$functionArgs) {
77  if (!$plugin) {
78  // Create a new custom block plugin
79  import('plugins.generic.customBlockManager.CustomBlockPlugin');
80  $customBlockManagerPlugin = PluginRegistry::getPlugin('generic', CUSTOMBLOCKMANAGER_PLUGIN_NAME);
81  $plugin = new CustomBlockPlugin($this->getData('blockName'), $customBlockManagerPlugin);
82  // Default the block to being enabled
83  $plugin->setEnabled(true);
84 
85  // Add the custom block to the list of the custom block plugins in the
86  // custom block manager plugin
87  $blocks = $customBlockManagerPlugin->getSetting($contextId, 'blocks');
88  if (!isset($blocks)) $blocks = array();
89 
90  array_push($blocks, $this->getData('blockName'));
91  $customBlockManagerPlugin->updateSetting($contextId, 'blocks', $blocks);
92  }
93 
94  // update custom block plugin content
95  $plugin->updateSetting($contextId, 'blockContent', $this->getData('blockContent'));
96 
97  parent::execute(...$functionArgs);
98  }
99 }
100 
CustomBlockForm\$plugin
$plugin
Definition: CustomBlockForm.inc.php:30
Form\setData
setData($key, $value=null)
Definition: Form.inc.php:229
Form\readUserVars
readUserVars($vars)
Definition: Form.inc.php:378
Form\getData
getData($key)
Definition: Form.inc.php:220
FormValidatorPost
Form validation check to make sure the form is POSTed.
Definition: FormValidatorPost.inc.php:18
CustomBlockForm\readInputData
readInputData()
Definition: CustomBlockForm.inc.php:73
CustomBlockForm\execute
execute(... $functionArgs)
Definition: CustomBlockForm.inc.php:80
CustomBlockForm\$contextId
$contextId
Definition: CustomBlockForm.inc.php:24
CustomBlockPlugin
Definition: CustomBlockPlugin.inc.php:19
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
FormValidator
Class to represent a form validation check.
Definition: FormValidator.inc.php:23
FormValidatorRegExp
Form validation check using a regular expression.
Definition: FormValidatorRegExp.inc.php:18
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
CustomBlockForm\initData
initData()
Definition: CustomBlockForm.inc.php:54
Form\addCheck
addCheck($formValidator)
Definition: Form.inc.php:395
FormValidatorCSRF
Form validation check to make sure the CSRF token is correct.
Definition: FormValidatorCSRF.inc.php:18
CustomBlockForm
Definition: CustomBlockForm.inc.php:19
Form
Class defining basic operations for handling HTML forms.
Definition: Form.inc.php:47
CustomBlockForm\__construct
__construct($template, $contextId, $plugin=null)
Definition: CustomBlockForm.inc.php:38