/**
 * Renders a basic HTML <textarea> element.
 *
 * @param array    $element The element to render
 * @param Pieform  $form    The form to render the element for
 * @return string           The HTML for the element
 */
function pieform_element_textarea(Pieform $form, $element)
{
    /*{{{*/
    global $_PIEFORM_TEXTAREAS;
    $rows = $cols = $style = '';
    if (isset($element['height'])) {
        $style .= 'height:' . $element['height'] . ';';
        $rows = intval($element['height'] > 0) ? ceil(intval($element['height']) / 10) : 1;
    } elseif (isset($element['rows'])) {
        $rows = $element['rows'];
    } else {
        Pieform::info('No value for rows or height specified for textarea "' . $element['name'] . '"');
    }
    if (isset($element['width'])) {
        $style .= 'width:' . $element['width'] . ';';
        $cols = intval($element['width'] > 0) ? ceil(intval($element['width']) / 10) : 1;
    } elseif (isset($element['cols'])) {
        $cols = $element['cols'];
    } else {
        Pieform::info('No value for cols or width specified for textarea "' . $element['name'] . '"');
    }
    $element['style'] = isset($element['style']) ? $style . $element['style'] : $style;
    $fullwidth = !empty($element['fullwidth']) ? 'true' : 'false';
    if (!empty($element['resizable'])) {
        $element['class'] = isset($element['class']) && $element['class'] ? $element['class'] . ' resizable' : 'resizable';
        $_PIEFORM_TEXTAREAS[] = array('formname' => $form->get_name(), 'elementname' => $form->get_name() . '_' . $element['id'], 'fullwidth' => $fullwidth);
    }
    return '<textarea' . ($rows ? ' rows="' . $rows . '"' : '') . ($cols ? ' cols="' . $cols . '"' : '') . $form->element_attributes($element, array('maxlength', 'size')) . '>' . Pieform::hsc($form->get_value($element)) . '</textarea>';
}
/**
 * Extension by Mahara. This api function returns the javascript required to
 * set up the element, assuming the element has been placed in the page using
 * javascript. This feature is used in the views interface.
 *
 * In theory, this could go upstream to pieforms itself
 *
 * @param Pieform $form     The form
 * @param array   $element  The element
 */
function pieform_element_tinywysiwyg_views_js(Pieform $form, $element)
{
    if (is_html_editor_enabled()) {
        $formname = json_encode($form->get_name());
        $editor = json_encode($form->get_name() . '_' . $element['name']);
        return "\ntinyMCE.idCounter=0;" . "\ntinyMCE.execCommand('mceAddEditor', false, {$editor});" . "\nPieformManager.connect('onsubmit', {$formname}, tinyMCE.triggerSave);" . "\nPieformManager.connect('onreply', {$formname}, function () {" . "\n  tinyMCE.execCommand('mceRemoveEditor', false, {$editor});" . "});";
    }
    return '';
}
Esempio n. 3
0
/**
 * Extension by Mahara. This api function returns the javascript required to 
 * set up the element, assuming the element has been placed in the page using 
 * javascript. This feature is used in the views interface.
 *
 * In theory, this could go upstream to pieforms itself
 *
 * @param Pieform $form     The form
 * @param array   $element  The element
 */
function pieform_element_tinywysiwyg_views_js(Pieform $form, $element)
{
    global $USER;
    if ($USER->get_account_preference('wysiwyg') || defined('PUBLIC')) {
        $formname = json_encode($form->get_name());
        $editor = json_encode($form->get_name() . '_' . $element['name']);
        return "\ntinyMCE.idCounter=0;tinyMCE.execCommand('mceAddControl', true, {$editor});PieformManager.connect('onsubmit', {$formname}, tinyMCE.triggerSave);";
    }
    return '';
}
Esempio n. 4
0
/**
 * Renders form elements inside a <table>.
 *
 * @param Pieform $form    The form the element is being rendered for
 * @param array   $element The element to be rendered
 * @return string          The element rendered inside an appropriate container
 */
