Framework  3.9
CompositeAutoForm Class Reference

CompositeAutoForm is a container for situations where multiple AutoForms need to be managed on the same page within a single outer form. More...

+ Inheritance diagram for CompositeAutoForm:

Public Member Functions

 CompositeAutoForm ($method="POST", $action="", $containerClass="")
 Creates a new CompositeAutoForm. More...
 
 addForm ($form, $enableValidation=false)
 Add a sub-form to this form. More...
 
 writeScript ()
 Write script for all the sub-forms. More...
 
 preset ()
 Array of fields to be ignored when determining if a record is empty in cases where empty records should not trigger an error. More...
 
 drawForm ()
 Draw the sub-forms, with a combined single set of submission buttons. More...
 
 drawReadOnly ()
 Draw the read-only sub-forms. More...
 
 button ($text, $url, $confirm=null, $isScript=false)
 Adds a custom button to the form. More...
 
 drawButtons ()
 Draws any additional buttons specified in the calling script. More...
 
 save ()
 

Public Attributes

 $forms
 
 $buttons = array()
 
 $method
 
 $action
 
 $containerClass
 
 $buttonAlignment = "left"
 
 $preset = array()
 
 $readOnlyForm
 Specifies whether the entire form is read-only. More...
 
 $onSaveComplete = null
 Callback event handler that is fired after all the subordinate AutoForms have finished saving data to the database. More...
 

Detailed Description

CompositeAutoForm is a container for situations where multiple AutoForms need to be managed on the same page within a single outer form.

To create a composite form, create an instance of CompositeAutoForm and add to it all the subforms (instances of SubordinateAutoForm) to appear on that page. Some subforms may need special handling and so you will need to create a child class of SubordinateAutoForm.

Example: $compositeForm = new CompositeAutoForm("POST", "", "my_form_id");

$compositeForm = new CompositeAutoForm("POST", "", "parts_order_address_form"); $shippingPanel = new OrderAddressForm($compositeForm, "shipping", "Shipping Address", $shippingAddress, "ship_address_id", $orderShip); $billingPanel = new OrderAddressForm($compositeForm, "billing", "Billing Address", $billingAddress, "bill_address_id", $order);

Where OrderAddressForm is a child of class SubordinateAutoForm.

Author
andy

Definition at line 29 of file composite_auto_form.inc.

Member Function Documentation

◆ addForm()

CompositeAutoForm::addForm (   $form,
  $enableValidation = false 
)

Add a sub-form to this form.

Parameters
AutoForm$formthe form to be added

Definition at line 67 of file composite_auto_form.inc.

68  {
69  $form->makeSubordinate();
70  $this->forms[] = $form;
71  $form->getValidationEngine()->generateScript = $enableValidation;
72  }

◆ button()

CompositeAutoForm::button (   $text,
  $url,
  $confirm = null,
  $isScript = false 
)

Adds a custom button to the form.

Parameters
string$textthe button label text
string$urlthe URL to handle the button press
string$confirmoptional confirmation message
boolean$isScripttrue if the url is javascript code to execute, false if it is a URL to redirect to

Definition at line 209 of file composite_auto_form.inc.

210  {
211  $this->buttons[] = array('text' => $text, 'url' => $url, 'confirm' => $confirm, 'isScript' => $isScript);
212  }

◆ CompositeAutoForm()

CompositeAutoForm::CompositeAutoForm (   $method = "POST",
  $action = "",
  $containerClass = "" 
)

Creates a new CompositeAutoForm.

Parameters
string$methodthe HTTP method to use
string$actionthe URL to submit to
string$containerClassCSS class to use for the container for each sub-form

Definition at line 48 of file composite_auto_form.inc.

49  {
50  global $auto_form_defaults;
51 
52  foreach($auto_form_defaults as $field => $value)
53  {
54  $this->$field = $value;
55  }
56 
57  $this->forms = array();
58  $this->method = $method;
59  $this->action = $action;
60  $this->containerClass = $containerClass;
61  }

◆ drawButtons()

