Adding optgroup support in quickform + using formvalidator when creating an event not ready for 1.9.4 see #5208

skala
Julio Montoya 12 years ago
parent 54891f4083
commit 401d41fc0c
  1. 7
      main/admin/calendar.lib.php
  2. 129
      main/calendar/agenda.lib.php
  3. 6
      main/calendar/agenda.php
  4. 126
      main/inc/lib/pear/HTML/QuickForm/select.php

@ -1248,11 +1248,8 @@ function show_add_form($id = '') {
$content=stripslashes($content);
$title=stripslashes($title);
// we start a completely new item, we do not come from the resource linker
if ($_GET['originalresource']!=="no" and $_GET['action']=="add")
{
$_SESSION["formelements"]=null;
//unset_session_resources();//--------------------------------------------------------------------------------------
if ($_GET['originalresource']!=="no" and $_GET['action']=="add") {
$_SESSION["formelements"]=null;
}
?>

@ -693,13 +693,9 @@ class Agenda {
* @author: Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @return html code
*/
function construct_not_selected_select_form($group_list = null, $user_list = null, $to_already_selected = array()) {
static function construct_not_selected_select_form($group_list = null, $user_list = null, $to_already_selected = array()) {
$html = '<select id="users_to_send_id" data-placeholder="'.get_lang('Select').'" name="users_to_send[]" multiple="multiple" style="width:250px" class="chzn-select">';
// adding the groups to the select form
if (isset($to_already_selected) && $to_already_selected==='everyone') {
}
$html .= '<option value="everyone">'.get_lang('Everyone').'</option>';
if (is_array($group_list)) {
@ -737,6 +733,52 @@ class Agenda {
return $html;
}
static function construct_not_selected_select_form_validator($form, $group_list = null, $user_list = null, $to_already_selected = array()) {
$params = array(
'id' => 'users_to_send_id',
'data-placeholder'=> get_lang('Select'),
'multiple' => 'multiple',
'style' => 'width:250px',
'class' => 'chzn-select'
);
$select = $form->addElement('select', 'users_to_send', get_lang('To'), null, $params);
$select->addOption(get_lang('Everyone'), 'everyone');
$options = array();
if (is_array($group_list)) {
foreach ($group_list as $this_group) {
if (!is_array($to_already_selected) || !in_array("GROUP:".$this_group['id'], $to_already_selected)) {
// $to_already_selected is the array containing the groups (and users) that are already selected
$count_users = isset($this_group['count_users']) ? $this_group['count_users'] : $this_group['userNb'];
$count_users = " &ndash; $count_users ".get_lang('Users');
$options[] = array('text' => $this_group['name'].$count_users, 'value' => "GROUP:".$this_group['id']);
}
}
$select->addOptGroup($options, get_lang('Groups'));
$html .= '</optgroup>';
}
// adding the individual users to the select form
if (is_array($group_list)) {
$options = array();
foreach ($user_list as $this_user) {
// $to_already_selected is the array containing the users (and groups) that are already selected
if (!is_array($to_already_selected) || !in_array("USER:".$this_user['user_id'],$to_already_selected)) {
//$username = api_htmlentities(sprintf(get_lang('LoginX'), $this_user['username']), ENT_QUOTES);
// @todo : add title attribute $username in the jqdialog window. wait for a chosen version to inherit title attribute
// from <option> to <li>
//$html .= '<option title="'.$username.'" value="USER:'.$this_user['user_id'].'">'.api_get_person_name($this_user['firstname'], $this_user['lastname']).' ('.$this_user['username'].') </option>';
$options[] = array('text' => api_get_person_name($this_user['firstname'], $this_user['lastname']).' ('.$this_user['username'].')',
'value' => "USER:".$this_user['user_id']);
}
}
$select->addOptGroup($options, get_lang('Users'));
}
}
/**
* This function separates the users from the groups
* users have a value USER:XXX (with XXX the dokeos id
@ -771,6 +813,83 @@ class Agenda {
}
return $send_to;
}
static function show_form($params = array()) {
$form = new FormValidator('add_event', 'POST', api_get_self().'?'.api_get_cidreq(), null, array('enctype' => 'multipart/form-data'));
$id = isset($params['id']) ? $params['id'] : null;
if ($id) {
$form_title = get_lang('ModifyCalendarItem');
$button = get_lang('ModifyEvent');
} else {
$form_title = get_lang('AddCalendarItem');
$button = get_lang('AgendaAdd');
}
$form->addElement('header', $form_title);
$form->addElement('hidden', 'id', $id);
$form->addElement('hidden', 'action', $params['action']);
$form->addElement('hidden', 'id_attach', $params['id_attach']);
$form->addElement('text', 'title', get_lang('ItemTitle'));
$group_id = api_get_group_id();
if (isset ($group_id) && !empty($group_id)) {
$form->addElement('hidden', 'selected_form[0]', "GROUP:'.$group_id.'");
$form->addElement('hidden', 'to', 'true');
} else {
self::show_to_form($form, $to);
}
$form->addElement('text', 'start_date', get_lang('StartDate'));
$form->addElement('text', 'end_date', get_lang('EndDate'));
if (empty($id)) {
$form->addElement('advanced_settings', '<a href="javascript://" onclick="return plus_repeated_event();"><span id="plus2">
<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('RepeatEvent').'</span>
</a>');
$form->addElement('html', '<div style="display:block">');
$form->addElement('checkbox', 'repeat', null, get_lang('RepeatEvent'));
$repeat_events = array(
'daily' => get_lang('RepeatDaily'),
'weekly' => get_lang('RepeatWeekly'),
'monthlyByDate' => get_lang('RepeatMonthlyByDate'),
'yearly' => get_lang('RepeatYearly')
);
$form->addElement('select', 'repeat_type', get_lang('RepeatType'), $repeat_events);
$form->addElement('text', 'repeat_end_day', get_lang('RepeatEnd'));
$form->addElement('html', '</div>');
if(!api_is_allowed_to_edit(null,true)) {
$toolbar = 'AgendaStudent';
} else {
$toolbar = 'Agenda';
}
//$form->addElement('html_editor', 'content', get_lang('Description'), null, array('ToolbarSet' => $toolbar, 'Width' => '100%', 'Height' => '200'));
$form->addElement('file', 'user_upload', get_lang('AddAnAttachment'));
$form->addElement('text', 'file_comment', get_lang('Comment'));
}
$form->addElement('button', 'submit', $button);
$form->display();
}
static function show_to_form($form, $to_already_selected) {
$order = 'lastname';
if (api_is_western_name_order) {
$order = 'firstname';
}
$user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id(), null, $order);
$group_list = CourseManager::get_group_list_of_course(api_get_course_id(), api_get_session_id());
self::construct_not_selected_select_form_validator($form, $group_list, $user_list, $to_already_selected);
}
}

@ -219,10 +219,8 @@ if (api_is_allowed_to_edit(false,true) OR (api_get_course_setting('allow_user_ed
$end_d = intval($_POST['repeat_end_day']);
$end = mktime(23, 59, 59, $end_m, $end_d, $end_y);
$res = agenda_add_repeat_item($course_info,$id, $_POST['repeat_type'], $end,$_POST['selected_form'], $safe_file_comment);
}
Display::display_confirmation_message(get_lang('AddSuccess'));
}
Display::display_confirmation_message(get_lang('AddSuccess'));
} else {
show_add_form();
}

@ -1,43 +1,43 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Class to dynamically create an HTML SELECT
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category HTML
* @package HTML_QuickForm
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @author Alexey Borzov <avb@php.net>
* @copyright 2001-2009 The PHP Group
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version CVS: $Id: select.php,v 1.34 2009/04/04 21:34:04 avb Exp $
* @link http://pear.php.net/package/HTML_QuickForm
*/
/**
* Base class for form elements
*/
require_once 'HTML/QuickForm/element.php';
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Class to dynamically create an HTML SELECT
*
* @category HTML
* @package HTML_QuickForm
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @author Alexey Borzov <avb@php.net>
* @version Release: 3.2.11
* @since 1.0
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category HTML
* @package HTML_QuickForm
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @author Alexey Borzov <avb@php.net>
* @copyright 2001-2009 The PHP Group
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version CVS: $Id: select.php,v 1.34 2009/04/04 21:34:04 avb Exp $
* @link http://pear.php.net/package/HTML_QuickForm
*/
/**
* Base class for form elements
*/
require_once 'HTML/QuickForm/element.php';
/**
* Class to dynamically create an HTML SELECT
*
* @category HTML
* @package HTML_QuickForm
* @author Adam Daniel <adaniel1@eesus.jnj.com>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @author Alexey Borzov <avb@php.net>
* @version Release: 3.2.11
* @since 1.0
*/
class HTML_QuickForm_select extends HTML_QuickForm_element {
@ -51,6 +51,8 @@ class HTML_QuickForm_select extends HTML_QuickForm_element {
* @access private
*/
var $_options = array();
var $_optgroups = array();
/**
* Default values of the SELECT
@ -299,10 +301,10 @@ class HTML_QuickForm_select extends HTML_QuickForm_element {
* @access public
* @return void
*/
function addOption($text, $value, $attributes=null)
function addOption($text, $value, $attributes = null, $return_array = false)
{
if (null === $attributes) {
$attributes = array('value' => (string)$value);
$attributes = array('value' => (string)$value);
} else {
$attributes = $this->_parseAttributes($attributes);
if (isset($attributes['selected'])) {
@ -314,9 +316,33 @@ class HTML_QuickForm_select extends HTML_QuickForm_element {
$this->_values[] = $value;
}
}
$this->_updateAttrArray($attributes, array('value' => (string)$value));
$this->_updateAttrArray($attributes, array('value' => (string)$value));
}
if ($return_array) {
return array('text' => $text, 'attr' => $attributes);
} else {
$this->_options[] = array('text' => $text, 'attr' => $attributes);
}
$this->_options[] = array('text' => $text, 'attr' => $attributes);
} // end func addOption
/**
* Adds a new OPTION to the SELECT
*
* @param string $text Display text for the OPTION
* @param string $value Value for the OPTION
* @param mixed $attributes Either a typical HTML attribute string
* or an associative array
* @since 1.0
* @access public
* @return void
*/
function addOptGroup($options, $label)
{
foreach ($options as &$option) {
$option[] = $this->addOption($option['text'], $option['value'], null, true);
}
$this->_optgroups[] = array('label' => $label, 'options' => $options);
} // end func addOption
// }}}
@ -496,15 +522,25 @@ class HTML_QuickForm_select extends HTML_QuickForm_element {
}
$strHtml .= $tabs . '<select' . $attrString . ">\n";
$strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
$strValues = is_array($this->_values)? array_map('strval', $this->_values): array();
foreach ($this->_options as $option) {
if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
$option['attr']['selected'] = 'selected';
if (!empty($strValues) && in_array($option['attr']['value'], $strValues, true)) {
$option['attr']['selected'] = 'selected';
}
$strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' .
$option['text'] . "</option>\n";
}
foreach ($this->_optgroups as $optgroup) {
$strHtml .= $tabs . "\t<optgroup label=" . $optgroup['label'].">";
foreach($optgroup['options'] as $option) {
$strHtml .= $tabs . "\t<option" . $this->_getAttrString($option['attr']) . '>' .
$option['text'] . "</option>\n";
}
$strHtml .= "</optgroup>\n";
}
return $strHtml . $tabs . '</select>';
}
} //end func toHtml
@ -525,7 +561,7 @@ class HTML_QuickForm_select extends HTML_QuickForm_element {
if (is_array($this->_values)) {
foreach ($this->_values as $key => $val) {
for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
if (0 == strcmp($val, $this->_options[$i]['attr']['value'])) {
$value[$key] = $this->_options[$i]['text'];
break;
}
@ -572,7 +608,7 @@ class HTML_QuickForm_select extends HTML_QuickForm_element {
$cleanValue = null;
foreach ($value as $v) {
for ($i = 0, $optCount = count($this->_options); $i < $optCount; $i++) {
if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
if (0 == strcmp($v, $this->_options[$i]['attr']['value'])) {
$cleanValue[] = $v;
break;
}

Loading…
Cancel
Save