function pieform_renderer_table(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    if ($element['type'] == 'fieldset') {
        // Add table tags to the build element, to preserve HTML compliance
        $builtelement = $element['html'];
        if (0 === strpos($builtelement, "\n<fieldset")) {
            $closelegendpos = strpos($builtelement, '</legend>');
            if ($closelegendpos !== false) {
                $closelegendpos += 9;
                $builtelement = substr($builtelement, 0, $closelegendpos) . '<table><tbody>' . substr($builtelement, $closelegendpos);
            } else {
                $pos = strpos($builtelement, '>') + 1;
                $builtelement = substr($builtelement, 0, $pos) . '<table><tbody>' . substr($builtelement, $pos);
            }
        } else {
            $builtelement = substr($builtelement, 0, 11) . '<table><tbody>' . substr($builtelement, 11);
        }
        $builtelement = substr($builtelement, 0, -12) . '</tbody></table></fieldset>';
        $result = "\t<tr>\n\t\t<td colspan=\"2\">";
        $result .= $builtelement;
        $result .= "</td>\n\t</tr>";
        return $result;
    }
    $result = "\t<tr";
    $result .= ' id="' . $formname . '_' . $element['name'] . '_container"';
    // Set the class of the enclosing <tr> to match that of the element
    if (!empty($element['class'])) {
        $result .= ' class="' . $element['class'] . '"';
    }
    $result .= ">\n\t\t";
    $result .= '<th>';
    if (isset($element['labelhtml'])) {
        $result .= $element['labelhtml'];
    }
    $result .= "</th>\n\t\t<td>";
    $result .= $element['html'];
    if (isset($element['helphtml'])) {
        $result .= ' ' . $element['helphtml'];
    }
    $result .= "</td>\n\t</tr>\n";
    // Description - optional description of the element, or other note that should be visible
    // on the form itself (without the user having to hover over contextual help
    if ((!$form->has_errors() || $form->get_property('showdescriptiononerror')) && !empty($element['description'])) {
        if ($form->get_property('descriptionintwocells')) {
            $result .= "\t<tr>\n\t\t<td></td><td class=\"description\">";
        } else {
            $result .= "\t<tr>\n\t\t<td colspan=\"2\" class=\"description\">";
        }
        $result .= $element['description'];
        $result .= "</td>\n\t</tr>\n";
    }
    if (!empty($element['error'])) {
        $result .= "\t<tr>\n\t\t<td colspan=\"2\" class=\"errmsg\">";
        $result .= $element['error'];
        $result .= "</td>\n\t</tr>\n";
    }
    return $result;
}
Esempio n. 5
0
/**
 * Renders form elements all on one line.
 *
 * @param Pieform $form    The form the element is being rendered for
 * @param array   $element The element that is being rendered
 * @return string          The element rendered inside an appropriate container
 */
function pieform_renderer_oneline(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    // Set the class of the enclosing <div> to match that of the element
    $result = '<span';
    if (isset($element['name'])) {
        $result .= ' id="' . $formname . '_' . $element['name'] . '_container"';
    }
    if (!empty($element['class'])) {
        $result .= ' class="' . $element['class'] . '"';
    }
    $result .= '>';
    if (isset($element['title']) && $element['title'] !== '' && $element['type'] != 'fieldset') {
        if (!empty($element['nolabel'])) {
            // Don't bother with a label for the element
            $result .= Pieform::hsc($element['title']);
        } else {
            $result .= '<label for="' . $element['id'] . '">' . Pieform::hsc($element['title']) . '</label>';
        }
        if ($form->get_property('requiredmarker') && !empty($element['rules']['required'])) {
            $result .= ' <span class="requiredmarker">*</span>';
        }
    }
    $result .= $element['html'];
    $result .= "</span>";
    return $result;
}
Esempio n. 6
0
/**
 * Provides a duration chooser, with a text box for a number and a
 * select box to choose the units, in days, weeks, months, years, or 'no end date'.
 *
 * @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_expiry(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    $result = '';
    $name = $element['name'];
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    // Get the value of the element for rendering.
    if (isset($element['value'])) {
        $seconds = $element['value'];
        $values = pieform_element_expiry_get_expiry_from_seconds($element['value']);
    } else {
        if (isset($global[$element['name']]) && isset($global[$element['name'] . '_units'])) {
            $values = array('number' => $global[$element['name']], 'units' => $global[$element['name'] . '_units']);
            $seconds = $values['number'] * pieform_element_expiry_seconds_in($values['units']);
        } else {
            if (isset($element['defaultvalue'])) {
                $seconds = $element['defaultvalue'];
                $values = pieform_element_expiry_get_expiry_from_seconds($seconds);
            } else {
                $values = array('number' => '', 'units' => 'noenddate');
                $seconds = null;
            }
        }
    }
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
    // Same with the select. And do the events using mochikit signal instead of dom events
    $numberinput = '<input';
    $numberinput .= $values['units'] == 'noenddate' && empty($element['rules']['required']) ? ' disabled="disabled"' : '';
    $numberinput .= ' type="text" size="4" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . $values['number'] . '" tabindex="' . $element['tabindex'] . '"';
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
    $uselect = '<select onchange="' . $name . '_change()" ';
    $uselect .= 'name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . $element['tabindex'] . "\">\n";
    foreach (pieform_element_expire_get_expiry_units() as $u) {
        // Don't allow 'no end date' if the element is required
        if ($u == 'noenddate' && !empty($element['rules']['required'])) {
            continue;
        }
        $uselect .= "\t<option value=\"{$u}\"" . ($values['units'] == $u ? ' selected="selected"' : '') . '>' . $form->i18n('element', 'expiry', $u, $element) . "</option>\n";
    }
    $uselect .= "</select>\n";
    // Make sure the input is disabled if "no end date" is selected
    $script = <<<EOJS
<script type="text/javascript" language="javascript">
function {$name}_change() {
    if (\$('{$formname}_{$name}_units').value == 'noenddate') {
        \$('{$formname}_{$name}').disabled = true;
    }
    else {
        \$('{$formname}_{$name}').disabled = false;
    }
}
</script>
EOJS;
    return $numberinput . $uselect . $script;
}
/**
 * Provides a javascript calendar for inputting a date.
 *
 * General documentation about the calendar is available at
 * http://www.dynarch.com/static/jscalendar-1.0/doc/html/reference.html
 *
 * @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_calendar(Pieform $form, $element)
{
    /*{{{*/
    $id = $form->get_name() . '_' . $element['name'];
    $value = $form->get_value($element);
    if ($value) {
        $value = Pieform::hsc(strftime($element['caloptions']['ifFormat'], $value));
    }
    // Build the HTML
    $result = '<input type="text"' . $form->element_attributes($element) . ' value="' . $value . '">';
    if (isset($element['imagefile'])) {
        $result .= '<a href="" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"' . ' tabindex="' . $element['tabindex'] . '">' . '<img src="' . $element['imagefile'] . '" alt=""></a>';
    } else {
        $result .= '<input type="button" id="' . $id . '_btn" onclick="return false;" class="pieform-calendar-toggle"' . ' value="..." tabindex="' . $element['tabindex'] . '">';
    }
    // Build the configuring javascript
    $options = array_merge($element['caloptions'], array('inputField' => $id, 'button' => $id . '_btn'));
    $encodedoptions = json_encode($options);
    // Some options are callbacks and need their quoting removed
    foreach (array('dateStatusFunc', 'flatCallback', 'onSelect', 'onClose', 'onUpdate') as $function) {
        $encodedoptions = preg_replace('/("' . $function . '"):"([a-zA-Z0-9$]+)"/', '\\1:\\2', $encodedoptions);
    }
    $result .= '<script type="text/javascript">Calendar.setup(' . $encodedoptions . ');</script>';
    return $result;
}
/**
 * Renders a set of radio buttons for a form
 *
 * @param array    $element The element to render
 * @param Pieform  $form    The form to render the element for
 * @return string           The HTML for the element
 */
