Changeset 11361

Show
Ignore:
Timestamp:
07/26/07 21:37:16 (1 year ago)
Author:
w_i
Message:

Added tag-selection widget (Some features are still untested)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.helper.datamanager2/exec/tags_handler.php

    r11353 r11361  
    1010 */ 
    1111 
    12 debug_print_r('_REQUEST',  $_REQUEST); 
     12//debug_print_r('_REQUEST',  $_REQUEST); 
    1313 
    1414// Common variables 
    1515$encoding = 'UTF-8'; 
     16$items = array(); 
     17$mode = 'callback'; 
     18$_callback = null; 
     19$callback_args = array(); 
    1620 
    1721// Common headers 
     
    3640$query = $_REQUEST["query"]; 
    3741 
    38 /* 
    39 <results> 
    40  <result> 
    41   <id>home</id> 
    42   <name>Name</name> 
    43   <color>#4c4c4c</color> 
    44  </result> 
    45 </results> 
    46 */ 
    47  
    4842// // Convert tradiotional wildcard to SQL wildcard 
    4943// $query = str_replace('*', '%', $_REQUEST['query']); 
     
    5145// $query = preg_replace('/%+/', '%', $query); 
    5246 
    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'); 
     49foreach ($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 
     66if (   !empty($component) 
     67    && !empty($class) 
     68    && !empty($object_id) 
     69    && !empty($id_field)) 
     70
     71    $mode = 'object'; 
     72
     73 
     74if ($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
     120else 
     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
    82152 
    83153$results = array(); 
    84154$added_keys = array(); 
    85 foreach ($items as $i => $item) 
     155foreach ($items as $id => $item) 
    86156{ 
    87157    foreach ($item as $key => $value) 
    88158    { 
    89         if (   strpos(strtolower($key), $query) !== false 
    90             || strpos(strtolower($value), $query) !== false) 
     159        if (strpos(strtolower($value), $query) !== false) 
    91160        { 
    92             if (! array_key_exists($i, $added_keys)) 
     161            if (! array_key_exists($id, $added_keys)) 
    93162            { 
    94163                $results[] = $item; 
    95                 $added_keys[$i] = true; 
     164                $added_keys[$id] = true; 
    96165            } 
    97         }         
     166        } 
    98167    } 
    99168} 
     
    124193echo "</response>\n"; 
    125194 
    126 // echo json_encode($results); 
    127 debug_print_r('Found results',$results); 
     195//debug_print_r('Got results',$results); 
    128196 
    129197debug_pop(); 
  • trunk/midcom/midcom.helper.datamanager2/static/tags/jquery.tags_widget.css

    r11353 r11361  
    5151.tags_widget_selections 
    5252{ 
     53    width: 50%; 
    5354    background-color: #fff; 
     55    border: 1px solid #000; 
    5456} 
    5557 
     
    5759{ 
    5860    height: 20px; 
    59     border: 1px solid WindowFrame; 
    6061    list-style-position: outside; 
    6162    list-style-type: none; 
     
    6768{ 
    6869    height: 16px; 
    69     margin: 1px 0 0 0; 
     70    margin: 2px 0 0 0; 
    7071    padding: 0 3px 0 3px; 
    7172    margin-right: 3px; 
    7273    -moz-border-radius: 3px; 
    7374    float: left; 
    74     color: HighlightText
     75    color: #ffffff
    7576    font-size: 0.9em; 
    7677} 
     
    8485.tags_widget_selection_item_deleted 
    8586{ 
    86         background-color: white; 
    8787        color: black; 
    8888        border: 1px solid red; 
  • trunk/midcom/midcom.helper.datamanager2/static/tags/jquery.tags_widget.js

    r11353 r11361  
    3636        }, 
    3737        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]); 
    3939        }, 
    4040        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]); 
    4242        }        
    4343}); 
     
    104104                                break; 
    105105                } 
    106         }).keypress(function() { 
     106        }).keypress(function(event) { 
    107107                // 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            } 
    109114        }).focus(function(){ 
    110115                // track whether the field has focus, we shouldn't process any 
     
    416421                .hide() 
    417422                .addClass('tags_widget_selections'); 
    418         jQuery(input).after( element ); 
     423        jQuery(input).before( element ); 
    419424 
    420425        var list = jQuery("<ul>").appendTo(element); 
    421426 
    422     var list_items
     427    var list_items = []
    423428        has_content = false; 
    424429 
     
    432437        } 
    433438         
    434         function can_add(
     439        function can_add(item_id
    435440        { 
    436441            if (options.selection_limit > 0) 
     
    442447            } 
    443448             
     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             
    444460            return true; 
    445461        } 
     
    452468            console.log('data.color: '+data.color); 
    453469                             
    454         if (! can_add()) 
     470        if (! can_add(data.id)) 
    455471        { 
    456472            return false; 
     
    463479        } 
    464480             
    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         
    467485            var li_elem = jQuery("<li>") 
    468486            .attr({ id: 'tag_'+data.id }) 
    469             .css({background: data.color}) 
     487            .css({background: '#'+data.color}) 
    470488        .mouseover( function(event) { 
    471489                active = jQuery("li", list).removeClass(CLASSES.HOVER).index(target(event)); 
     
    492510            .appendTo(li_elem); 
    493511            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 }) 
    495513            .hide() 
    496514            .appendTo(li_elem); 
    497515             
    498516            li_elem.appendTo(list); 
     517             
     518            list_items.push(data.id); 
    499519        } 
    500520         
  • trunk/midcom/midcom.helper.datamanager2/widget/tags.php

    r11359 r11361  
    1111 * Datamanager 2 Tags widget 
    1212 * 
    13  * As with all subclasses, the actual initialization is done in the initialize() function, 
     13 * As with all subclasses, the actual initialization is done in the initialize() funciton, 
    1414 * not in the constructor, to allow for error handling. 
    1515 * 
    16  * It can only be bound to a tagselect type (or subclass thereof), and inherits the configuration 
     16 * It can only be bound to a tagselect type (or subclass thereoff), and inherits the configuration 
    1717 * from there as far as possible. 
    1818 * 
     
    2222 * - <i>integer result_limit:</i> Number max Limit the number of items in the select box. 
    2323 * 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 ), 
    2442 * 
    2543 * @package midcom.helper.datamanager2 
     
    5472        debug_push_class(__CLASS__, __FUNCTION__); 
    5573         
    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       
    6381         
    6482        $_MIDCOM->enable_jquery(); 
     
    113131                $this->_js_widget_options['match_inner'] = "false"; 
    114132            } 
    115         }  
     133        } 
     134         
     135        $this->_generate_extra_params(); 
     136         
    116137        debug_pop(); 
    117138        return true; 
    118139    } 
    119140     
     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     
    120172    function _get_key_data($key) 
    121173    { 
    122174        debug_push_class(__CLASS__, __FUNCTION__); 
     175         
     176        $data = $this->_type->get_data_for_key($key); 
     177         
    123178        $value = "{"; 
    124179         
    125180        $name = "Test"; 
    126         $color = "4c4c4c"; 
     181        $color = "#628ce4"; 
    127182         
    128183        $value .= "id: '{$key}',"; 
    129         $value .= "name: '{$name}',"; 
    130         $value .= "color: '{$color}',"; 
     184        $value .= "name: '{$data['name']}',"; 
     185        $value .= "color: '{$data['color']}'"; 
    131186                 
    132187        $value .= "}"; 
     
    142197    { 
    143198        debug_push_class(__CLASS__, __FUNCTION__); 
    144          
    145         // $selections_html = "<div class='tags-widget-selections'></div>"; 
    146         // $results_html = "<ul class='tags-widget-results'></ul>"; 
    147199 
    148200        $attributes = Array 
     
    152204        ); 
    153205 
    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); 
    159212 
    160213        // Get url to search handler 
     
    176229        // Add existing selection 
    177230        $existing_elements = $this->_type->selection; 
     231        debug_print_r('existing_elements',$existing_elements); 
    178232        $ee_script = ''; 
    179233        foreach ($existing_elements as $key) 
     
    194248     function get_default() 
    195249     { 
     250         debug_push_class(__CLASS__, __FUNCTION__); 
     251          
     252         //debug_print_r('this->_type',$this->_type); 
     253          
    196254         $defaults = Array(); 
    197255         foreach ($this->_type->selection as $key) 
     
    199257             $defaults[$key] = true; 
    200258         } 
     259          
     260         debug_print_r('defaults',$defaults); 
     261          
     262         debug_pop(); 
    201263         return Array($this->name => $defaults); 
    202264     } 
     
    212274         
    213275        $this->_type->selection = Array(); 
    214         if (!isset($results["{$this->name}_tag"])) 
     276        if (!isset($results["{$this->name}_tags"])) 
    215277        { 
    216278            return; 
    217279        } 
    218         $real_results =& $results["{$this->name}_tag"]; 
     280        $real_results =& $results["{$this->name}_tags"]; 
    219281         
    220282        foreach ($real_results as $key => $value) 
     
    231293    { 
    232294    } 
    233  
    234     /** 
    235      * Unfreezes all form elements associated with the widget. The default implementation 
    236      * works on the default field name, you don't need to override this function unless 
    237      * you have multiple widgets in the form. 
    238      * 
    239      * This maps to the HTML_QuickForm_element::unfreeze()unction. 
    240      */ 
    241295    function unfreeze() 
    242296    { 
     297    } 
     298    function is_frozen() 
     299    { 
     300        return false; 
    243301    } 
    244302     
     
    254312            foreach ($this->_type->selection as $key) 
    255313            { 
    256                 echo '<li>' . $this->_get_key_value($key) . '</li>'; 
     314                $data = $this->_get_key_data($key); 
     315                echo '<li>' . $data['name'] . '</li>'; 
    257316            } 
    258317        }