Ejemplo n.º 1
0
/**
 * Renders a container. Containers contain other elements, and do not count as a
 * "true" element, in that they do not have a value and cannot be validated.
 *
 * Similar to a fieldset, except with no wrapper, apart from the div produced by
 * the pieform
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_container(Pieform $form, $element)
{
    /*{{{*/
    $result = "";
    foreach ($element['elements'] as $subname => $subelement) {
        $result .= "\t" . pieform_render_element($form, $subelement);
    }
    return $result;
}
Ejemplo n.º 2
0
/**
 * Renders a fieldset. Fieldsets contain other elements, and do not count as a
 * "true" element, in that they do not have a value and cannot be validated.
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_fieldset(Pieform $form, $element)
{
    /*{{{*/
    $result = "\n<fieldset";
    if (!empty($element['collapsible']) || !empty($element['class'])) {
        if (!isset($element['legend']) || $element['legend'] === '') {
            Pieform::info('Collapsible fieldsets should have a legend so they can be toggled');
        }
        $classes = array('collapsible');
        // Work out whether any of the children have errors on them
        $error = false;
        foreach ($element['elements'] as $subelement) {
            if (isset($subelement['error'])) {
                $error = true;
                break;
            }
        }
        if (!empty($element['collapsed']) && !$error) {
            $classes[] = 'collapsed';
        }
        if (!empty($element['class'])) {
            $classes[] = $element['class'];
        }
        $result .= ' class="' . implode(' ', $classes) . '"';
    }
    $result .= ">\n";
    if (isset($element['legend'])) {
        $result .= '<legend';
        if (!empty($element['collapsible'])) {
            $id = substr(md5(microtime()), 0, 4);
            $result .= ' id="' . $id . '">';
            $result .= '<script type="text/javascript">';
            $result .= "var a = A({'href':'', 'tabindex':{$form->get_property('tabindex')}}, " . json_encode($element['legend']) . "); ";
            $result .= "connect(a, 'onclick', function(e) { toggleElementClass('collapsed', \$('{$id}').parentNode); e.stop(); });";
            $result .= "replaceChildNodes('{$id}', a);</script>";
        } else {
            $result .= '>' . Pieform::hsc($element['legend']);
        }
        $result .= "</legend>\n";
    }
    foreach ($element['elements'] as $subname => $subelement) {
        if ($subelement['type'] == 'hidden') {
            throw new PieformError("You cannot put hidden elements in fieldsets");
        }
        $result .= "\t" . pieform_render_element($form, $subelement);
    }
    $result .= "</fieldset>\n";
    return $result;
}
Ejemplo n.º 3
0
function _render_elements_as_multicolumn($form, $element)
{
    // we want to render the elements as div within each table cell
    // so we record the old renderer and switch to div and switch back afterwards
    $oldrenderer = $form->get_property('renderer');
    $form->set_property('renderer', 'div');
    $form->include_plugin('renderer', 'div');
    // We have a list of which elements are going to be the column headings
    $columns = $element['columns'];
    $footer = $element['footer'];
    $count = 0;
    $result = '';
    // If we want a description above the table we can add it as 'comment' to the fieldset element
    if (!empty($element['comment'])) {
        $result .= '<p';
        if (isset($element['class'])) {
            $result .= ' class="' . Pieform::hsc($element['class']) . '"';
        }
        $result .= '>' . Pieform::hsc($element['comment']);
        $result .= "</p>\n";
    }
    $result .= '<table class="fullwidth table">';
    // Now we loop through the elements chuncking them into rows based on the columns count
    // but we include the labelhtml as the first column to describe what the row is about.
    if (count($element['columns']) > 0) {
        $result .= '<thead>';
        foreach ($columns as $name) {
            $data = $element['elements'][$name];
            if (empty($count)) {
                $result .= "\t<tr";
                // Set the class of the enclosing <tr> to match that of the element
                if (isset($data['class'])) {
                    $result .= ' class="' . Pieform::hsc($data['class']) . '"';
                }
                $result .= ">\n\t\t";
            }
            if (empty($count)) {
                $result .= "<th></th>\n\t";
            }
            $result .= '<th>';
            $result .= Pieform::hsc($data['value']);
            if ($form->get_property('requiredmarker') && !empty($data['rules']['required'])) {
                $result .= ' <span class="requiredmarker">*</span>';
            }
            $result .= "</th>\n\t";
            $count++;
        }
        $result .= '</thead>';
        $count = 0;
    }
    $result .= '<tbody>';
    foreach ($element['elements'] as $name => $data) {
        if (empty($count)) {
            $result .= "\t<tr>";
        }
        // ignore heading columns - we've already rendered them, and footer
        if (array_search($name, $columns) === false && array_search($name, $footer) === false) {
            if (empty($count)) {
                $result .= '<th>';
                if (isset($data['labelhtml'])) {
                    $result .= $data['labelhtml'];
                }
                $result .= "</th>\n\t";
            }
            unset($data['labelhtml']);
            $result .= "\t<td";
            if (isset($data['name'])) {
                $result .= " id=\"" . $form->get_name() . '_' . Pieform::hsc($data['name']) . '_container"';
            }
            $result .= '>';
            $result .= pieform_render_element($form, $data);
            // Contextual help
            if (isset($data['helphtml'])) {
                $result .= ' ' . $data['helphtml'];
            }
            $result .= "</td>\n\t";
        }
        $count++;
        if ($count == count($columns)) {
            $result .= "</tr>\n";
            $count = 0;
        }
    }
    $result .= '</tbody></table>';
    if (count($element['footer']) > 0) {
        foreach ($footer as $name) {
            $data = $element['elements'][$name];
            $result .= pieform_render_element($form, $data);
        }
    }
    $form->set_property('renderer', $oldrenderer);
    return $result;
}
Ejemplo n.º 4
0
 /**
  * Builds and returns the HTML for the form, using the chosen renderer or
  * template
  *
  * Note that the "action" attribute for the form tag is NOT HTML escaped
  * for you. This allows you to build your own URLs, should you require. On
  * the other hand, this means you must be careful about escaping the URL,
  * especially if it has data from an external source in it.
  *
  * @param boolean Whether to include the <form...></form> tags in the output
  * @return string The form as HTML
  */
 public function build($outputformtags = true)
 {
     /*{{{*/
     $result = '';
     // Builds the HTML each element (see the build_element_html method for
     // more information)
     foreach ($this->data['elements'] as &$element) {
         if ($element['type'] == 'fieldset' || $element['type'] == 'container') {
             foreach ($element['elements'] as &$subelement) {
                 $this->build_element_html($subelement);
             }
             unset($subelement);
         } else {
             $this->build_element_html($element);
         }
     }
     unset($element);
     // If a template is to be used, use it instead of a renderer
     if (!empty($this->data['template'])) {
         $form_tag = $this->get_form_tag();
         // $elements is a convenience variable that contains all of the form elements (minus fieldsets and
         // hidden elements)
         $elements = array();
         foreach ($this->elementrefs as $element) {
             if ($element['type'] != 'hidden') {
                 $elements[$element['name']] = $element;
             }
         }
         // Hidden elements
         $this->include_plugin('element', 'hidden');
         $hidden_elements = '';
         foreach ($this->elementrefs as $element) {
             if ($element['type'] == 'hidden') {
                 $hidden_elements .= pieform_element_hidden($this, $element);
             }
         }
         $element = array('type' => 'hidden', 'name' => 'pieform_' . $this->get_name(), 'value' => '');
         $hidden_elements .= pieform_element_hidden($this, $element);
         ob_start();
         if ($this->get_property('ignoretemplatenotices')) {
             $old_level = error_reporting(E_ALL & ~E_NOTICE);
         }
         $templatepath = $this->get_property('templatedir');
         $templatepath = $templatepath && substr($templatepath, -1) != '/' ? $templatepath . '/' : $templatepath;
         $templatepath .= $this->get_property('template');
         require $templatepath;
         if ($this->get_property('ignoretemplatenotices')) {
             error_reporting($old_level);
         }
         $result = ob_get_contents();
         ob_end_clean();
     } else {
         // No template being used - instead use a renderer
         if ($outputformtags) {
             $result = $this->get_form_tag() . "\n";
         }
         $this->include_plugin('renderer', $this->data['renderer']);
         // Form header
         $function = 'pieform_renderer_' . $this->data['renderer'] . '_header';
         if (function_exists($function)) {
             $result .= $function();
         }
         // Render each element
         foreach ($this->data['elements'] as $name => $element) {
             if ($element['type'] != 'hidden') {
                 $result .= pieform_render_element($this, $element);
             }
         }
         // Form footer
         $function = 'pieform_renderer_' . $this->data['renderer'] . '_footer';
         if (function_exists($function)) {
             $result .= $function();
         }
         // Hidden elements
         $this->include_plugin('element', 'hidden');
         foreach ($this->elementrefs as $element) {
             if ($element['type'] == 'hidden') {
                 $result .= pieform_element_hidden($this, $element);
             }
         }
         $element = array('type' => 'hidden', 'name' => 'pieform_' . $this->name, 'value' => '');
         $result .= pieform_element_hidden($this, $element);
         if ($outputformtags) {
             $result .= "</form>\n";
         }
     }
     // Output the javascript to wire things up, but only if it is needed.
     // The two cases where it is needed is when:
     // 1) The form is a JS form that hasn't been submitted yet. When the
     // form has been submitted the javascript from the first page load is
     // still active in the document
     // 2) The form is NOT a JS form, but has a presubmitcallback
     if ($outputformtags && ($this->data['jsform'] && !$this->submitted || !$this->data['jsform'] && $this->data['presubmitcallback'])) {
         // Establish which buttons in the form are submit buttons. This is
         // used to detect which button was pressed to cause the form
         // submission
         $submitbuttons = array();
         foreach ($this->elementrefs as $element) {
             if (!empty($element['submitelement'])) {
                 // TODO: might have to deal with cancel elements here too
                 $submitbuttons[] = $element['name'];
             }
         }
         $data = json_encode(array('name' => $this->name, 'jsForm' => $this->data['jsform'], 'submitButtons' => $submitbuttons, 'preSubmitCallback' => $this->data['presubmitcallback'], 'jsSuccessCallback' => $this->data['jssuccesscallback'], 'jsErrorCallback' => $this->data['jserrorcallback'], 'globalJsErrorCallback' => $this->data['globaljserrorcallback'], 'postSubmitCallback' => $this->data['postsubmitcallback'], 'newIframeOnSubmit' => $this->data['newiframeonsubmit'], 'checkDirtyChange' => $this->data['checkdirtychange']));
         $result .= "<script type=\"text/javascript\">new Pieform({$data});</script>\n";
     }
     return $result;
 }
