Example #1
0
function html_render_tags_editor($props, $return = false)
{
    // Defaults
    $tags;
    $selected_tags = array();
    $select_name = 'tags[]';
    $any = false;
    $label = __('Selected tags');
    $disabled = false;
    $visible = false;
    if (!isset($props)) {
        $props = array();
    }
    if (isset($props['tags'])) {
        $tags = $props['tags'];
    } else {
        $tags = get_available_tags_indexed();
        $tags = get_available_tags();
        $tag_ids = array_map(function ($tag) {
            return $tag[TAGS_TABLE_ID_COL];
        }, $tags);
        $tags = array_combine($tag_ids, $tags);
    }
    // Selected tags
    if (isset($props['selected_tags'])) {
        $selected_tags = $props['selected_tags'];
    }
    // Select name
    if (isset($props['select_name'])) {
        $select_name = $props['select_name'];
    }
    // Tags multi selector
    $tag_ids = array_map(function ($tag) {
        return $tag[TAGS_TABLE_ID_COL];
    }, $tags);
    $tag_names = array_map(function ($tag) {
        return $tag[TAGS_TABLE_NAME_COL];
    }, $tags);
    $tags_for_select = array_combine($tag_ids, $tag_names);
    $select_selected_tags = html_print_select($tags_for_select, $select_name, $selected_tags, '', '', 0, true, true, true, '', $disabled, 'display:none;');
    // Tags simple selector
    if (!empty($selected_tags)) {
        $selected_tags_comb = array_combine($selected_tags, $selected_tags);
    } else {
        $selected_tags_comb = array();
    }
    $not_added_tags = array_diff_key($tags, $selected_tags_comb);
    $select_add_tags = '<div class="tags-select">';
    $select_add_tags .= html_print_select($not_added_tags, 'add-tags-select', array(), '', __('Select'), 0, true, false, true, '', $disabled);
    $select_add_tags .= '</div>';
    // Tags view
    $view_tags_selected = '<div class="tags-view"></div>';
    ob_start();
    echo '<div class="tags-editor">';
    echo $select_selected_tags;
    echo $select_add_tags;
    echo $view_tags_selected;
    echo '</div>';
    ?>
	<script type="text/javascript">
	(function ($) {
		
		var TAGS_TABLE_ID_COL = '<?php 
    echo TAGS_TABLE_ID_COL;
    ?>
';
		var TAGS_TABLE_NAME_COL = '<?php 
    echo TAGS_TABLE_NAME_COL;
    ?>
';
		var TAGS_TABLE_COLOUR_COL = '<?php 
    echo TAGS_TABLE_COLOUR_COL;
    ?>
';
		var availableTags = <?php 
    echo json_encode($tags);
    ?>
;
		
		var $selectSelectedTags = $('select[name="<?php 
    echo $select_name;
    ?>
"]');
		var $selectAddTags = $('select[name="add-tags-select"]');
		var $tagsView = $('div.tags-view');
		
		var addTag = function (id) {
			if (typeof availableTags[id] === 'undefined')
				return;
			
			var name = availableTags[id][TAGS_TABLE_NAME_COL];
			var colour = availableTags[id][TAGS_TABLE_COLOUR_COL];
			
			var $tagName = $('<span></span>');
			$tagName.html(name);
			var $tagBtn = $('<a></a>');
			var $tag = $('<span></span>');
			$tag.append($tagName, $tagBtn)
				.prop('id', 'tag-'+id)
				.addClass('tag')
				.addClass('label')
				.addClass(colour)
				.data('id', id)
				.data('name', name)
				.data('colour', colour);
			$tagsView.append($tag);
			
			// Remove the label from the 'add select'
			$selectAddTags
				.children('option[value="' + id + '"]')
					.remove();
			
			// Select the item of the tags select
			$selectSelectedTags
				.children('option[value="' + id + '"]')
					.prop('selected', true);
		}
		
		var removeTag = function (id) {
			if (typeof availableTags[id] === 'undefined')
				return;
			
			var name = availableTags[id][TAGS_TABLE_NAME_COL];
			var colour = availableTags[id][TAGS_TABLE_COLOUR_COL];
			
			// Add the deleted item to the 'add select'
			var $option = $('<option></option>');
			$option
				.val(id)
				.html(name);
			$selectAddTags.append($option).val(0).change();
			
			// Unselect the item of the tags select
			$selectSelectedTags
				.children('option[value="' + id + '"]')
					.prop('selected', false);
			
			// Remove the tag
			$('span#tag-'+id).remove();
		}
		
		// Handler to add a new label with the 'add select'
		$selectAddTags.change(function (event) {
			event.preventDefault();
			
			// Retrieve the label info from the 'add select'
			var id = this.value;
			
			if (id != 0) {
				// Add the tag
				addTag(id);
			}
		});
		
		// Handler to delete a label selection
		$tagsView.on('click', 'span.tag>a', function (event) {
			event.preventDefault();
			
			if (typeof event.target !== 'undefined') {
				// Get the label info from the target element
				var id = $(event.target).parent().data('id');
				
				// Remove the tag
				removeTag(id);
			}
		});
		
		// Fill the tags view
		$selectSelectedTags
			.children('option:selected')
				.each(function(index, el) {
					addTag(el.value);
				});
		
	})(window.jQuery);
	</script>
<?php 
    $html = ob_get_clean();
    if ($return) {
        return $html;
    }
    echo $html;
}
Example #2
0
/**
 * Prints an array of fields in a popup menu of a form based on a SQL query.
 * The first and second columns of the query will be used.
 * 
 * The element will have an id like: "password-$value". Based on choose_from_menu() from Moodle.
 * 
 * @param string $sql SQL sentence, the first field will be the identifier of the option. 
 * The second field will be the shown value in the dropdown.
 * @param string $name Select form name
 * @param string $selected Current selected value.
 * @param string $script Javascript onChange code.
 * @param string $nothing Label when nothing is selected.
 * @param string $nothing_value Value when nothing is selected
 * @param bool $return Whether to return an output string or echo now (optional, echo by default).
 * @param bool $multiple Whether to allow multiple selections or not. Single by default
 * @param bool $sort Whether to sort the options or not. Sorted by default.
 * @param bool $disabled if it's true, disable the select.
 * @param string $style The string of style.
 * @param mixed $size Max elements showed in select or default (size=10) 
 * @param int $truncante_size Truncate size of the element, by default is set to GENERIC_SIZE_TEXT constant
 *
 * @return string HTML code if return parameter is true.
 */
function html_print_select_from_sql($sql, $name, $selected = '', $script = '', $nothing = '', $nothing_value = '0', $return = false, $multiple = false, $sort = true, $disabled = false, $style = false, $size = false, $trucate_size = GENERIC_SIZE_TEXT)
{
    global $config;
    $fields = array();
    $result = get_db_all_rows_sql($sql);
    if ($result === false) {
        $result = array();
    }
    foreach ($result as $row) {
        $id = array_shift($row);
        $value = array_shift($row);
        $fields[$id] = ui_print_truncate_text($value, $trucate_size, false, true, false);
    }
    return html_print_select($fields, $name, $selected, $script, $nothing, $nothing_value, $return, $multiple, $sort, '', $disabled, $style, '', $size);
}
Example #3
0
 $table->rowspan = array();
 $table->data = array();
 $table->colspan[0][0] = 2;
 $table->data[0][0] = print_input_text('entry_title', $entry['title'], '', 40, 100, true, __('Title'));
 $table->data[0][2] = print_checkbox('entry_public', $entry['public'], $entry['public'], true, __('Public'));
 if (!$entry['duration']) {
     $entry['duration'] = 0;
 }
 $table->data[1][0] = print_input_text('entry_duration', $entry['duration'], '', 6, 6, true, __('Duration in hours'));
 $alarms = array();
 $alarms[60] = __('One hour');
 $alarms[120] = __('Two hours');
 $alarms[240] = __('Four hours');
 $alarms[1440] = __('One day');
 $table->data[1][1] = print_label(__('Alarm'), 'entry_alarm', 'select', true);
 $table->data[1][1] .= html_print_select($alarms, 'entry_alarm', $entry['alarm'], '', __('None'), 0, true, false, false);
 $table->rowspan[1][2] = 2;
 $table->data[1][2] = html_print_entry_visibility_groups($config['id_user'], $entry['groups'], true);
 if (!$entry['timestamp']) {
     if (!$date) {
         $date = date('Y-m-d');
     }
     $time = date('H:i');
 } else {
     if (!$date) {
         $date = date('Y-m-d', $entry['timestamp']);
     }
     $result = explode(" ", $entry['timestamp']);
     $time = $result[1];
 }
 $table->data[2][0] = print_input_text('entry_date', $date, '', 10, 20, true, __('Date'));