Elgg  Version 3.0
FormsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 
15 class FormsService {
16 
17  use Loggable;
18 
22  private $views;
23 
27  private $rendering;
28 
32  private $footer = '';
33 
40  public function __construct(ViewsService $views, LoggerInterface $logger) {
41  $this->views = $views;
42  $this->logger = $logger;
43  }
44 
80  public function render($action, $form_vars = [], $body_vars = []) {
81 
82  $defaults = [
83  'action' => elgg_generate_action_url($action, [], false),
84  'method' => 'post',
85  'ajax' => false,
86  ];
87 
88  // append elgg-form class to any class options set
89  $form_vars['class'] = (array) elgg_extract('class', $form_vars, []);
90  $form_vars['class'][] = 'elgg-form-' . preg_replace('/[^a-z0-9]/i', '-', $action);
91 
92  $form_vars = array_merge($defaults, $form_vars);
93 
94  if (!isset($form_vars['enctype']) && strtolower($form_vars['method']) == 'post') {
95  $form_vars['enctype'] = 'multipart/form-data';
96  }
97 
98  if (elgg_extract('ajax', $form_vars)) {
99  $form_vars['class'][] = 'elgg-js-ajax-form';
100  unset($form_vars['ajax']);
101  }
102 
103  $form_vars['action_name'] = $action;
104 
105  $form_vars['ignore_empty_body'] = (bool) elgg_extract('ignore_empty_body', $form_vars, false);
106 
107  if (!isset($form_vars['body'])) {
108  $this->rendering = true;
109  $this->footer = '';
110 
111  // Render form body
112  $body = $this->views->renderView("forms/$action", $body_vars);
113 
114  if (!empty($body)) {
115  // Grab the footer if one was set during form rendering
116  $body .= $this->views->renderView('elements/forms/footer', [
117  'footer' => $this->getFooter(),
118  'action_name' => $action,
119  'body_vars' => $body_vars,
120  'form_vars' => $form_vars,
121  ]);
122  }
123 
124  $this->rendering = false;
125 
126  $form_vars['body'] = $body;
127  }
128 
129  return elgg_view('input/form', $form_vars);
130  }
131 
141  public function setFooter($footer = '') {
142 
143  if (!$this->rendering) {
144  $this->logger->error('Form footer can only be set and retrieved during form rendering, '
145  . 'anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view hooks)');
146  return false;
147  }
148 
149  $this->footer = $footer;
150  return true;
151  }
152 
157  public function getFooter() {
158  if (!$this->rendering) {
159  $this->logger->error('Form footer can only be set and retrieved during form rendering, '
160  . 'anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view hooks)');
161  return false;
162  }
163 
164  return $this->footer;
165  }
166 
167 }
$action
Definition: full.php:111
$footer
Wrap form footer.
Definition: footer.php:11
elgg_generate_action_url($action, array $query=[], $add_csrf_tokens=true)
Generate an action URL.
$defaults
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
WARNING: API IN FLUX.
if(!elgg_view_exists("plugins/{$plugin_id}/settings")) $form_vars
Configuration exception.
__construct(ViewsService $views, LoggerInterface $logger)
Constructor.
WARNING: API IN FLUX.
setFooter($footer= '')
Sets form footer and defers its rendering until the form view and extensions have been rendered...
$body
Definition: useradd.php:60
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
getFooter()
Returns currently set footer, or false if not in the form rendering stack.
if(!($comment instanceof ElggComment)||!$comment->canEdit()) $body_vars
elgg_view($view, $vars=[], $viewtype= '')
Return a parsed view.
Definition: views.php:246
render($action, $form_vars=[], $body_vars=[])
$views
Definition: item.php:17