Ejemplo n.º 5
0
                    <h4 class="modal-title blockinstance-header  text-inline general-account-options-title" id="general-account-options-label"><?php 
echo get_string('accountoptionsdesc', 'account');
?>
</h4>
                  </div>

                   <div class="modal-body">
                    <?php 
// Render account preferences with a renderer (inside this template :D)
$accountprefs = (object) expected_account_preferences();
$accountprefs = array_keys(general_account_prefs_form_elements($accountprefs));
$fieldset_elements = array();
foreach ($accountprefs as $p) {
    $fieldset_elements[] = $elements[$p];
}
$accountoptions_fieldset = array('name' => 'generalaccountoptions', 'type' => 'fieldset', 'class' => 'last', 'elements' => $fieldset_elements);
$this->include_plugin('renderer', $this->data['renderer']);
$this->include_plugin('element', 'fieldset');
$this->build_element_html($accountoptions_fieldset);
echo pieform_render_element($this, $accountoptions_fieldset);
echo $hidden_elements;
?>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
            </div>
        </div>
    </div>
</form>
Ejemplo n.º 6
0
/**
 * Renders a fieldset. Fieldsets contain other elements, and do not count as a
 * "true" element, in that they do not have a value and cannot be validated.
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_fieldset(Pieform $form, $element)
{
    /*{{{*/
    global $_PIEFORM_FIELDSETS;
    $result = "\n<fieldset";
    $classes = array('pieform-fieldset');
    if (!empty($element['class'])) {
        $classes[] = Pieform::hsc($element['class']);
    }
    if (!empty($element['collapsible'])) {
        if (!isset($element['legend']) || $element['legend'] === '') {
            Pieform::info('Collapsible fieldsets should have a legend so they can be toggled');
        }
        $classes[] = 'collapsible';
        $formname = $form->get_name();
        if (!isset($_PIEFORM_FIELDSETS['forms'][$formname])) {
            $_PIEFORM_FIELDSETS['forms'][$formname] = array('formname' => $formname);
        }
        if (isset($element['name'])) {
            $openparam = $formname . '_' . $element['name'] . '_open';
        }
        // Work out whether any of the children have errors on them
        $error = false;
        foreach ($element['elements'] as $subelement) {
            if (isset($subelement['error'])) {
                $error = true;
                break;
            }
        }
        if (!empty($element['collapsed']) && !$error && (!isset($element['name']) || param_alphanumext('fs', null) != $element['name'] && !param_boolean($openparam, false))) {
            $classes[] = 'collapsed';
        }
    }
    $result .= ' class="' . implode(' ', $classes) . '"';
    $result .= ">\n";
    if (isset($element['legend'])) {
        $result .= '<legend><h4>';
        if (!empty($element['collapsible'])) {
            $result .= '<a href="">' . Pieform::hsc($element['legend']) . '</a>';
            if (isset($openparam)) {
                $result .= '<input type="hidden" name="' . hsc($openparam) . '" class="open-fieldset-input" ' . 'value="' . intval(!in_array('collapsed', $classes)) . '">';
            }
        } else {
            $result .= Pieform::hsc($element['legend']);
        }
        // Help icon
        if (!empty($element['help'])) {
            $function = $form->get_property('helpcallback');
            if (function_exists($function)) {
                $result .= $function($form, $element);
            } else {
                $result .= '<span class="help"><a href="" title="' . Pieform::hsc($element['help']) . '" onclick="return false;">?</a></span>';
            }
        }
        $result .= "</h4></legend>\n";
    }
    foreach ($element['elements'] as $subname => $subelement) {
        if ($subelement['type'] == 'hidden') {
            throw new PieformException("You cannot put hidden elements in fieldsets");
        }
        $result .= "\t" . pieform_render_element($form, $subelement);
    }
    $result .= "</fieldset>\n";
    return $result;
}
Ejemplo n.º 7
0
/**
 * Renders a fieldset. Fieldsets contain other elements, and do not count as a
 * "true" element, in that they do not have a value and cannot be validated.
 *
 * @param Pieform $form    The form to render the element for
 * @param array   $element The element to render
 * @return string          The HTML for the element
 */
