Exemple #1
0
/**
 * Returns the number of available tags for the connected user.
 *
 * @return int
 */
function get_nb_available_tags()
{
    global $user;
    if (!isset($user['nb_available_tags'])) {
        $user['nb_available_tags'] = count(get_available_tags());
        single_update(USER_CACHE_TABLE, array('nb_available_tags' => $user['nb_available_tags']), array('user_id' => $user['id']));
    }
    return $user['nb_available_tags'];
}
Exemple #2
0
/**
 * API method
 * Returns a list of tags
 * @param mixed[] $params
 *    @option bool sort_by_counter
 */
function ws_tags_getList($params, &$service)
{
    $tags = get_available_tags();
    if ($params['sort_by_counter']) {
        usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];'));
    } else {
        usort($tags, 'tag_alpha_compare');
    }
    for ($i = 0; $i < count($tags); $i++) {
        $tags[$i]['id'] = (int) $tags[$i]['id'];
        $tags[$i]['counter'] = (int) $tags[$i]['counter'];
        $tags[$i]['url'] = make_index_url(array('section' => 'tags', 'tags' => array($tags[$i])));
    }
    return array('tags' => new PwgNamedArray($tags, 'tag', ws_std_get_tag_xml_attributes()));
}
Exemple #3
0
$row[6] = print_input_hidden('id', $id, true);
if (empty($id)) {
    $row[6] .= print_input_hidden('action', 'create', true);
} else {
    $row[6] .= print_input_hidden('action', 'update', true);
}
$row[6] .= print_submit_button(__('Add'), 'create_btn', false, 'class="sub create"', true);
$row[6] .= print_submit_button(__('Update'), 'update_btn', false, 'class="sub upd"', true);
$row[6] .= '&nbsp;';
$row[6] .= print_submit_button(__('Delete'), 'delete_btn', false, 'class="sub delete"', true);
$table->data[] = $row;
echo '<form id="tags-form" method="POST">';
print_table($table);
echo '</form>';
// List
$tags = get_available_tags();
html_render_tags_view($tags);
?>

<script type="text/javascript">
(function ($) {
	var $idHidden = $('input#hidden-id');
	var $nameInput = $('input#text-name');
	var $colourInput = $('select#colour');
	var $tagSpan = $('span#tag-preview');
	
	var $form = $('form#tags-form');
	var $actionHidden = $('input#hidden-action');
	var $createSubmit = $('input#submit-create_btn');
	var $updateSubmit = $('input#submit-update_btn');
	var $deleteSubmit = $('input#submit-delete_btn');
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;
}