function pieform_element_radio(Pieform $form, $element)
{
    /*{{{*/
    if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) {
        throw new PieformException('Radio elements should have at least one option');
    }
    $result = '';
    $form_value = $form->get_value($element);
    $id = $element['id'];
    $separator = "\n";
    if (isset($element['separator'])) {
        $separator = $element['separator'] . $separator;
    }
    foreach ($element['options'] as $value => $data) {
        $uid = $id . substr(md5(microtime()), 0, 4);
        $element['id'] = $uid;
        if (is_array($data)) {
            $text = $data['text'];
            $description = isset($data['description']) ? $data['description'] : '';
        } else {
            $text = $data;
            $description = '';
        }
        $result .= '<input type="radio"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($value) . '"' . ($form_value == $value ? ' checked="checked"' : '') . '> <label for="' . $form->get_name() . '_' . $uid . '">' . Pieform::hsc($text) . "</label>" . ($description != '' ? '<div class="radio-description">' . $description . '</div>' : '') . $separator;
    }
    $result = substr($result, 0, -strlen($separator));
    return $result;
}
Esempio n. 9
0
/**
 * Renders form elements all on one line.
 *
 * @param Pieform $form    The form the element is being rendered for
 * @param array   $element The element that is being rendered
 * @return string          The element rendered inside an appropriate container
 */
function pieform_renderer_oneline(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    // Set the class of the enclosing <div> to match that of the element
    $result = '<span';
    if (isset($element['name'])) {
        $result .= ' id="' . $formname . '_' . Pieform::hsc($element['name']) . '_container"';
    }
    if (!empty($element['class'])) {
        $result .= ' class="' . Pieform::hsc($element['class']) . '"';
    }
    $result .= '>';
    if (isset($element['labelhtml'])) {
        $result .= $element['labelhtml'];
    }
    if (isset($element['prehtml'])) {
        $result .= '<span class="prehtml">' . $element['prehtml'] . '</span>';
    }
    $result .= $element['html'];
    if (isset($element['posthtml'])) {
        $result .= '<span class="posthtml">' . $element['posthtml'] . '</span>';
    }
    $result .= "</span>";
    return $result;
}
Esempio n. 10
0
function pieform_element_captcha_get_value(Pieform $form, $element)
{
    global $SESSION;
    $name = $element['name'];
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    return isset($global[$name]) && strtolower($global[$name]) == strtolower($SESSION->get($form->get_name() . '_' . $name));
}
Esempio n. 11
0
/**
 * Provides a tag input field
 *
 * @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_tags(Pieform $form, $element)
{
    $smarty = smarty();
    $value = array();
    if (isset($element['defaultvalue']) && is_array($element['defaultvalue'])) {
        $value = $element['defaultvalue'];
    }
    if ($form->get_value($element)) {
        $value = $form->get_value($element);
    }
    if (isset($element['value']) && is_array($element['value'])) {
        $value = $element['value'];
    }
    if (!is_array($value)) {
        $value = array();
    }
    if (!isset($element['size'])) {
        $element['size'] = 60;
    }
    $smarty->assign('name', $element['name']);
    $smarty->assign('size', $element['size']);
    $smarty->assign('id', $form->get_name() . '_' . $element['id']);
    $smarty->assign('value', join(', ', $value));
    $smarty->left_delimiter = '{{';
    $smarty->right_delimiter = '}}';
    return $smarty->fetch('form/tags.tpl');
}
Esempio n. 12
0
/**
 * Renders form elements all on one line.
 *
 * @param Pieform $form    The form the element is being rendered for
 * @param array   $element The element that is being rendered
 * @return string          The element rendered inside an appropriate container
 */