function pieform_element_fieldset(Pieform $form, $element)
{
    /*{{{*/
    global $_PIEFORM_FIELDSETS;
    $result = "\n<fieldset";
    if (!empty($element['collapsible']) || !empty($element['class'])) {
        if (!isset($element['legend']) || $element['legend'] === '') {
            Pieform::info('Collapsible fieldsets should have a legend so they can be toggled');
        }
        $classes = array('collapsible');
        $formname = $form->get_name();
        if (!isset($_PIEFORM_FIELDSETS['forms'][$formname])) {
            $_PIEFORM_FIELDSETS['forms'][$formname] = array('formname' => $formname);
        }
        // Work out whether any of the children have errors on them
        $error = false;
        foreach ($element['elements'] as $subelement) {
            if (isset($subelement['error'])) {
                $error = true;
                break;
            }
        }
        if (!empty($element['collapsed']) && !$error) {
            $classes[] = 'collapsed';
        }
        if (!empty($element['class'])) {
            $classes[] = Pieform::hsc($element['class']);
        }
        $result .= ' class="' . implode(' ', $classes) . '"';
    }
    $result .= ">\n";
    if (isset($element['legend'])) {
        $result .= '<legend>';
        if (!empty($element['collapsible'])) {
            $result .= '<a href="">' . Pieform::hsc($element['legend']) . '</a>';
        } else {
            $result .= Pieform::hsc($element['legend']);
        }
        // Help icon
        if (!empty($element['help'])) {
            $function = $form->get_property('helpcallback');
            if (function_exists($function)) {
                $result .= $function($form, $element);
            } else {
                $result .= '<span class="help"><a href="" title="' . Pieform::hsc($element['help']) . '" onclick="return false;">?</a></span>';
            }
        }
        $result .= "</legend>\n";
    }
    foreach ($element['elements'] as $subname => $subelement) {
        if ($subelement['type'] == 'hidden') {
            throw new PieformError("You cannot put hidden elements in fieldsets");
        }
        $result .= "\t" . pieform_render_element($form, $subelement);
    }
    $result .= "</fieldset>\n";
    return $result;
}