CompositeAutoForm::drawButtons ( )

Draws any additional buttons specified in the calling script.

Definition at line 218 of file composite_auto_form.inc.

219  {
220 
221  $submitLabel = $this->submitLabel;
222  $obj = $this->forms[0]->getData();
223 
224  if ($submitLabel == "")
225  {
226  $submitLabel = "Save ".pluralize($obj->prettifyClassName());
227  }
228 
229  echo "<div style='clear: both; text-align: {$this->buttonAlignment}'><br/>";
230  if ($this->useLinkSubmit)
231  {
232  echo "<a class='{$this->buttonCSS}' name='submit' onclick='if (onSubmit{$this->id}(document.forms.{$this->id})) return document.forms.{$this->id}.submit(); else return false;'>$submitLabel</a>";
233  }
234  else
235  {
236  echo "<input type='submit' class='{$this->buttonCSS}' value='$submitLabel'/>";
237  }
238 
239  foreach($this->buttons as $button)
240  {
241  $url = ($button['isScript']) ? $button['url'] : "go('{$button['url']}');";
242 
243  if ($button['confirm'])
244  {
245  $link = "if (confirm('".jsSafe($button['confirm'])."')) $url; return false;";
246  }
247  else
248  {
249  $link = "$url; return false;";
250  }
251 
252  echo "&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' class='{$this->buttonCSS}' onclick=\"$link\" value=\"{$button['text']}\"/>";
253  }
254 
255  echo "</div>";
256  }

◆ drawForm()

CompositeAutoForm::drawForm ( )

Draw the sub-forms, with a combined single set of submission buttons.

Reimplemented in SpreadsheetForm.

Definition at line 137 of file composite_auto_form.inc.

138  {
139  if ($this->readOnlyForm) return $this->drawReadOnly();
140 
141  echo "<form id='composite_auto_form' method='{$this->method}' action='{$this->action}' enctype='multipart/form-data'";
142  echo " onsubmit='return onCompositeFormSubmit(this);'";
143  echo ">\n";
144 
145  if ($this->buttons_at_top)
146  {
147  $this->drawButtons();
148  }
149 
150  foreach($this->forms as $form)
151  {
152  ob_start();
153  $form->drawForm();
154  $output = ob_get_contents();
155  ob_end_clean();
156 
157  $output = preg_replace("/\\bname=(['\"])([^'\"]*?)['\"]/", "name=$1{$form->id}__$2$1", $output);
158 
159 ?>
160  <div id="<?echo $form->id?>_container" class="<?echo $this->containerClass?>">
161 <?
162  echo $output;
163 ?>
164  </div>
165 <?
166  }
167 
168  $this->drawButtons();
169 
170  echo "</form>\n";
171  }
drawButtons()
Draws any additional buttons specified in the calling script.
drawReadOnly()
Draw the read-only sub-forms.

◆ drawReadOnly()

CompositeAutoForm::drawReadOnly ( )

Draw the read-only sub-forms.

Definition at line 176 of file composite_auto_form.inc.

177  {
178  foreach($this->forms as $form)
179  {
180  ob_start();
181  $form->drawReadOnly();
182  $output = ob_get_contents();
183  ob_end_clean();
184 
185  $output = preg_replace("/\\bname=(['\"])([^'\"]*?)['\"]/", "name=$1{$form->id}__$2$1", $output);
186 
187 ?>
188  <div id="<?echo $form->id?>_container" class="<?echo $this->containerClass?>">
189 <?
190  echo $output;
191 ?>
192 
193  </div>
194 <?
195  }
196 
197  echo "<div style='clear:both'>&nbsp;</div><br/>";
198  }

◆ preset()

CompositeAutoForm::preset ( )

Array of fields to be ignored when determining if a record is empty in cases where empty records should not trigger an error.

For example: A spreadsheet form has the fields "sort_order" preset. The user does not complete any values in a row. If the subordinate form has allowEmpty set to true, we don't want the value in the sort_order field to trigger an error message.

Definition at line 126 of file composite_auto_form.inc.