function pieform_renderer_oneline(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    // Set the class of the enclosing <div> to match that of the element
    $result = '<span';
    if (isset($element['name'])) {
        $result .= ' id="' . $formname . '_' . Pieform::hsc($element['name']) . '_container"';
    }
    if (!empty($element['class'])) {
        // add form-group classes to all real form fields
        if (strpos($element['class'], 'html') === false) {
            // $element['class'] = $element['class'] . ' form-group-inline';
            $element['class'] = 'form-group-inline';
        }
        // add bootstrap has-error class to any error fields
        if (strpos($element['class'], 'error') !== false) {
            $element['class'] = $element['class'] . ' has-error';
        }
        $result .= ' class="' . Pieform::hsc($element['class']) . '"';
    }
    $result .= '>';
    if (isset($element['labelhtml'])) {
        $result .= $element['labelhtml'];
    }
    if (isset($element['prehtml'])) {
        $result .= '<span class="prehtml">' . $element['prehtml'] . '</span>';
    }
    $result .= $element['html'];
    if (isset($element['posthtml'])) {
        $result .= '<span class="posthtml">' . $element['posthtml'] . '</span>';
    }
    $result .= "</span>";
    return $result;
}
Esempio n. 13
0
/**
 * Renders form elements inside <div>s.
 *
 * @param Pieform $form    The form the element is being rendered for
 * @param array   $element The element to be rendered
 * @return string          The element rendered inside an appropriate container
 */
function pieform_renderer_div(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    // Set the class of the enclosing <div> to match that of the element
    $result = '<div';
    if (isset($element['name'])) {
        $result .= ' id="' . $formname . '_' . $element['name'] . '_container"';
    }
    if (!empty($element['class'])) {
        $result .= ' class="' . $element['class'] . '"';
    }
    $result .= '>';
    if (isset($element['labelhtml'])) {
        $result .= $element['labelhtml'];
    }
    //$result .= $builtelement;
    $result .= $element['html'];
    if (isset($element['helphtml'])) {
        $result .= ' ' . $element['helphtml'];
    }
    // Description - optional description of the element, or other note that should be visible
    // on the form itself (without the user having to hover over contextual help
    if ((!$form->has_errors() || $form->get_property('showdescriptiononerror')) && !empty($element['description'])) {
        $result .= '<div class="description"> ' . Pieform::hsc($element['description']) . "</div>";
    }
    if (!empty($element['error'])) {
        $result .= '<div class="errmsg">' . Pieform::hsc($element['error']) . '</div>';
    }
    $result .= "</div>\n";
    return $result;
}
/**
 * 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;
}
Esempio n. 15
0
function pieform_element_files_get_value(Pieform $form, $element)
{
    $name = $form->get_name() . '_' . $element['name'];
    $value = array();
    foreach ($_FILES as $k => $v) {
        if (preg_match('/^' . $name . '_files_\\d+$/', $k) && !empty($v['name'])) {
            $value[] = $k;
        }
    }
    return $value;
}
Esempio n. 16
0
/**
 * Provides a size chooser, with a text box for a number and a
 * select box to choose the units, in bytes, kilobytes, megabytes or gigabytes
 *
 * @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_bytes(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    $result = '';
    $name = Pieform::hsc($element['name']);
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    // Get the value of the element for rendering.
    if (isset($element['value'])) {
        $bytes = $element['value'];
        $values = pieform_element_bytes_get_bytes_from_bytes($element['value']);
    } else {
        if ($form->is_submitted() && isset($global[$element['name']]) && isset($global[$element['name'] . '_units'])) {
            $values = array('number' => $global[$element['name']], 'units' => $global[$element['name'] . '_units']);
            $bytes = $values['number'] * pieform_element_bytes_in($values['units']);
        } else {
            if (isset($element['defaultvalue'])) {
                $bytes = $element['defaultvalue'];
                $values = pieform_element_bytes_get_bytes_from_bytes($bytes);
            } else {
                $values = array('number' => '0', 'units' => 'bytes');
                $bytes = 0;
            }
        }
    }
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
    // Same with the select. And do the events using mochikit signal instead of dom events
    $numberinput = '<div class="with-dropdown js-with-dropdown text">';
    $numberinput .= '<label for="' . $formname . '_' . $name . '">' . Pieform::hsc($element['title']) . ': </label><input';
    $numberinput .= ' type="text" size="6" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . Pieform::hsc($values['number']) . '" tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    $numberinput .= 'class="with-dropdown js-with-dropdown form-control text' . (isset($element['error']) ? ' error"' : '') . '"';
    if (isset($element['description'])) {
        $numberinput .= ' aria-describedby="' . $form->element_descriptors($element) . '"';
    }
    $numberinput .= "></div>\n";
    $uselect = '<div class="dropdown-connect js-dropdown-connect select">';
    $uselect .= '<label for="' . $formname . '_' . $name . '_units" class="accessible-hidden sr-only">' . get_string('units') . '</label>';
    $uselect .= '<span class="picker"><select class="form-control dropdown-connect js-dropdown-connect select" name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . Pieform::hsc($element['tabindex']) . '"';
    if (isset($element['description'])) {
        $uselect .= ' aria-describedby="' . $form->element_descriptors($element) . '"';
    }
    $uselect .= ">\n";
    foreach (pieform_element_bytes_get_bytes_units() as $u) {
        $uselect .= "\t<option value=\"{$u}\"" . ($values['units'] == $u ? ' selected="selected"' : '') . '>' . $form->i18n('element', 'bytes', $u, $element) . "</option>\n";
    }
    $uselect .= "</select></span></div>\n";
    $fieldset = '<div id="' . $formname . '_' . $name . '_fieldset" class="dropdown-group js-dropdown-group form-group">' . '<fieldset class="pieform-fieldset dropdown-group js-dropdown-group">' . $numberinput . $uselect . '</fieldset></div>';
    return $fieldset;
}
Esempio n. 17
0
/**
 * Renders a set of radio buttons for a form
 *
 * @param Pieform $form  The form to render the element for
 * @param array $element The element to render. In addition to the standard Pieform
 *                       element attributes, it can also take the following optional
 *                       attributes:
 *                       - separator: The HTML string that should separate the radio
 *                         buttons (defaults to \n, always has \n appended to it)
 *                       - rowsize: How many radio buttons to print per row (defaults to 1)
 *                       - nolabels: Don't print the labels next to the individual radio buttons.
 * @return string           The HTML for the element
 */
function pieform_element_radio(Pieform $form, $element)
{
    if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) {
        throw new PieformException('Radio elements should have at least one option');
    }
    $result = '';
    $form_value = $form->get_value($element);
    $id = $element['id'];
    $separator = "\n";
    if (isset($element['separator'])) {
        $separator = $element['separator'] . $separator;
    }
    $rowsize = isset($element['rowsize']) ? (int) $element['rowsize'] : 1;
    $nolabels = isset($element['nolabels']) ? $element['nolabels'] : false;
    $classname = '';
    if (!empty($element['hiddenlabels'])) {
        $classname = ' class="accessible-hidden"';
    }
    $titletext = '';
    if (!empty($element['title'])) {
        $titletext = '<span class="accessible-hidden">' . Pieform::hsc($element['title']) . ': </span>';
    }
    $i = 0;
    foreach ($element['options'] as $value => $data) {
        $idsuffix = substr(md5(microtime()), 0, 4);
        $baseid = $element['id'];
        $element['id'] = $uid = $id . $idsuffix;
        if (is_array($data)) {
            $text = $data['text'];
            $description = isset($data['description']) ? $data['description'] : '';
        } else {
            $text = $data;
            $description = '';
        }
        $attributes = $form->element_attributes($element);
        $attributes = preg_replace("/aria-describedby=\"[^\"]*{$baseid}{$idsuffix}_description\\s*[^\"]*\"/", 'aria-describedby="$1_description"', $attributes);
        $result .= '<div class="radio">';
        $result .= '<input type="radio"' . $attributes . ' value="' . Pieform::hsc($value) . '"' . ($form_value == $value ? ' checked="checked"' : '') . '>';
        if (!$nolabels) {
            $result .= ' <label for="' . $form->get_name() . '_' . $uid . '"' . $classname . '>' . $titletext . Pieform::hsc($text) . "</label>" . ($description != '' ? '<div class="description">' . $description . '</div>' : '');
        }
        $result .= '</div>';
        $i++;
        if ($rowsize <= 1 || $i % $rowsize == 0) {
            $result .= $separator;
        }
    }
    // If there was a separator printed on the end, then remove it
    if ($rowsize <= 1 || $i % $rowsize == 0) {
        $result = substr($result, 0, -strlen($separator));
    }
    return $result;
}
Esempio n. 18
0
/**
 * Provides a basic text field input.
 *
 * @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_text(Pieform $form, $element)
{
    /*{{{*/
    $value = Pieform::hsc($form->get_value($element));
    $html = '';
    // If hidewhenempty is set, the text box is hidden by a link which expands it.
    if (!empty($element['hidewhenempty']) && $value == '') {
        $inputid = hsc($form->get_name() . '_' . $element['name']);
        $linktext = $element['expandtext'] ? hsc($element['expandtext']) : get_string('edit');
        $html .= '<a class="btn btn-default" href="" ' . "onclick=\"addElementClass('{$inputid}_expand', 'hidden'); removeElementClass('{$inputid}', 'hidden'); return false;\"" . "id=\"{$inputid}_expand\">" . $linktext . '</a>';
        $element['class'] .= ' hidden';
    }
    return $html . '<input type="text"' . $form->element_attributes($element) . ' value="' . $value . '">';
}
/**
 *
 * @package    mahara
 * @subpackage form-element
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */
function pieform_element_checkboxes(Pieform $form, $element)
{
    /*{{{*/
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    $submitted = $form->is_submitted();
    if ($submitted) {
        $value = isset($global[$element['name']]) ? $global[$element['name']] : array();
    }
    $result = '';
    if (count($element['elements']) > 1) {
        $id = hsc($form->get_name() . '_' . $element['name']) . '_container';
        $result .= '<a href="" onclick="pieform_element_checkboxes_update(\'' . $id . '\', true); return false;">' . get_string('selectall') . '</a>' . '&nbsp;' . ' <a href="" onclick="pieform_element_checkboxes_update(\'' . $id . '\', false); return false;">' . get_string('selectnone') . '</a>';
    }
    $result .= '<div class="cl"></div>';
    $element['name'] .= '[]';
    // Number of characters in checkbox labels (use 0 or false for no limit).
    $labelwidth = isset($element['labelwidth']) ? (int) $element['labelwidth'] : 17;
    $elementtitle = '';
    if (isset($element['title'])) {
        $elementtitle = '<span class="accessible-hidden">' . $element['title'] . ': </span>';
    }
    foreach ($element['elements'] as $e) {
        $id = $form->get_name() . '_' . $element['id'];
        $idsuffix = substr(md5(microtime()), 0, 4);
        if (!$submitted || !empty($e['disabled'])) {
            $checked = $e['defaultvalue'];
        } else {
            $checked = !empty($value[$e['value']]) || in_array($e['value'], $value);
        }
        $attributes = $form->element_attributes($element);
        $attributes = preg_replace("/\\bid=\"{$id}\"/", "id=\"{$id}{$idsuffix}\"", $attributes);
        $title = $labelwidth ? str_shorten_text($e['title'], $labelwidth, true) : $e['title'];
        $result .= '<div class="checkboxes-option"><input type="checkbox" value="' . $e['value'] . '" ' . $attributes . ($checked ? ' checked="checked"' : '') . (!empty($e['disabled']) ? ' disabled' : '') . '>' . ' <label class="checkbox" for="' . $id . $idsuffix . '">' . $elementtitle . Pieform::hsc($title) . '</label></div>';
    }
    $result .= '<div class="cl"></div>';
    return $result;
}
Esempio n. 20
0
function pieform_element_multitext_get_value(Pieform $form, $element)
{
    if (isset($element['value'])) {
        return $element['value'];
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    $name = $form->get_name() . '_' . $element['name'];
    if ($form->is_submitted() && isset($global[$name]) && is_array($global[$name])) {
        return array_values($global[$name]);
    }
    if (isset($element['defaultvalue'])) {
        return $element['defaultvalue'];
    }
    return null;
}
/**
 * Provides a password field that doesn't send the password text back to the user's browser.
 * If the value of the field is empty, then you get a standard input type=password.
 * If the field is non-empty, you get a link that says "Change password?" and if you click
 * on that, you get an input type=password.
 * The "Change password?" text can be customized by providing an "expandtext" value.
 *
 * @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_passwordnoread(Pieform $form, $element)
{
    if (isset($element['defaultvalue']) && $element['defaultvalue'] == '') {
        return '<input type="password"' . $form->element_attributes($element) . ' value="">';
    } else {
        $inputid = hsc($form->get_name() . '_' . $element['name']);
        $linktext = isset($element['expandtext']) ? hsc($element['expandtext']) : get_string('changepassword');
        $html = '<a href="" ' . "onclick=\"" . "addElementClass('{$inputid}_expand', 'hidden'); " . "jQuery('#{$inputid}').attr('name', '{$element['name']}');" . "removeElementClass('{$inputid}', 'hidden'); " . "return false;" . "\" id=\"{$inputid}_expand\">" . $linktext . '</a>';
        $element['class'] .= ' hidden';
        // This password input starts out invisible, and with a placeholder name (so that
        // it won't be processed by the form). When you click the link, it becomes visible
        // and gains its real name.
        // TODO: Non-JS version.
        return $html . '<input type="password" name="' . hsc($element['name'] . '_placeholder') . '" ' . $form->element_attributes($element, array('name')) . ' value="">';
    }
}
Esempio n. 22
0
/**
 * Provides a size chooser, with a text box for a number and a
 * select box to choose the units, in bytes, kilobytes, or megabytes
 *
 * @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_bytes(Pieform $form, $element)
{
    /*{{{*/
    $formname = $form->get_name();
    $result = '';
    $name = $element['name'];
    if (!isset($element['defaultvalue'])) {
        $element['defaultvalue'] = null;
    }
    $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
    // Get the value of the element for rendering.
    if (isset($element['value'])) {
        $bytes = $element['value'];
        $values = pieform_element_bytes_get_bytes_from_bytes($element['value']);
    } else {
        if (isset($global[$element['name']]) && isset($global[$element['name'] . '_units'])) {
            $values = array('number' => $global[$element['name']], 'units' => $global[$element['name'] . '_units']);
            $bytes = $values['number'] * pieform_element_bytes_in($values['units']);
        } else {
            if (isset($element['defaultvalue'])) {
                $bytes = $element['defaultvalue'];
                $values = pieform_element_bytes_get_bytes_from_bytes($bytes);
            } else {
                $values = array('number' => '0', 'units' => 'bytes');
                $bytes = 0;
            }
        }
    }
    // @todo probably create with an actual input element, as tabindex doesn't work here for one thing
    // Same with the select. And do the events using mochikit signal instead of dom events
    $numberinput = '<input';
    $numberinput .= ' type="text" size="8" name="' . $name . '"';
    $numberinput .= ' id="' . $formname . '_' . $name . '" value="' . $values['number'] . '" tabindex="' . $element['tabindex'] . '"';
    $numberinput .= (isset($element['error']) ? ' class="error"' : '') . ">\n";
    $uselect = '<select onchange="' . $name . '_change()" ';
    $uselect .= 'name="' . $name . '_units" id="' . $formname . '_' . $name . '_units"' . ' tabindex="' . $element['tabindex'] . "\">\n";
    foreach (pieform_element_bytes_get_bytes_units() as $u) {
        $uselect .= "\t<option value=\"{$u}\"" . ($values['units'] == $u ? ' selected="selected"' : '') . '>' . $form->i18n('element', 'bytes', $u, $element) . "</option>\n";
    }
    $uselect .= "</select>\n";
    return $numberinput . $uselect;
}
Esempio n. 23
0
/**
 * Renders a textarea, but with extra javascript to turn it into a wysiwyg
 * textarea.
 *
 * @todo support resizable.
 *
 * @param array   $element The element to render
 * @param Pieform $form    The form to render the element for
 * @return string          The HTML for the element
 */
function pieform_element_wysiwyg(Pieform $form, $element)
{
    global $USER, $_PIEFORM_WYSIWYGS;
    $_PIEFORM_WYSIWYGS[] = $form->get_name() . '_' . $element['name'];
    if ($USER->get_account_preference('wysiwyg')) {
        if (!$form->get_property('elementclasses')) {
            $element['class'] = isset($element['class']) && $element['class'] !== '' ? $element['class'] . ' wysiwyg' : 'wysiwyg';
        }
    }
    $rows = $cols = $style = '';
    if (isset($element['height'])) {
        $style .= 'height:' . $element['height'] . ';';
        $rows = intval($element['height'] > 0) ? ceil(intval($element['height']) / 10) : 1;
    } elseif (isset($element['rows'])) {
        $rows = $element['rows'];
    } else {
        log_warn('No value for rows or height specified for textarea ' . $element['name']);
    }
    if (isset($element['width'])) {
        $style .= 'width:' . $element['width'] . ';';
        $cols = intval($element['width'] > 0) ? ceil(intval($element['width']) / 10) : 1;
    } elseif (isset($element['cols'])) {
        $cols = $element['cols'];
    } else {
        log_warn('No value for cols or width specified for textarea ' . $element['name']);
    }
    $element['style'] = isset($element['style']) ? $style . $element['style'] : $style;
    if ($USER->get_account_preference('wysiwyg')) {
        $value = Pieform::hsc($form->get_value($element));
    } else {
        // Replace <br>s as added by wysiwyg editor or nl2br with a newline
        $value = preg_replace("#<br />\\s#", "\n", $form->get_value($element));
        // As placed in the value by the wysiwyg editor
        $value = str_replace('</p><p>', "\n\n", $value);
        // Find the last </p> and replace with newlines
        $value = preg_replace('#</p>\\s#', "\n", $value);
        $value = strip_tags($value);
    }
    return '<textarea' . ($rows ? ' rows="' . $rows . '"' : '') . ($cols ? ' cols="' . $cols . '"' : '') . $form->element_attributes($element, array('maxlength', 'size')) . '>' . $value . '</textarea>';
}
Esempio n. 24
0
/**
 * Provides an email list, with verification to enable addresses
 *
 * @param array    $element The element to render
 * @param Pieform  $form    The form to render the element for
 * @return string           The HTML for the element
 */
function pieform_element_emaillist(Pieform $form, $element)
{
    $smarty = smarty_core();
    $smarty->left_delimiter = '{{';
    $smarty->right_delimiter = '}}';
    $value = $form->get_value($element);
    if (!is_array($value) && isset($element['defaultvalue']) && is_array($element['defaultvalue'])) {
        $value = $element['defaultvalue'];
    }
    if (!isset($value['validated'])) {
        $value['validated'] = array();
    }
    if (!isset($value['unvalidated'])) {
        $value['unvalidated'] = array();
    }
    if (!isset($value['unsent'])) {
        $value['unsent'] = array();
    }
    if (!isset($value['default'])) {
        $value['default'] = '';
    }
    if (is_array($value) && count($value)) {
        $smarty->assign('validated', $value['validated']);
        $smarty->assign('unvalidated', $value['unvalidated']);
        $smarty->assign('unsent', $value['unsent']);
        $smarty->assign('default', $value['default']);
    }
    $smarty->assign('form', $form->get_name());
    $smarty->assign('name', $element['name']);
    $smarty->assign('title', $element['title']);
    $smarty->assign('addbuttonstr', get_string('addbutton', 'artefact.internal'));
    $smarty->assign('validationemailstr', json_encode(get_string('validationemailwillbesent', 'artefact.internal')));
    $smarty->assign('disabled', !empty($element['disabled']));
    if (isset($element['description'])) {
        $smarty->assign('describedby', $form->element_descriptors($element));
    }
    return $smarty->fetch('form/emaillist.tpl');
}
Esempio n. 25
0
/**
 * Provides a javascript color selector for inputting a hex color value.
 *
 * General documentation about the JavaScript Color Picker (Chooser) is available at
 * http://jscolor.com/
 *
 * @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_color(Pieform $form, $element)
{
    $result = '';
    $name = Pieform::hsc($element['name']);
    $baseid = Pieform::hsc($form->get_name() . '_' . $element['name']);
    $value = Pieform::hsc($element['defaultvalue']);
    $transparent = !empty($element['options']['transparent']) && $element['options']['transparent'] == true;
    // Transparency optional control
    if ($transparent) {
        $optional = <<<EOF
        <script type="application/javascript">
            var {$baseid}_oldval = '';
            function {$baseid}_toggle(x) {
                if ( x.checked ) {
                    {$baseid}_oldval = \$('{$baseid}').value;
                    \$('{$baseid}').value   = '';
                    \$('{$baseid}').disabled   = true;
                }
                else {
                    \$('{$baseid}').value   = {$baseid}_oldval;
                    \$('{$baseid}').disabled   = false;
                }
            }
        </script>
EOF;
        $title = '';
        if (!empty($element['title'])) {
            $title = '<span class="accessible-hidden sr-only">' . $element['title'] . ':</span>';
        }
        $optional .= ' <input type="checkbox" ' . (isset($element['defaultvalue']) && $element['defaultvalue'] == 'transparent' ? 'checked="checked" ' : '') . 'name="' . $name . '_optional" id="' . $baseid . '_optional" onchange="' . $baseid . '_toggle(this)" ' . 'tabindex="' . Pieform::hsc($element['tabindex']) . '">';
        $optional .= ' <label for="' . $baseid . '_optional">' . $title . $form->i18n('element', 'color', 'transparent', $element) . '</label> ';
        $result .= $optional;
    }
    // Color Picker (Chooser)
    $result .= '<input type="text" name="' . $name . '_color" id="' . $baseid . '"' . ($transparent && (!isset($element['defaultvalue']) || $element['defaultvalue'] == 'transparent') ? ' disabled="disabled"' : '') . ($transparent ? ' class="color {hash:true,required:false}"' : ' class="color {hash:true}"') . ' value="' . ($value == 'transparent' ? '' : $value) . '">';
    return $result;
}
function pieform_element_rolepermissions_get_value(Pieform $form, $element)
{
    /*{{{*/
    if (isset($element['value'])) {
        return $element['value'];
    }
    if (isset($element['defaultvalue'])) {
        $value = $element['defaultvalue'];
    } else {
        $value = group_get_default_artefact_permissions($element['group']);
    }
    if ($form->is_submitted()) {
        $global = $form->get_property('method') == 'get' ? $_GET : $_POST;
        $prefix = $form->get_name() . '_' . $element['name'] . '_p';
        foreach ($value as $r => $perms) {
            foreach (array_keys(get_object_vars($perms)) as $p) {
                if ($r != 'admin') {
                    $value[$r]->{$p} = param_boolean($prefix . '_' . $r . '_' . $p);
                }
            }
        }
    }
    return $value;
}
Esempio n. 27
0
function pieform_element_filebrowser_views_js(Pieform $form, $element)
{
    global $_PIEFORM_FILEBROWSERS;
    $formname = $form->get_name();
    $prefix = $formname . '_' . $element['name'];
    return $_PIEFORM_FILEBROWSERS[$prefix]['views_js'] . " {$prefix}.init();";
}
Esempio n. 28
0
function auth_generate_registration_form_js($aform, $registerconfirm)
{
    // The javascript needs to refer to field names, but they are obfuscated in this form,
    // so construct and build the form in separate steps, so we can get the field names.
    $form = new Pieform($aform);
    $institutionid = $form->get_name() . '_' . $form->hashedfields['institution'];
    $reasonid = $form->get_name() . '_' . $form->hashedfields['reason'];
    $formhtml = $form->build();
    if (count($registerconfirm) == 1) {
        $js = '
        $j(function() {
            $j("#' . $reasonid . '_container").removeClass("js-hidden");
            $j("#' . $reasonid . '_container textarea").removeClass("js-hidden");
            $j("#' . $reasonid . '_container").next("tr.textarea").removeClass("js-hidden");
        });
       ';
    } else {
        $url = get_config('wwwroot') . 'json/termsandconditions.php';
        $js = '
        var registerconfirm = ' . json_encode($registerconfirm) . ';
        $j(function() {
            $j("#' . $institutionid . '").change(function() {
                if (this.value && registerconfirm[this.value] == 1) {
                    $j("#' . $reasonid . '_container").removeClass("js-hidden");
                    $j("#' . $reasonid . '_container textarea").removeClass("js-hidden");
                    $j("#' . $reasonid . '_container").next("tr.textarea").removeClass("js-hidden");
                }
                else {
                    $j("#' . $reasonid . '_container").addClass("js-hidden");
                    $j("#' . $reasonid . '_container textarea").addClass("js-hidden");
                    $j("#' . $reasonid . '_container").next("tr.textarea").addClass("js-hidden");
                }
                // need to fetch the correct terms and conditions for the institution
                if (this.value) {
                    $j.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "' . $url . '",
                        data: {
                            "institution": this.value,
                        }
                    }).done(function (data) {
                        if (data.content) {
                            $j("#termscontainer").html(data.content);
                        }
                    });
                }
            });
        });
        ';
    }
    return array($formhtml, $js);
}
Esempio n. 29
0
/**
 * Check if the form is supposed to be collapsed
 * @param array      $element The element to render
 * @return boolean   if the fieldset should be collapsed
 */
function pieform_is_collapsed(Pieform $form, $element)
{
    $formname = $form->get_name();
    $iscollapsed = !empty($element['collapsed']);
    // if name element is not set, element should not be collapsed
    if (!isset($element['name'])) {
        return false;
    }
    $valid = param_alphanumext('fs', null) !== $element['name'];
    // Work out whether any of the children have errors on them
    foreach ($element['elements'] as $subelement) {
        if (isset($subelement['error'])) {
            return false;
            // collapsible element should be open
        }
    }
    if (isset($element['name'])) {
        $openparam = $formname . '_' . $element['name'] . '_open';
    }
    if ($iscollapsed && $valid && !param_boolean($openparam, false)) {
        return true;
    }
    return false;
}
Esempio n. 30
0
function pieform_get_help(Pieform $form, $element)
{
    return get_help_icon($form->get_property('plugintype'), $form->get_property('pluginname'), $form->get_name(), $element['name']);
}