File: platform/plugins/Q/handlers/Q/panel/tool.php
<?php
/**
* @module Q-tools
*/
/**
* This tool generates a panel with a <form> tag inside it
* @class Q panel
* @constructor
* @param array $options
* An associative array of parameters, containing:
* "uri" => the uri or url the form should post to
* "method" => the method used to submit form
* "title" => the title of the panel
* "complete" => boolean, indicating whether the data on the server is in a complete state
* "editing" => boolean, indicating whether to show the form in the "editing" state
* "form" => string containing the contents of the form portion of the panel
* which is normally generated by a "Q/form" tool
* "static" => string containing the contents of the "static" portion
* "collapsed" => defaults to false. Whether the panel is shown as collapsed into just the header
* "toggle" => defaults to false. The events that cause toggling of collapsed state.
* If the string is 'click' then toggles the panel on clicks.
* If the string is 'move' then toggles the panel on mouseenter/mouseleave.
* "edit_button" => optional, to override the edit button
* "save_button" => optional, to override the save button
* "cancel_button" => optional, to override the cancel button
* "panel_classes" => optional, additional classes for the panel
* "snf" => optional. The name of the nonce field in the session
* "onSuccess" => optional. The URI to redirect to on success
* "onErrors" => optional. The URI to display if errors occur
* "inProcess" => optional. Causes the panel to appear as if it's a step in a process.
*/
function Q_panel_tool($options)
{
foreach (array('title', 'static', 'form') as $f) {
if (!array_key_exists($f, $options)) {
throw new Q_Exception_RequiredField(array('field' => '$'.$f));
}
}
$edit_button = "<button type='submit' class='basic16 basic16_edit Q_panel_tool_edit'>edit</button>";
$save_button = "<button type='submit' class='basic16 basic16_check Q_panel_tool_save'>save</button>";
$cancel_button = "<button type='reset' class='basic16 basic16_cancel Q_panel_tool_cancel'>cancel</button>";
$panel_classes = '';
$uri = null;
$method = 'post';
$collapsed = false;
$toggle = false;
$inProcess = false;
$onSuccess = null;
$onErrors = null;
$snf = null;
$static = null;
$form = null;
$editing = false;
$complete = false;
$setSlots = null;
extract($options, EXTR_OVERWRITE);
$more_class = ($options['complete'])
? 'Q_panel_tool_complete'
: 'Q_panel_tool_incomplete';
$panel_classes = "$more_class $panel_classes";
$title_div = "<div class='Q_panel_tool_title'>$title</div>";
if ($uri) {
$header = "<div class='Q_panel_tool_buttons'>$save_button$cancel_button$edit_button</div>$title_div";
} else {
$header = $title_div;
}
// Whether to display the panel one way or the other
if ($inProcess) {
$header = $title_div;
if (is_array($form)) {
$form['fields']['_Q_buttons'] = array(
'type' => 'buttons',
'label' => '',
'options' => array(
'continue' => 'Continue'
),
'attributes' => array('class' => 'basic32 basic32_right', 'type' => 'submit')
);
} else {
$form .= "<div class='Q_panel_tool_formbuttons'><button type='submit' class='Q_panel_tool_continue basic32 basic32_right' value='continue'>Continue</button></div>";
}
}
if (is_array($static)) {
foreach ($static['fields'] as $k => $f) {
if (Q::ifset($static, 'fields', $k, 'type', null)) {
switch ($static['fields'][$k]['type']) {
case 'textarea':
$static['fields'][$k]['value'] = str_replace("\n", "<br>", $static['fields'][$k]['value']);
break;
case 'date':
if (!isset($static['fields'][$k]['options']['date'])) {
$static['fields'][$k]['options']['date'] = "M j, Y";
}
break;
case 'buttons':
unset($static['fields'][$k]);
}
}
$static['fields'][$k]['type'] = 'static';
}
$static = Q::tool('Q/form', $static, array('id' => 'static'));
}
// Turn the form into a form
if (is_array($form)) {
$form['slotsToRequest'] = array('form', 'static');
$form['contentElements'] = array(
'form' => '.Q_panel_tool_form',
'static' => '.Q_panel_tool_static'
);
$form = Q::tool('Q/form', $form);
}
// Build the panel
$panel = "<div class='Q_panel_tool_header'>$header</div>"
. "<div class='Q_panel_tool_form'>$form</div>";
if (isset($snf) or isset($onSuccess) or isset($onErrors)) {
$panel .= "<div>".Q_Html::formInfo($onSuccess, $onErrors, $snf)."</div>";
}
$panel .= "<div class='Q_panel_tool_static'>$static</div>";
if ($uri) {
$panel = Q_Html::form(
$uri,
$method,
array('class' => "Q_panel_tool_panel"),
$panel
);
}
if ($editing) $panel_classes .= ' Q_editing';
if ($complete) $panel_classes .= ' Q_complete';
if ($collapsed) $panel_classes .= ' Q_collapsed';
if ($toggle === 'click') $panel_classes .= ' Q_panel_tool_toggle_onclick';
if ($toggle === 'move') $panel_classes .= ' Q_panel_tool_toggle_move';
Q_Response::addScript('{{Q}}/js/tools/panel.js', 'Q');
Q_Response::addStylesheet('{{Q}}/css/panel.css', 'Q');
if (isset($setSlots)) {
Q_Response::setSlot('form', $form);
Q_Response::setSlot('static', $static);
}
return "<div class='Q_panel_tool_container $panel_classes'>$panel</div>";
}