View source
<?php
interface IBotchaFormController {
public function getForm($form_id, $create = TRUE);
public function getForms($reset = FALSE);
public function save($form);
public function delete($form);
}
class BotchaFormController extends Controller implements IBotchaFormController {
protected $app_name = 'Botcha';
protected $controller_type = Botcha::CONTROLLER_TYPE_FORM;
protected function getModel() {
return parent::getModel();
}
public function getForm($form_id, $create = TRUE) {
$none = TRUE;
$form = $this
->getModel()
->getForm($form_id);
if ($form || $create) {
$none = FALSE;
}
if ($none) {
$botcha_form = new BotchaFormNone($form_id);
}
else {
$botcha_form = new BotchaForm($form_id);
$botcha_form
->getRecipebook();
}
return $botcha_form;
}
public function getForms($reset = FALSE) {
$forms = array();
foreach ($this
->getModel()
->getForms() as $form) {
$forms[$form->id] = $this
->getForm($form->id, FALSE);
}
return $forms;
}
public function save($form) {
$this
->getModel()
->save($form);
return $this
->getForm($form->id, FALSE);
}
public function delete($form) {
$this
->getModel()
->delete($form);
}
}
class BotchaForm {
protected $recipebook;
public function __construct($form_id) {
$this->id = $form_id;
$this->form_id = $form_id;
}
public function setRecipebook($rbid) {
$this->recipebook = $rbid;
return $this;
}
function unsetRecipebook() {
unset($this->recipebook);
return $this;
}
public function getRecipebook() {
if (!isset($this->recipebook)) {
$rbs = BotchaModel::getRecipebooksForms(array(
'mode' => 'recipebook',
'forms' => $this->id,
));
$this->recipebook = !empty($rbs) ? current($rbs) : 'none';
}
return $this->recipebook;
}
public function isEnabled() {
$form_id = $this->id;
$isEnabled = variable_get("botcha_enabled_{$form_id}", 0);
return $isEnabled;
}
public function setEnabled($enabled) {
$form_id = $this->id;
$enabled = (bool) $enabled;
$enabled = (int) $enabled;
variable_set("botcha_enabled_{$form_id}", $enabled);
return $this;
}
public function addAdminLinks(&$form) {
$form_id = $form['form_id']['#value'];
if (variable_get('botcha_administration_mode', FALSE) && user_access('administer BOTCHA settings') && (arg(0) != 'admin' || variable_get('botcha_allow_on_admin_pages', FALSE) || $form_id == 'user_register') && $form_id != 'update_script_selection_form') {
$botcha_element = $this
->createAdminLinksFieldset($form_id);
$botcha_placement = _botcha_get_botcha_placement($form_id, $form);
_botcha_insert_botcha_element($form, $botcha_placement, $botcha_element);
}
}
protected function createAdminLinksFieldset($form_id) {
$botcha_element = array(
'#type' => 'fieldset',
'#title' => t('BOTCHA'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array(
'class' => array(
'botcha-admin-links',
),
),
);
$rbid = $this
->getRecipebook();
if ($rbid === 'none') {
$botcha_element['#title'] = t('BOTCHA: no protection enabled');
$botcha_element['add_botcha'] = array(
'#markup' => l(t('Add BOTCHA protection on form'), Botcha::ADMIN_PATH . "/form/add", array(
'query' => array_merge(drupal_get_destination(), array(
'botcha_form_id' => $form_id,
)),
'html' => TRUE,
)),
);
}
else {
$botcha_element['#title'] = t('BOTCHA: protection enabled (@recipebook recipe book)', array(
'@recipebook' => $rbid,
));
$botcha_element['#description'] = t('Untrusted users will have form %form_id protected by BOTCHA (!recipebook_settings, !general_settings).', array(
'%form_id' => $form_id,
'!recipebook_settings' => l(t('Recipe book settings'), Botcha::ADMIN_PATH . "/recipebook/{$rbid}"),
'!general_settings' => l(t('General BOTCHA settings'), Botcha::ADMIN_PATH),
));
$botcha_element['protection'] = array(
'#type' => 'item',
'#title' => t('Enabled protection'),
'#markup' => t('Form is protected by "@recipebook" recipe book (!edit, !disable)', array(
'@recipebook' => $rbid,
'!edit' => l(t('edit'), Botcha::ADMIN_PATH . "/form/{$form_id}", array(
'query' => drupal_get_destination(),
'html' => TRUE,
)),
'!disable' => l(t('disable'), Botcha::ADMIN_PATH . "/form/{$form_id}/disable", array(
'query' => drupal_get_destination(),
'html' => TRUE,
)),
)),
);
}
return $botcha_element;
}
}
class BotchaFormNone extends BotchaForm {
public function __construct($form_id = NULL) {
$this->id = !empty($form_id) ? $form_id : 'none';
}
public function getRecipebook() {
return 'none';
}
public function addAdminLinks(&$form) {
$form_id = $form['form_id']['#value'];
if (!in_array($form_id, array(
'update_script_selection_form',
'user_login',
'user_login_block',
))) {
parent::addAdminLinks($form);
}
}
}