127  {
128  foreach(func_get_args() as $preset)
129  {
130  $this->presetFields[$preset] = true;
131  }
132  }

◆ save()

CompositeAutoForm::save ( )

Reimplemented in SpreadsheetForm.

Definition at line 259 of file composite_auto_form.inc.

260  {
261  global $method;
262 
263  // Do magic
264 
265  $valid = true;
266 
267  foreach($this->forms as $form)
268  {
269  foreach($_POST as $name => $value)
270  {
271  if (strpos($name, "__") === false)
272  {
273  unset($_POST[$name]);
274  }
275  }
276 
277  $prefix = $form->id."__";
278  $len = strlen($prefix);
279 
280  foreach($_POST as $name => $value)
281  {
282  if (!strncmp($name, $prefix, $len))
283  {
284  $_POST[substr($name, $len)] = $value;
285  }
286  }
287 
288  foreach($_FILES as $name => $value)
289  {
290  if (strpos($name, "__") === false)
291  {
292  unset($_FILES[$name]);
293  }
294  }
295 
296  $prefix = $form->id."__";
297  $len = strlen($prefix);
298 
299  foreach($_FILES as $name => $value)
300  {
301  if (!strncmp($name, $prefix, $len))
302  {
303  $_FILES[substr($name, $len)] = $value;
304  }
305  }
306 
307  if (!$form->save())
308  {
309  $valid = false;
310  }
311  }
312 
313  if ($valid)
314  {
315  // onComplete event is fired once all processing has been completed
316 
317  if ($this->onSaveComplete)
318  {
319  call_user_func_array($this->onSaveComplete, array($this));
320  }
321  }
322 
323  return $valid;
324  }

◆ writeScript()

CompositeAutoForm::writeScript ( )

Write script for all the sub-forms.

Reimplemented in SpreadsheetForm.

Definition at line 77 of file composite_auto_form.inc.

78  {
79  $script = "";
80  foreach($this->forms as $form)
81  {
82  $s = $form->writeScript();
83  if ($form->getValidationEngine()->generateScript)
84  {
85  // Remap form control ids
86  $s= preg_replace("/\\bform\\[(['\"])([^'\"]*?)['\"]\\]/", "form[$1{$form->id}__$2$1]", $s);
87  }
88  $script .= $s;
89  }
90 
91  ob_start();
92 ?>
93 <script type="text/javascript">
94 function onCompositeFormSubmit(form)
95 {
96 <?
97  foreach($this->forms as $form)
98  {
99 ?>
100  if (!onSubmit<?echo $form->id?>(form)) return false;
101 <?
102  }
103  ?>
104  return true;
105 }
106 </script>
107 <?
108  $script .= ob_get_contents();
109  ob_end_clean();
110 
111  return $script;
112  }

Member Data Documentation

◆ $action

CompositeAutoForm::$action

Definition at line 34 of file composite_auto_form.inc.

◆ $buttonAlignment

CompositeAutoForm::$buttonAlignment = "left"

Definition at line 36 of file composite_auto_form.inc.

◆ $buttons

CompositeAutoForm::$buttons = array()

Definition at line 32 of file composite_auto_form.inc.

◆ $containerClass

CompositeAutoForm::$containerClass

Definition at line 35 of file composite_auto_form.inc.

◆ $forms

CompositeAutoForm::$forms

Definition at line 31 of file composite_auto_form.inc.

◆ $method

CompositeAutoForm::$method

Definition at line 33 of file composite_auto_form.inc.

◆ $onSaveComplete

CompositeAutoForm::$onSaveComplete = null

Callback event handler that is fired after all the subordinate AutoForms have finished saving data to the database.

Definition at line 39 of file composite_auto_form.inc.

◆ $preset

CompositeAutoForm::$preset = array()

Definition at line 37 of file composite_auto_form.inc.

◆ $readOnlyForm

CompositeAutoForm::$readOnlyForm

Specifies whether the entire form is read-only.

Definition at line 38 of file composite_auto_form.inc.


The documentation for this class was generated from the following file: