/** * Renders a hidden element. * * @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_hidden(Pieform $form, $element) { /*{{{*/ if (!array_key_exists('value', $element)) { throw new PieformException('The hidden element "' . $element['name'] . '" must have a value set'); } if (!empty($element['sesskey']) && $form->get_property('method') != 'post') { throw new PieformException('Sesskey values should be POSTed'); } $value = $form->get_value($element); if (is_array($value)) { $result = ''; foreach ($value as $k => $v) { if (is_array($v)) { foreach ($v as $subk => $subv) { $result .= '<input type="hidden" name="' . Pieform::hsc($element['name']) . '[' . Pieform::hsc($k) . '][' . Pieform::hsc($subk) . ']" value="' . Pieform::hsc($subv) . "\">\n"; } } else { $result .= '<input type="hidden" name="' . Pieform::hsc($element['name']) . '[' . Pieform::hsc($k) . ']" value="' . Pieform::hsc($v) . "\">\n"; } } return $result; } return '<input type="hidden"' . $form->element_attributes($element, array('accesskey', 'onclick', 'size', 'style', 'tabindex')) . ' value="' . Pieform::hsc($form->get_value($element)) . "\">\n"; }
/** * Renders a set (red, yellow, green) of radio buttons with corresponding background colors as in traffic lights * * @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_trafficlights(Pieform $form, $element) { /*{{{*/ $global = $form->get_property('method') == 'get' ? $_GET : $_POST; $submitted = $form->is_submitted(); if ($submitted && isset($global[$element['name']])) { $value = $global[$element['name']]; } $result = ''; if (!isset($element['options'])) { $element['options'] = array(array('value' => '0', 'title' => 'r'), array('value' => '1', 'title' => 'y'), array('value' => '2', 'title' => 'g')); } $result .= "<span style='display:block;border:0px;padding-bottom:4px;width:70px !important;'>"; $colors = array('r' => '#FF8080', 'y' => '#FFEB80', 'g' => '#80B280'); $borders = array('r' => 'border-top:1px solid #000000;border-left:1px solid #000000;border-bottom:1px solid #000000;', 'y' => 'border-top:1px solid #000000;border-bottom:1px solid #000000', 'g' => 'border-top:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;'); foreach ($element['options'] as $e) { $checked = $form->get_value($element) === $e['value'] && !is_null($form->get_value($element)); if ($e['value'] == null) { $checked = false; } $result .= '<span style="' . $borders[$e['title']] . ';padding-bottom:4px;background-color:' . $colors[$e['title']] . '"><input type="radio" value="' . $e['value'] . '" ' . $form->element_attributes($element) . ($checked ? ' checked="checked"' : '') . ' style="vertical-align:top"></span>'; } $result .= "</span>"; $result .= '<div class="cl"></div>'; return $result; }
/** * 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'); }
/** * 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>'; }
/** * 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; }
/** * 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_authlist(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['default'])) { $value['default'] = ''; } if (is_array($value) && count($value)) { $smarty->assign('authtypes', $value['authtypes']); $smarty->assign('instancelist', $value['instancelist']); $smarty->assign('instancestring', implode(',', $value['instancearray'])); $smarty->assign('default', $value['default']); $smarty->assign('institution', $value['institution']); } $smarty->assign('name', $element['name']); $smarty->assign('cannotremove', json_encode(get_string('cannotremove', 'auth'))); $smarty->assign('cannotremoveinuse', json_encode(get_string('cannotremoveinuse', 'auth'))); $smarty->assign('saveinstitutiondetailsfirst', json_encode(get_string('saveinstitutiondetailsfirst', 'auth'))); $smarty->assign('noauthpluginconfigoptions', json_encode(get_string('noauthpluginconfigoptions', 'auth'))); return $smarty->fetch('form/authlist.tpl'); }
function pieform_element_rolepermissions(Pieform $form, $element) { /*{{{*/ $value = $form->get_value($element); $roles = group_get_role_info($element['group']); $permissions = array_keys(get_object_vars($value['member'])); $result = '<table class="editpermissions"><tbody>'; $result .= '<tr><th>' . get_string('Role', 'group') . '</th>'; foreach ($permissions as $p) { $result .= '<th>' . get_string('filepermission.' . $p, 'artefact.file') . '</th>'; } $result .= '</tr>'; $prefix = $form->get_name() . '_' . $element['name'] . '_p'; foreach ($roles as $r) { $result .= '<tr>'; $result .= '<td>' . hsc($r->display) . '</td>'; foreach ($permissions as $p) { $inputname = $prefix . '_' . $r->name . '_' . $p; $result .= '<td><input type="checkbox" class="permission" name="' . hsc($inputname) . '"'; if ($r->name == 'admin') { $result .= ' checked disabled'; } else { if ($value[$r->name]->{$p}) { $result .= ' checked'; } } $result .= '/></td>'; } $result .= '</tr>'; } $result .= '</tbody></table>'; return $result; }
/** * 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('name', $element['name']); $smarty->assign('addbuttonstr', get_string('addbutton', 'artefact.internal')); $smarty->assign('validationemailstr', json_encode(get_string('validationemailwillbesent', 'artefact.internal'))); return $smarty->fetch('form/emaillist.tpl'); }
/** * 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 submit button * * @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_submit(Pieform $form, $element) { /*{{{*/ if (isset($element['confirm'])) { $element['onclick'] = 'return confirm(' . json_encode($element['confirm']) . ');'; } return '<input type="submit"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Checks whether the given element's value is less than another element. * * Typically useful for dates. * * @param Pieform $form The form the rule is being applied to * @param string $value The value to check * @param array $element The element to check * @param string $otherelement The other element to check for * @return string The error message, if the value is invalid. */ function pieform_rule_before(Pieform $form, $value, $element, $otherelement) { /*{{{*/ $otherelement = $form->get_element($otherelement); $othervalue = $form->get_value($otherelement); if ($value != '' && $othervalue != '' && intval($value) > intval($othervalue)) { return sprintf($form->i18n('rule', 'before', 'before', $element), $otherelement['title']); } }
/** * Autocomplete list selector 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_autocomplete(Pieform $form, $element) { global $USER; $wwwroot = get_config('wwwroot'); $smarty = smarty_core(); $smarty->left_delimiter = '{{'; $smarty->right_delimiter = '}}'; $value = $form->get_value($element); $multiple = !empty($element['multiple']); if ($multiple) { $valuestr = implode(',', $value); } else { $valuestr = $value; } if (!empty($element['initfunction'])) { $initvalue = json_encode(call_user_func($element['initfunction'], $value)); } else { $initvalue = '[]'; } if (array_key_exists('mininputlength', $element)) { $mininputlength = $element['mininputlength']; } else { $mininputlength = 1; } $extraparams = ''; if (!empty($element['extraparams'])) { foreach ($element['extraparams'] as $k => $v) { if (!is_numeric($v) && !preg_match('/^function/', $v)) { if (preg_match('/^\'(.*)\'$/', $v, $match)) { $v = $match[1]; } $element['extraparams'][$k] = json_encode($v); } $extraparams .= $k . ': ' . $element['extraparams'][$k] . ','; } } $smarty->assign('id', $form->get_name() . '_' . $element['id']); $smarty->assign('name', $element['name']); $smarty->assign('value', $valuestr); // Pre-populate form element. $smarty->assign('initvalue', $initvalue); $smarty->assign('width', empty($element['width']) ? '300px' : $element['width']); $smarty->assign('multiple', $multiple ? 'true' : 'false'); $smarty->assign('mininputlength', $mininputlength); $smarty->assign('allowclear', empty($element['allowclear']) ? 'false' : 'true'); $smarty->assign('disabled', !empty($element['disabled']) ? 'true' : 'false'); $smarty->assign('ajaxurl', $element['ajaxurl']); $smarty->assign('sesskey', $USER->get('sesskey')); $smarty->assign('hint', empty($element['hint']) ? get_string('defaulthint') : $element['hint']); $smarty->assign('extraparams', $extraparams); $smarty->assign('inblockconfig', !empty($element['inblockconfig']) ? 'true' : 'false'); if (isset($element['description'])) { $smarty->assign('describedby', $form->element_descriptors($element)); } return $smarty->fetch('form/autocomplete.tpl'); }
/** * Renders an <input type="image"> button * * @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_image(Pieform $form, $element) { /*{{{*/ if (!isset($element['src'])) { throw new PieformException('"image" elements must have a "src" for the image'); } if (!isset($element['value'])) { $element['value'] = true; } return '<input type="image" src="' . Pieform::hsc($element['src']) . '"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * 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; }
/** * 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 $_PIEFORM_WYSIWYGS; $_PIEFORM_WYSIWYGS[] = $form->get_name() . '_' . $element['name']; if (is_html_editor_enabled()) { 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 (is_html_editor_enabled()) { $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>'; }
/** * Provides an element to manage a view ACL * * @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_viewacl(Pieform $form, $element) { $smarty = smarty_core(); $smarty->left_delimiter = '{{'; $smarty->right_delimiter = '}}'; $value = $form->get_value($element); // Look for the presets and split them into two groups $public = get_config('allowpublicviews') == '1'; $presets = array(); $loggedinindex = 0; if ($public) { $presets[] = 'public'; $loggedinindex = 1; } $presets[] = 'loggedin'; if ($form->get_property('userview')) { $presets[] = 'friends'; } if ($public) { $presets[] = 'token'; } if ($value) { foreach ($value as $key => &$item) { if (is_array($item)) { if (in_array($item['type'], $presets)) { $item['name'] = get_string($item['type'], 'view'); $item['preset'] = true; } else { $item['name'] = pieform_render_viewacl_getvaluebytype($item['type'], $item['id']); } // only show access that is still current. Expired access will be deleted if the form is saved if ($item['stopdate'] && time() > strtotime($item['stopdate'])) { unset($value[$key]); } } else { unset($value[$key]); } } } $potentialpresets = $presets; foreach ($potentialpresets as &$preset) { $preset = array('type' => $preset, 'id' => $preset, 'start' => null, 'end' => null, 'name' => get_string($preset, 'view'), 'preset' => true); } $smarty->assign('potentialpresets', json_encode($potentialpresets)); $smarty->assign('loggedinindex', $loggedinindex); $smarty->assign('accesslist', json_encode($value)); $smarty->assign('viewid', $form->get_property('viewid')); $smarty->assign('formname', $form->get_property('name')); return $smarty->fetch('form/viewacl.tpl'); }
/** * 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 . '">'; }
/** * Multiple file elements * * @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_files(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']; } $smarty->assign('name', $form->get_name() . '_' . $element['name']); if (isset($element['maxfilesize']) && is_int($element['maxfilesize'])) { $smarty->assign('maxfilesize', $element['maxfilesize']); } return $smarty->fetch('form/files.tpl'); }
/** * Multiple text elements * * @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_multitext(Pieform $form, $element) { $smarty = smarty_core(); $smarty->left_delimiter = '{{'; $smarty->right_delimiter = '}}'; $values = array(); foreach ($form->get_value($element) as $v) { if ($v) { $values[] = hsc($v); } } $smarty->assign('value', $values); $smarty->assign('next', count($values)); $smarty->assign('name', $form->get_name() . '_' . $element['name']); return $smarty->fetch('form/multitext.tpl'); }
/** * Provides a basic text field input. * * @todo this is just lies ... * @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_userlist(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 (is_array($value) && count($value)) { $orderby = isset($element['searchparams']['orderby']) && $element['searchparams']['orderby'] == 'lastname' ? 'lastname,firstname,id' : 'firstname,lastname,id'; $members = get_records_select_assoc('usr', 'id IN (' . join(',', array_map('intval', $value)) . ')', null, $orderby, 'id,username,firstname,lastname,preferredname,staff'); foreach ($members as &$member) { $member = display_name($member); } $smarty->assign('options', $members); $smarty->assign('value', join(',', $value)); } $smarty->assign('name', $element['name']); if (!empty($element['lefttitle'])) { $smarty->assign('lefttitle', $element['lefttitle']); } if (!empty($element['righttitle'])) { $smarty->assign('righttitle', $element['righttitle']); } if (!empty($element['leftarrowlabel'])) { $smarty->assign('leftarrowlabel', $element['leftarrowlabel']); } if (!empty($element['rightarrowlabel'])) { $smarty->assign('rightarrowlabel', $element['rightarrowlabel']); } if (!empty($element['group'])) { $smarty->assign('group', $element['group']); $smarty->assign('includeadmins', !isset($element['includeadmins']) || $element['includeadmins'] ? 1 : 0); } if (empty($element['searchscript'])) { $element['searchscript'] = 'json/usersearch.php'; } $smarty->assign('searchscript', $element['searchscript']); if (empty($element['searchparams'])) { $element['searchparams'] = array('query' => '', 'limit' => 100); } $smarty->assign('searchparams', json_encode($element['searchparams'])); $smarty->assign('onlyshowingfirst', json_encode(get_string('onlyshowingfirst', 'admin'))); $smarty->assign('resultsof', json_encode(get_string('resultsof', 'admin'))); return $smarty->fetch('form/userlist.tpl'); }
/** * Renders a drag & drop sortable list using Javascript and CSS. * Useful for ranking answers to a question. * * Based on ToolMan DHTML Library. See: http://tool-man.org/examples/sorting.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_rank(Pieform $form, $element) { /*{{{*/ $optionsavailable = true; if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) { $optionsavailable = false; Pieform::info('Rank elements should have at least one option'); } if (!empty($element['collapseifoneoption']) && isset($element['options']) && is_array($element['options']) && count($element['options']) == 1) { foreach ($element['options'] as $key => $value) { if (is_array($value)) { $value = $value['value']; } $result = Pieform::hsc($value) . '<input type="hidden" name="' . Pieform::hsc($element['name']) . '" value="' . Pieform::hsc($key) . '">'; } return $result; } $result = '<ul' . $form->element_attributes($element) . ">\n"; if (!$optionsavailable) { $result .= "\t<li> </li>\n</ul>"; return $result; } // Values are serialized like: a|b|c|d // so we need to unserialze them... $values = explode('|', $form->get_value($element)); $optionselected = false; foreach ($element['options'] as $key => $value) { // Get the value to display/put in the value attribute if (is_array($value)) { if (!isset($value['value'])) { Pieform::info('No value set for option "' . $key . '" of rank element "' . $element['name'] . '"'); $value = ''; } else { $value = $value['value']; } } $result .= "\t<li itemID=\"" . Pieform::hsc($key) . "\">" . Pieform::hsc($value) . "</li>\n"; } if (!$optionselected && !is_array($values) && $values !== null) { Pieform::info('Invalid value for rank "' . $element['name'] . '"'); } $result .= '</ul>'; return $result; }
/** * Renders an <input type="image"> button * * @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_image(Pieform $form, $element) { /*{{{*/ if (!isset($element['src'])) { throw new PieformException('"image" elements must have a "src" for the image'); } if (!isset($element['value'])) { $element['value'] = true; } if (isset($element['confirm'])) { $element['onclick'] = 'return confirm(' . json_encode($element['confirm']) . ');'; } if (!isset($element['alt'])) { if (isset($element['elementtitle'])) { $element['alt'] = $element['elementtitle']; } else { $element['alt'] = ''; } } return '<input type="image" src="' . Pieform::hsc($element['src']) . '"' . ' alt="' . Pieform::hsc($element['alt']) . '"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Provides a mechanism for choosing one or more artefacts from a list of them. * * @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_artefactchooser(Pieform $form, $element) { global $USER, $pagination_js; $value = $form->get_value($element); $element['offset'] = param_integer('offset', 0); list($html, $pagination, $count) = View::build_artefactchooser_data($element, $form->get_property('viewgroup'), $form->get_property('viewinstitution')); $smarty = smarty_core(); $smarty->assign('datatable', $element['name'] . '_data'); $smarty->assign('artefacts', $html); $smarty->assign('pagination', $pagination['html']); $formname = $form->get_name(); $smarty->assign('blockinstance', substr($formname, strpos($formname, '_') + 1)); // Save the pagination javascript for later, when it is asked for. This is // messy, but can't be helped until Pieforms goes to a more OO way of // managing stuff. $pagination_js = $pagination['javascript']; $baseurl = View::make_base_url(); $smarty->assign('browseurl', $baseurl); $smarty->assign('searchurl', $baseurl . '&s=1'); $smarty->assign('searchable', !empty($element['search'])); return $smarty->fetch('form/artefactchooser.tpl'); }
/** * Provides a control that allows the user to input where in a list they want * to put something. Handy for indicating the order of some objects. * * @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_weight(Pieform $form, $element) { pieform_element_weight_validate_element($element); $default = isset($element['defaultvalue']) ? intval($element['defaultvalue']) : 0; $result = '<input type="radio"' . $form->element_attributes($element) . ' value="0"'; if ($default == 0) { $result .= ' checked="checked"'; } $result .= '>'; $i = 0; foreach ($element['existing'] as $existing) { $i++; $result .= "<br>" . Pieform::hsc($existing['title']); $result .= "<br><input type=\"radio\"" . $form->element_attributes($element) . " value=\"{$i}\""; if ($i == $default) { $result .= ' checked="checked"'; } $result .= '>'; } return $result; return '<input type="text"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }
/** * Renders a set of radio buttons for scale type questions in surveys. * * @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_scale(Pieform $form, $element) { /*{{{*/ $global = $form->get_property('method') == 'get' ? $_GET : $_POST; $submitted = $form->is_submitted(); if ($submitted && isset($global[$element['name']])) { $value = $global[$element['name']]; } $result = ''; $separator = null; //$separator = ''; if (isset($element['separator'])) { $separator = $element['separator']; } $length = 17; if (isset($element['length'])) { $length = $element['length']; } if (!isset($element['options'])) { $steps = 5; if (isset($element['steps'])) { if ($element['steps'] < 2) { $steps = 2; } elseif ($element['steps'] > 10) { $steps = 10; } else { $steps = $element['steps']; } } $element['options'] = array(); for ($i = 0; $i < $steps; $i++) { $element['options'] = array_merge($element['options'], array(array('value' => strval($i), 'title' => ''))); } } // If needed, set answers in reverse order if (isset($element['reverse']) && $element['reverse'] == 'true') { $element['options'] = array_reverse($element['options']); } //$result .= "<span style='display:block-inline'>"; $result .= "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"; $result .= "<td width='45%' valign='top' align='right'>"; if (isset($element['labelleft'])) { $result .= "<strong>" . $element['labelleft'] . "</strong>"; } if (isset($element['titleleft'])) { $result .= $element['titleleft']; } $result .= "</td>"; $result .= "<td width='10%' valign='top' align='center'>"; $result .= "<div style='white-space:nowrap;'>"; foreach ($element['options'] as $e) { $checked = $form->get_value($element) === $e['value'] && !is_null($form->get_value($element)); if ($e['value'] == null) { $checked = false; } $result .= '<input type="radio" value="' . $e['value'] . '" ' . $form->element_attributes($element) . ($checked ? ' checked="checked"' : '') . ' style="vertical-align:top">' . Pieform::hsc(str_shorten_text($e['title'], $length, true)); $result .= $separator; } // Strip the separator after the last option... //$result = substr($result, 0, -strlen($separator)); $result .= "</div>"; $result .= "</td>"; $result .= "<td width='45%' valign='top' align='left'>"; if (isset($element['titleright'])) { $result .= $element['titleright']; } if (isset($element['labelright'])) { $result .= "<strong>" . $element['labelright'] . "</strong>"; } $result .= "</td>"; $result .= "</tr></table>"; //$result .= "</span>"; $result .= '<div class="cl"></div>'; return $result; }
/** * Renders a dropdown list, including support for multiple choices. * * @todo Currently, putting a junk defaultvalue/value for a multiple select * does not trigger any kind of error, it should perhaps trigger a * Pieform::info * * @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_select(Pieform $form, $element) { /*{{{*/ if (!empty($element['multiple'])) { $element['name'] .= '[]'; } $optionsavailable = true; if (!isset($element['options']) || !is_array($element['options']) || count($element['options']) < 1) { $optionsavailable = false; Pieform::info('Select elements should have at least one option'); } if (!empty($element['collapseifoneoption']) && isset($element['options']) && is_array($element['options']) && count($element['options']) == 1) { foreach ($element['options'] as $key => $value) { if (is_array($value)) { $value = $value['value']; } $result = $value . '<input type="hidden" name="' . $element['name'] . '" value="' . $key . '">'; } return $result; } $result = '<select' . $form->element_attributes($element) . (!empty($element['multiple']) ? ' multiple="multiple"' : '') . ">\n"; if (!$optionsavailable) { $result .= "\t<option></option>\n</select>"; return $result; } $values = $form->get_value($element); $optionselected = false; foreach ($element['options'] as $key => $value) { // Select the element if it's in the values or if there are no values // and this is the first option if (!is_array($values) && $key == $values || is_array($values) && (in_array($key, $values) || isset($values[0]) && $values[0] === null && !$optionselected)) { $selected = ' selected="selected"'; $optionselected = true; } else { $selected = ''; } // Disable the option if necessary if (is_array($value) && !empty($value['disabled'])) { $disabled = ' disabled="disabled"'; } else { $disabled = ''; } // Add a label if necessary. None of the common browsers actually render // this properly at the moment, but that may change in future. if (is_array($value) && isset($value['label'])) { $label = ' label="' . Pieform::hsc($value['label']) . '"'; } else { $label = ''; } // Get the value to display/put in the value attribute if (is_array($value)) { if (!isset($value['value'])) { Pieform::info('No value set for option "' . $key . '" of select element "' . $element['name'] . '"'); $value = ''; } else { $value = $value['value']; } } $result .= "\t<option value=\"" . Pieform::hsc($key) . "\"{$selected}{$label}{$disabled}>" . Pieform::hsc($value) . "</option>\n"; } if (!$optionselected && !is_array($values) && $values !== null) { Pieform::info('Invalid value for select "' . $element['name'] . '"'); } $result .= '</select>'; return $result; }
/** * Renders a dropdown list, including support for multiple choices. * * Configuration options: * * In addition to the standard pieform element configuration options, the select element * can take these. Each one must include an "options", "optgroups", or "optionurl" * parameter, because otherwise it won't have any options. * * - "options" (array): The list of items in the menu. A little confusingly, the array key * for each array entry is used as the "value" attribute for the <option> tag. Each array * value should usually be a string, in which case it will be displayed as the text between * the <option> tags. Or for more advanced functionality each array value can be an array * with these keys: * --- "value": The string to display inside the <option> tags * --- "disabled" (boolean default false): Set to true to disable this option * --- "style": CSS styling to set on this option * --- "label": sets a "label" attribute on the <option> tag * * - "optgroups" (array of arrays): Provide this INSTEAD of "options" if you want to use * optgroups. This should be an array of arrays. Each entry in the array represents an * optgroup (the key is ignored). Each optgroup array should contain an item with the * key "label", which will be the user-displayed label, and an item with the key "options", * which should be a list of options to go in that outgroup, with the same format as a * standard "options" array. * * - "multiple" (boolean, default false): Indicates whether to allow multiple items to be selected * * - "collapseifoneoption" (boolean, default true): If there's only one option, print it as a string * instead of as a menu. * * - "allowother" (boolean, default false): Says to add an "other" free text field to the menu. If there * is a menu option with value "other", then selecting this will display the free text * field. If there is no menu option with value "other", one will be added to the option * list. * * - "width" (deprecated): How wide to make this select element in pixels. Better to do this with CSS. * * - "height" (deprecated): How tall to make this select element in pixels. Better to do this with CSS. * * @todo Currently, putting a junk defaultvalue/value for a multiple select * does not trigger any kind of error, it should perhaps trigger a * Pieform::info * * @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_select(Pieform $form, $element) { if (!empty($element['multiple'])) { $element['name'] .= '[]'; } if (!empty($element['allowother']) and !isset($element['options']['other'])) { $element['options']['other'] = get_string('element.select.other', 'pieforms'); } $optionsavailable = true; $options = pieform_element_select_get_options($element); if (empty($options)) { $optionsavailable = false; Pieform::info('Select elements should have at least one option'); } if (!empty($element['collapseifoneoption']) && is_array($options) && count($options) == 1) { foreach ($options as $key => $value) { if (is_array($value)) { $value = $value['value']; } $result = Pieform::hsc($value) . '<input type="hidden" id="' . $form->make_id($element, true) . '" name="' . Pieform::hsc($element['name']) . '" value="' . Pieform::hsc($key) . '">'; } return $result; } $result = '<select' . $form->element_attributes($element) . (!empty($element['multiple']) ? ' multiple="multiple"' : '') . (!empty($element['allowother']) ? ' onChange="pieform_select_other(this);"' : '') . (!empty($element['width']) ? ' style="width: ' . $element['width'] . 'px;' : ' style="') . (!empty($element['height']) ? ' height: ' . $element['height'] . 'px;"' : '"') . ">\n"; if (!$optionsavailable) { $result .= "\t<option></option>\n</select>"; return $result; } $values = $form->get_value($element); $optionselected = false; if (!empty($element['allowother'])) { $use_other = $values; foreach ($element['options'] as $key => $value) { if (!is_array($values) && $key == $values || is_array($values) && in_array($key, $values)) { unset($use_other); break; } } if (isset($use_other)) { $values = 'other'; } } if (empty($element['optgroups'])) { $result .= pieform_element_select_render_options($element['options'], $values, $optionselected, $element); } else { foreach ($element['optgroups'] as $optgroup) { $result .= "\t<optgroup label=\"" . hsc($optgroup['label']) . '">' . "\n" . pieform_element_select_render_options($optgroup['options'], $values, $optionselected, $element) . "\t</optgroup>\n"; } } if (!$optionselected && !is_array($values) && $values !== null) { Pieform::info('Invalid value for select "' . $element['name'] . '"'); } $result .= '</select>'; if (!empty($element['allowother'])) { $other_attrib = array('name' => $element['name'] . '_other', 'id' => $element['id'] . '_other'); if (isset($use_other)) { $other_value = ' value="' . hsc($use_other) . '"'; } else { $other_attrib['class'] = 'hidden'; $other_value = ''; } $result .= '<label for="' . $element['id'] . '_other" class="accessible-hidden">' . get_string('licenseotherurl') . '</label>' . '<input type="text"' . $form->element_attributes($other_attrib) . $other_value . ">\n"; } return $result; }
/** * Provides a javascript calendar for inputting a date/time. * * General documentation about the calendar is available at * http://api.jqueryui.com/datepicker/ * General documentation about the timepicker addon is available at * http://trentrichardson.com/examples/timepicker/ * * @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) { /*{{{*/ global $LANGDIRECTION; $id = $form->get_name() . '_' . $element['name']; $value = $form->get_value($element); if ($value) { $value = Pieform::hsc(strftime($element['caloptions']['ifFormat'], $value)); } // Build the configuring javascript $options = array_merge($element['caloptions'], array('inputField' => $id)); if (empty($options['dateFormat'])) { $options['dateFormat'] = get_string('calendar_dateFormat', 'langconfig'); } // Set up default timeFormat if needed if (!empty($options['showsTime']) && empty($options['timeFormat'])) { $options['timeFormat'] = get_string('calendar_timeFormat', 'langconfig'); } $options = pieform_element_calendar_get_lang_strings($options, $LANGDIRECTION); // Build the HTML $result = '<input type="text"' . $form->element_attributes($element) . ' value="' . $value . '">'; $result .= '<script type="text/javascript"> var input = jQuery("input#' . $id . '");'; if (!empty($options['showsTime'])) { $result .= 'input.datetimepicker({'; } else { $result .= 'input.datepicker({'; } $result .= ' onSelect: function(date) { if (typeof formchangemanager !== \'undefined\') { var form = input.closest(\'form\')[0]; formchangemanager.setFormState(form, FORM_CHANGED); } },'; foreach ($options as $key => $option) { if (is_numeric($option)) { $result .= $key . ': ' . $option . ','; } else { if (is_array($option)) { foreach ($option as $k => $v) { if (!is_numeric($v)) { if (preg_match('/^\'(.*)\'$/', $v, $match)) { $v = $match[1]; } $option[$k] = json_encode($v); } } $option = '[' . implode(',', $option) . ']'; $result .= $key . ': ' . $option . ','; } else { $result .= $key . ': ' . json_encode($option) . ','; } } } // Adding prev / next year buttons $result .= ' beforeShow: function(input, inst) { setTimeout(function() { add_prev_next_year(inst); }, 1); }, onChangeMonthYear: function(y, m, inst) { setTimeout(function() { add_prev_next_year(inst); }, 1); }, '; if (isset($element['imagefile'])) { $result .= 'showOn: "button", buttonImage: "' . $element['imagefile'] . '", buttonText: "' . get_string('element.calendar.opendatepicker', 'pieforms') . '",'; } $result .= ' }); </script>'; return $result; }
/** * Provides an element to manage a view ACL * * @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_viewacl(Pieform $form, $element) { global $USER, $SESSION, $LANGDIRECTION; $strlen = function_exists('mb_strlen') ? 'mb_strlen' : 'strlen'; $smarty = smarty_core(); $smarty->left_delimiter = '{{'; $smarty->right_delimiter = '}}'; $value = $form->get_value($element); // Look for the presets and split them into two groups require_once get_config('libroot') . 'antispam.php'; $public = false; if (is_probationary_user()) { $public = false; } else { if (get_config('allowpublicviews') && $USER->institution_allows_public_views()) { $public = true; } else { if (get_config('allowpublicprofiles') && $element['viewtype'] == 'profile') { $public = true; } } } $allpresets = array('public', 'loggedin', 'friends'); $allowedpresets = array(); $loggedinindex = 0; if ($public) { $allowedpresets[] = 'public'; $loggedinindex = 1; } $allowedpresets[] = 'loggedin'; if ($form->get_property('userview')) { $allowedpresets[] = 'friends'; } $accesslist = array(); if ($value) { foreach ($value as $item) { if (is_array($item)) { if ($item['type'] == 'public') { $item['publicallowed'] = (int) $public; } if (in_array($item['type'], $allpresets)) { $item['name'] = get_string($item['type'] == 'loggedin' ? 'registeredusers' : $item['type'], 'view'); $item['preset'] = true; } else { $item['name'] = pieform_render_viewacl_getvaluebytype($item['type'], $item['id']); } if ($strlen($item['name']) > 30) { $item['shortname'] = str_shorten_text($item['name'], 30, true); } // only show access that is still current. Expired access will be deleted if the form is saved if ($form->is_submitted() || empty($item['stopdate']) || time() <= strtotime($item['stopdate'])) { $accesslist[] = $item; } } } } $defaultaccesslist = $accesslist ? 0 : 1; $myinstitutions = array(); foreach ($USER->get('institutions') as $i) { $myinstitutions[] = array('type' => 'institution', 'id' => $i->institution, 'start' => null, 'end' => null, 'name' => hsc($i->displayname), 'preset' => false); } foreach ($allowedpresets as &$preset) { $preset = array('type' => $preset, 'id' => $preset, 'start' => null, 'end' => null, 'name' => get_string($preset == 'loggedin' ? 'registeredusers' : $preset, 'view'), 'preset' => true); } $allgroups = array('type' => 'allgroups', 'id' => 'allgroups', 'start' => null, 'end' => null, 'name' => get_string('allmygroups', 'group'), 'preset' => true); $mygroups = array(); foreach (group_get_user_groups($USER->get('id')) as $g) { $group = array('type' => 'group', 'id' => $g->id, 'start' => null, 'end' => null, 'name' => $g->name, 'preset' => false); if ($strlen($g->name) > 30) { $group['shortname'] = str_shorten_text($g->name, 30, true); } $mygroups[] = $group; } $faves = array(); foreach (get_user_favorites($USER->get('id')) as $u) { $fave = array('type' => 'user', 'id' => $u->id, 'start' => null, 'end' => null, 'name' => $u->name, 'preset' => false); if ($strlen($u->name) > 30) { $fave['shortname'] = str_shorten_text($u->name, 30, true); } $faves[] = $fave; } require_once get_config('libroot') . 'pieforms/pieform/elements/calendar.php'; $options = array('dateFormat' => get_string('calendar_dateFormat', 'langconfig'), 'timeFormat' => get_string('calendar_timeFormat', 'langconfig'), 'stepHour' => 1, 'stepMinute' => 5); $options = pieform_element_calendar_get_lang_strings($options, $LANGDIRECTION); $datepickeroptionstr = ''; foreach ($options as $key => $option) { if (is_numeric($option)) { $datepickeroptionstr .= $key . ': ' . $option . ','; } else { if (is_array($option)) { foreach ($option as $k => $v) { if (!is_numeric($v)) { if (preg_match('/^\'(.*)\'$/', $v, $match)) { $v = $match[1]; } $option[$k] = json_encode($v); } } $option = '[' . implode(',', $option) . ']'; $datepickeroptionstr .= $key . ': ' . $option . ','; } else { $datepickeroptionstr .= $key . ': ' . json_encode($option) . ','; } } } $smarty->assign('datepickeroptions', $datepickeroptionstr); $smarty->assign('viewtype', $element['viewtype']); $smarty->assign('potentialpresets', json_encode($allowedpresets)); $smarty->assign('loggedinindex', $loggedinindex); $smarty->assign('accesslist', json_encode($accesslist)); $smarty->assign('defaultaccesslist', $defaultaccesslist); $smarty->assign('viewid', $form->get_property('viewid')); $smarty->assign('formname', $form->get_property('name')); $smarty->assign('myinstitutions', json_encode($myinstitutions)); $smarty->assign('allowcomments', $element['allowcomments']); $smarty->assign('allgroups', json_encode($allgroups)); $smarty->assign('mygroups', json_encode($mygroups)); $smarty->assign('faves', json_encode($faves)); return $smarty->fetch('form/viewacl.tpl'); }
/** * Renders a password field * * @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_password(Pieform $form, $element) { /*{{{*/ return '<input type="password"' . $form->element_attributes($element) . ' value="' . Pieform::hsc($form->get_value($element)) . '">'; }