Changeset 11361
- Timestamp:
- 07/26/07 21:37:16 (1 year ago)
- Files:
-
- trunk/midcom/midcom.helper.datamanager2/exec/tags_handler.php (modified) (4 diffs)
- trunk/midcom/midcom.helper.datamanager2/static/ajax-loading-small.gif (added)
- trunk/midcom/midcom.helper.datamanager2/static/tags/jquery.tags_widget.css (modified) (4 diffs)
- trunk/midcom/midcom.helper.datamanager2/static/tags/jquery.tags_widget.js (modified) (8 diffs)
- trunk/midcom/midcom.helper.datamanager2/type/tagselect.php (added)
- trunk/midcom/midcom.helper.datamanager2/widget/tags.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/midcom/midcom.helper.datamanager2/exec/tags_handler.php
r11353 r11361 10 10 */ 11 11 12 debug_print_r('_REQUEST', $_REQUEST);12 //debug_print_r('_REQUEST', $_REQUEST); 13 13 14 14 // Common variables 15 15 $encoding = 'UTF-8'; 16 $items = array(); 17 $mode = 'callback'; 18 $_callback = null; 19 $callback_args = array(); 16 20 17 21 // Common headers … … 36 40 $query = $_REQUEST["query"]; 37 41 38 /*39 <results>40 <result>41 <id>home</id>42 <name>Name</name>43 <color>#4c4c4c</color>44 </result>45 </results>46 */47 48 42 // // Convert tradiotional wildcard to SQL wildcard 49 43 // $query = str_replace('*', '%', $_REQUEST['query']); … … 51 45 // $query = preg_replace('/%+/', '%', $query); 52 46 53 // $items = array( 54 // 'home' => array( 'name' => 'Home', 'color' => '#000000' ), 55 // 'family' => array( 'name' => 'Family', 'color' => '#4c4c4c' ) 56 // ); 57 58 $items = array( 59 array( 'id' => 'home', 'name' => 'Home', 'color' => '#628ce4' ), 60 array( 'id' => 'family', 'name' => 'Family', 'color' => '#628ce4' ) 61 ); 62 63 // $res = ""; 64 // foreach ($items as $key => $data) { 65 // if (strpos(strtolower($key), $q) !== false) { 66 // $res .= "$key|"; 67 // $i = 1; 68 // $data_key_count = count($data); 69 // foreach ($data as $key => $value) 70 // { 71 // $res .= "$value"; 72 // if ($i < $data_key_count) 73 // { 74 // $res .= "|"; 75 // } 76 // $i++; 77 // } 78 // $res .= "\n"; 79 // } 80 // } 81 // debug_add("Found results: {$res}"); 47 // Get local copies of other variables from request 48 $map = array('component', 'class', 'object_id', 'id_field', 'callback', 'callback_args'); 49 foreach ($map as $varname) 50 { 51 if ( isset($_REQUEST[$varname]) 52 && !empty($_REQUEST[$varname])) 53 { 54 $$varname = $_REQUEST[$varname]; 55 if ($varname == 'callback_args') 56 { 57 $callback_args = json_decode($callback_args); 58 } 59 } 60 else 61 { 62 $$varname = false; 63 } 64 } 65 66 if ( !empty($component) 67 && !empty($class) 68 && !empty($object_id) 69 && !empty($id_field)) 70 { 71 $mode = 'object'; 72 } 73 74 if ($mode == 'object') 75 { 76 $_MIDCOM->load_library('net.nemein.tag'); 77 78 // Load component if required 79 if (!class_exists($class)) 80 { 81 $_MIDCOM->componentloader->load_graceful($component); 82 } 83 // Could not get required class defined, abort 84 if (!class_exists($class)) 85 { 86 echo " <status>0</status>\n"; 87 echo " <errstr>Class {$class} could not be loaded</errstr>\n"; 88 echo "</response>\n"; 89 $_MIDCOM->finish(); 90 exit(); 91 } 92 93 $qb = call_user_func(array($class, 'new_query_builder')); 94 $qb->add_constraint($id_field, '=', $object_id); 95 $results = $qb->execute(); 96 97 if ($results === false) 98 { 99 echo " <status>0</status>\n"; 100 echo " <errstr>Error when executing QB</errstr>\n"; 101 echo "</response>\n"; 102 $_MIDCOM->finish(); 103 exit(); 104 } 105 106 $object = $result[0]; 107 108 $tags = net_nemein_tag_handler::get_object_tags($object); 109 110 foreach($tags as $name => $link) 111 { 112 $data = array( 113 'id' => $name, 114 'name' => $name, 115 'color' => '8596b6' 116 ); 117 $items[$name] = $data; 118 } 119 } 120 else 121 { 122 if (! class_exists($callback)) 123 { 124 // Try auto-load. 125 $path = MIDCOM_ROOT . '/' . str_replace('_', '/', $callback) . '.php'; 126 if (! file_exists($path)) 127 { 128 debug_push_class(__CLASS__, __FUNCTION__); 129 debug_add("Auto-loading of the callback class {$callback} from {$path} failed: File does not exist.", MIDCOM_LOG_ERROR); 130 debug_pop(); 131 return false; 132 } 133 require_once($path); 134 } 135 136 if (! class_exists($callback)) 137 { 138 debug_push_class(__CLASS__, __FUNCTION__); 139 debug_add("The callback class {$callback} was defined as option for the field {$this->name} but did not exist.", MIDCOM_LOG_ERROR); 140 debug_pop(); 141 return false; 142 } 143 $_callback = new $callback($callback_args); 144 145 $all_items = $_callback->list_all(); 146 foreach ($all_items as $id => $data) 147 { 148 $items[$id] = $data; 149 } 150 151 } 82 152 83 153 $results = array(); 84 154 $added_keys = array(); 85 foreach ($items as $i => $item)155 foreach ($items as $id => $item) 86 156 { 87 157 foreach ($item as $key => $value) 88 158 { 89 if ( strpos(strtolower($key), $query) !== false 90 || strpos(strtolower($value), $query) !== false) 159 if (strpos(strtolower($value), $query) !== false) 91 160 { 92 if (! array_key_exists($i , $added_keys))161 if (! array_key_exists($id, $added_keys)) 93 162 { 94 163 $results[] = $item; 95 $added_keys[$i ] = true;164 $added_keys[$id] = true; 96 165 } 97 } 166 } 98 167 } 99 168 } … … 124 193 echo "</response>\n"; 125 194 126 // echo json_encode($results); 127 debug_print_r('Found results',$results); 195 //debug_print_r('Got results',$results); 128 196 129 197 debug_pop(); trunk/midcom/midcom.helper.datamanager2/static/tags/jquery.tags_widget.css
r11353 r11361 51 51 .tags_widget_selections 52 52 { 53 width: 50%; 53 54 background-color: #fff; 55 border: 1px solid #000; 54 56 } 55 57 … … 57 59 { 58 60 height: 20px; 59 border: 1px solid WindowFrame;60 61 list-style-position: outside; 61 62 list-style-type: none; … … 67 68 { 68 69 height: 16px; 69 margin: 1px 0 0 0;70 margin: 2px 0 0 0; 70 71 padding: 0 3px 0 3px; 71 72 margin-right: 3px; 72 73 -moz-border-radius: 3px; 73 74 float: left; 74 color: HighlightText;75 color: #ffffff; 75 76 font-size: 0.9em; 76 77 } … … 84 85 .tags_widget_selection_item_deleted 85 86 { 86 background-color: white;87 87 color: black; 88 88 border: 1px solid red; trunk/midcom/midcom.helper.datamanager2/static/tags/jquery.tags_widget.js
r11353 r11361 36 36 }, 37 37 midcom_helper_datamanager2_widget_tags_add_selection_item: function(item) { 38 return this.trigger("add_selection_item", item);38 return this.trigger("add_selection_item",[item]); 39 39 }, 40 40 midcom_helper_datamanager2_widget_tags_remove_selection_item: function(item_id) { 41 return this.trigger("remove_selection_item", item_id);41 return this.trigger("remove_selection_item",[item_id]); 42 42 } 43 43 }); … … 104 104 break; 105 105 } 106 }).keypress(function( ) {106 }).keypress(function(event) { 107 107 // having fun with opera - remove this binding and Opera submits the form when we select an entry via return 108 event.preventDefault(); 108 switch(event.keyCode) { 109 case KEY.TAB: 110 case KEY:RETURN: 111 event.preventDefault(); 112 break; 113 } 109 114 }).focus(function(){ 110 115 // track whether the field has focus, we shouldn't process any … … 416 421 .hide() 417 422 .addClass('tags_widget_selections'); 418 jQuery(input). after( element );423 jQuery(input).before( element ); 419 424 420 425 var list = jQuery("<ul>").appendTo(element); 421 426 422 var list_items ,427 var list_items = [], 423 428 has_content = false; 424 429 … … 432 437 } 433 438 434 function can_add( )439 function can_add(item_id) 435 440 { 436 441 if (options.selection_limit > 0) … … 442 447 } 443 448 449 var existing = false; 450 existing = jQuery.grep( list_items, function(n,i){ 451 return n == item_id; 452 }); 453 454 if (existing == item_id) 455 { 456 jQuery('#tag_'+item_id,list).Highlight(800, 'yellow'); 457 return false; 458 } 459 444 460 return true; 445 461 } … … 452 468 console.log('data.color: '+data.color); 453 469 454 if (! can_add( ))470 if (! can_add(data.id)) 455 471 { 456 472 return false; … … 463 479 } 464 480 465 var input_elem_id = options.widget_type_name + "_tag[]";// + data.id + "]"; 466 481 var input_elem_name = options.widget_type_name + "_tags[" + data.id + "]"; 482 483 data.color = String(data.color).replace("#",""); 484 467 485 var li_elem = jQuery("<li>") 468 486 .attr({ id: 'tag_'+data.id }) 469 .css({background: data.color})487 .css({background: '#'+data.color}) 470 488 .mouseover( function(event) { 471 489 active = jQuery("li", list).removeClass(CLASSES.HOVER).index(target(event)); … … 492 510 .appendTo(li_elem); 493 511 var input_elem = jQuery("<input>") 494 .attr({ type: ' checkbox', name: input_elem_id, value:data.id })512 .attr({ type: 'hidden', name: input_elem_name, value: 1, id: 'tags-widget_tag_'+data.id }) 495 513 .hide() 496 514 .appendTo(li_elem); 497 515 498 516 li_elem.appendTo(list); 517 518 list_items.push(data.id); 499 519 } 500 520 trunk/midcom/midcom.helper.datamanager2/widget/tags.php
r11359 r11361 11 11 * Datamanager 2 Tags widget 12 12 * 13 * As with all subclasses, the actual initialization is done in the initialize() func tion,13 * As with all subclasses, the actual initialization is done in the initialize() funciton, 14 14 * not in the constructor, to allow for error handling. 15 15 * 16 * It can only be bound to a tagselect type (or subclass thereof ), and inherits the configuration16 * It can only be bound to a tagselect type (or subclass thereoff), and inherits the configuration 17 17 * from there as far as possible. 18 18 * … … 22 22 * - <i>integer result_limit:</i> Number max Limit the number of items in the select box. 23 23 * Is also sent as a "limit" parameter with a remote request. Default: 10 24 * 25 * Component 26 * Class 27 * 28 * Example: 29 'tags' => Array 30 ( 31 'title' => 'event tags', 32 'storage' => 'null', 33 'type' => 'tagselect', 34 'type_config' => array 35 ( 36 'option_callback' => 'org_maemo_calendar_callbacks_personstags', 37 'enable_saving_to_callback' => false, 38 'force_saving_to_tag_library' => true, 39 ), 40 'widget' => 'tags', 41 ), 24 42 * 25 43 * @package midcom.helper.datamanager2 … … 54 72 debug_push_class(__CLASS__, __FUNCTION__); 55 73 56 //if (is_a('midcom_helper_datamanager2_type_tagselect', $this->_type))57 //{58 // debug_add("Warning, the field {$this->name} is not a tagselect type or subclass thereof, you cannot use the tags widget with it.",59 //MIDCOM_LOG_WARN);60 //debug_pop();61 //return false;62 //}74 if (is_a('midcom_helper_datamanager2_type_tagselect', $this->_type)) 75 { 76 debug_add("Warning, the field {$this->name} is not a tagselect type or subclass thereoff, you cannot use the tags widget with it.", 77 MIDCOM_LOG_WARN); 78 debug_pop(); 79 return false; 80 } 63 81 64 82 $_MIDCOM->enable_jquery(); … … 113 131 $this->_js_widget_options['match_inner'] = "false"; 114 132 } 115 } 133 } 134 135 $this->_generate_extra_params(); 136 116 137 debug_pop(); 117 138 return true; 118 139 } 119 140 141 function _generate_extra_params() 142 { 143 $map = array('component', 'class', 'object_id', 'id_field', 'callback', 'callback_args'); 144 145 $params = "{"; 146 147 if ( isset($this->component) 148 && isset($this->class) 149 && isset($this->object_id) 150 && isset($this->id_field)) 151 { 152 $params .= "component: '{$this->component}',"; 153 $params .= "class: '{$this->class}',"; 154 $params .= "object_id: '{$this->object_id}',"; 155 $params .= "id_field: '{$this->id_field}'"; 156 } 157 else if (isset($this->_type->option_callback)) 158 { 159 $params .= "callback: '{$this->_type->option_callback}'"; 160 if ( isset($this->_type->option_callback_args) 161 && is_array($this->_type->option_callback_args)) 162 { 163 $params .= ", callback_args: ".json_encode($this->_type->option_callback_args); 164 } 165 } 166 167 $params .= "}"; 168 169 $this->_js_widget_options['extra_params'] = $params; 170 } 171 120 172 function _get_key_data($key) 121 173 { 122 174 debug_push_class(__CLASS__, __FUNCTION__); 175 176 $data = $this->_type->get_data_for_key($key); 177 123 178 $value = "{"; 124 179 125 180 $name = "Test"; 126 $color = " 4c4c4c";181 $color = "#628ce4"; 127 182 128 183 $value .= "id: '{$key}',"; 129 $value .= "name: '{$ name}',";130 $value .= "color: '{$ color}',";184 $value .= "name: '{$data['name']}',"; 185 $value .= "color: '{$data['color']}'"; 131 186 132 187 $value .= "}"; … … 142 197 { 143 198 debug_push_class(__CLASS__, __FUNCTION__); 144 145 // $selections_html = "<div class='tags-widget-selections'></div>";146 // $results_html = "<ul class='tags-widget-results'></ul>";147 199 148 200 $attributes = Array … … 152 204 ); 153 205 154 $this->_form->addElement('text', "{$this->name}", $this->_translate($this->_field['title']), $attributes); 155 // $this->_form->addElement('static', $this->name.'_selections', '', $selections_html); 156 // $this->_form->addElement('static', $this->name.'_results', '', $results_html); 157 158 //$this->_form->applyFilter($this->name, 'trim'); 206 // $elements = array(); 207 // $elements[] = & HTML_QuickForm::createElement('hidden', "default", '1', array()); 208 // 209 // $group =& $this->_form->addGroup($elements, $this->name, $this->_translate($this->_field['title']), "<br />"); 210 211 $this->_form->addElement('text', "{$this->name}_input", $this->_translate($this->_field['title']), $attributes); 159 212 160 213 // Get url to search handler … … 176 229 // Add existing selection 177 230 $existing_elements = $this->_type->selection; 231 debug_print_r('existing_elements',$existing_elements); 178 232 $ee_script = ''; 179 233 foreach ($existing_elements as $key) … … 194 248 function get_default() 195 249 { 250 debug_push_class(__CLASS__, __FUNCTION__); 251 252 //debug_print_r('this->_type',$this->_type); 253 196 254 $defaults = Array(); 197 255 foreach ($this->_type->selection as $key) … … 199 257 $defaults[$key] = true; 200 258 } 259 260 debug_print_r('defaults',$defaults); 261 262 debug_pop(); 201 263 return Array($this->name => $defaults); 202 264 } … … 212 274 213 275 $this->_type->selection = Array(); 214 if (!isset($results["{$this->name}_tag "]))276 if (!isset($results["{$this->name}_tags"])) 215 277 { 216 278 return; 217 279 } 218 $real_results =& $results["{$this->name}_tag "];280 $real_results =& $results["{$this->name}_tags"]; 219 281 220 282 foreach ($real_results as $key => $value) … … 231 293 { 232 294 } 233 234 /**235 * Unfreezes all form elements associated with the widget. The default implementation236 * works on the default field name, you don't need to override this function unless237 * you have multiple widgets in the form.238 *239 * This maps to the HTML_QuickForm_element::unfreeze()unction.240 */241 295 function unfreeze() 242 296 { 297 } 298 function is_frozen() 299 { 300 return false; 243 301 } 244 302 … … 254 312 foreach ($this->_type->selection as $key) 255 313 { 256 echo '<li>' . $this->_get_key_value($key) . '</li>'; 314 $data = $this->_get_key_data($key); 315 echo '<li>' . $data['name'] . '</li>'; 257 316 } 258 317 }
