Changeset 265

Show
Ignore:
Timestamp:
03/09/08 08:57:37 (9 months ago)
Author:
kevin
Message:

Added generic keyword support and profile_radio_group display category.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • prfext/README.txt

    r221 r265  
    3333 
    3434Four profile fields work immediately without further configuration. 
     35 
     36*Keyword fields* 
     37         
     38When using any fields with prfext field types, you can use the "keyword_tag"  
     39parameter to make this field act like Elgg keywords - they are searchable  
     40and linked in the profile display. 
    3541 
    3642*Dates* 
     
    6066        "required" => false, 
    6167        "user_type" => "person", 
     68        "keyword_tag" => false 
    6269        )); 
    6370         
     
    6673Regardless of the date format displayed, the date values are always 
    6774stored in the database in the ISO standard YYYY-MM-DD. 
     75 
     76Notice in this case that keyword_tag is set to false. In this case 
     77this field will not be searchable. You can also leave "keyword_tag" 
     78off altogether unless you want it to be true. 
    6879 
    6980*Smaller fields* 
     
    93104will encourage people to enter only a few words for their minibio. 
    94105 
     106$data['profile:details'][] = (object)(array( 
     107        "name" => __gettext("Town"), 
     108        "internal_name" => "town", 
     109        "field_type" => "profile_shorttext", 
     110        "description" => "", 
     111        "category" => __gettext("Location"), 
     112        "col1" => true, 
     113        "invisible" => false, 
     114        "required" => false, 
     115        "user_type" => "", 
     116        "keyword_tag" => true, 
     117        )); 
     118         
     119provides a small amount of room for a town name. Notice that 
     120"keyword_tag" is set to true to make this field searchable. It 
     121will be linked in profiles if there are other people from 
     122the same town, and people will be able to search for these towns 
     123in the Elgg search box. 
     124 
    95125*Selects, radio buttons and checkbox groups* 
    96126 
    97 The profile extension plugin provides a simple function, prfext_display
     127The profile extension plugin provides a simple function, prfext_choices
    98128to register new display types with the Elgg profile system. You can use this 
    99129function to define selects, radio buttons and checkbox groups and then use 
    100130these new display types in your profile field descriptions. 
    101131 
    102 The definition of prext_display is: 
    103  
    104 prfext_display($your_field_display_type,$prfext_display_category,$is_keyword_tag,$default_value,$options) 
     132The definition of prfext_display is: 
     133 
     134prfext_choices($your_field_display_type,$prfext_display_category,$default_value,$options) 
    105135 
    106136The valid prfext display categories are: 
     
    109139profile_selectg 
    110140profile_radio 
     141profile_radio_group 
    111142profile_vertical_radio 
    112143profile_checkbox 
     
    115146You can find detailed examples explaining how to use these categories below. 
    116147 
    117 *New keyword display types* 
    118          
    119 The $is_keyword_tag parameter of prfext_display makes profile fields using  
    120 this display type act like Elgg keywords - they are searchable and linked 
    121 in the profile display. 
    122  
    123148*Simple select example* 
    124149 
    125150Here's a simple example: 
    126151 
    127 prfext_display('sports','profile_select',true,'',array('football','golf','darts','hockey','other sport')); 
    128  
    129 This creates an Elgg display type (a select box) called "sports". It will be treated 
    130 as a searchable keyword tag type and has no default value. 
     152prfext_choices('sports','profile_select','',array('football','golf','darts','hockey','other sport')); 
     153 
     154This creates an Elgg display type (a select box) called "sports". It has no default value. 
    131155 
    132156The values can be any phrase (including blanks) but must not include commas. The word 'null' 
     
    145169        "required" => false, 
    146170        "user_type" => "person", 
     171        "keyword_tag" => true, 
    147172        )); 
     173         
     174Notice the use of the "keyword_tag" value to make this field act like an 
     175Elgg keyword tag field. 
    148176         
    149177You can use the same display type multiple times - for example, you might want 
     
    156184For example: 
    157185 
    158 prfext_display('sports','profile_select',true,'hockey',array('football','golf','darts','hockey','other sport')); 
     186prfext_choices('sports','profile_select','hockey', 
     187    array('football','golf','darts','hockey','other sport')); 
    159188 
    160189sets hockey as the default (pre-selected) value. 
     
    169198There are two forms of radio buttons: 
    170199 
    171 prfext_display('sports','profile_radio',true,'',array('football','golf','darts','hockey','other sport')); 
     200prfext_choices('sports','profile_radio','', 
     201    array('football','golf','darts','hockey','other sport')); 
    172202 
    173203presents 5 horizontal radio buttons. 
    174204 
    175 prfext_display('sports','profile_vertical_radio',true,'',array('football','golf','darts','hockey','other sport')); 
     205prfext_choices('sports','profile_vertical_radio','', 
     206    array('football','golf','darts','hockey','other sport')); 
    176207 
    177208presents 5 vertical radio buttons, with one option displayed per line 
     
    184215For example, 
    185216 
    186 prfext_display('sports','profile_radio',false,'', 
     217prfext_choices('sports','profile_radio','', 
    187218    array( 
    188219        'gb'=>'United Kingdom', 
     
    197228In the profile, the appropriate label would be displayed instead of 
    198229the internal code. This works for any of the available prfext display 
    199 categories listed above
     230categories defined by prfext_choices
    200231 
    201232Display labels can be any Unicode string and can include commas, unlike 
    202233internal values. 
    203234 
    204 Note that it is the internal values that would be used as searchable tags, 
    205 and in this example, this profile field has $is_keyword__tag set 
    206 to false to avoid creating potentially obscure searchable tags like 'be'. 
    207  
    208235*Checkbox group example* 
    209236 
    210237Checkbox groups allow you to return more than one value. 
    211238 
    212 prfext_display('sports','profile_checkbox',true,'',array('football','golf','darts','hockey','other sport')); 
    213  
    214 presents a horizontal list of 5 checkboxes and users can choose any (or none) of them. 
     239prfext_choices('sports','profile_checkbox','', 
     240    array('football','golf','darts','hockey','other sport')); 
     241 
     242presents a horizontal list of 5 checkboxes and users can choose any  
     243(or none) of them. 
    215244 
    216245Similarly, 
    217246 
    218 prfext_display('sports','profile_vertical_checkbox',true,'',array('football','golf','darts','hockey','other sport')); 
     247prfext_choices('sports','profile_vertical_checkbox','', 
     248    array('football','golf','darts','hockey','other sport')); 
    219249 
    220250returns a vertical list of 5 checkboxes, one option per line. 
     
    225255For example, 
    226256 
    227 prfext_display('sports','profile_checkbox',true,'golf,darts,other sport',array('football','golf','darts','hockey','other sport')); 
    228  
    229 would pre-check the boxes for golf, darts and other, and leave football and 
    230 other sport unchecked. 
     257prfext_choices('sports','profile_checkbox','golf,darts,other sport', 
     258    array('football','golf','darts','hockey','other sport')); 
     259 
     260would pre-check the boxes for golf, darts and other, and leave  
     261football and other sport unchecked. 
     262 
     263*Radio groups* 
     264 
     265You can set up a two-dimensional radio button group using the  
     266profile_radio_group display category. See radioGroup.gif in this  
     267directory for an image. 
     268 
     269prfext_choices('activities','profile_radio_group','never,often,sometimes', 
     270    array(array('smoke','move','sleep'),array('often','sometimes','never'))); 
     271     
     272sets up the two dimensional array of choices shown in the image.  
     273The user can make zero or one choice for each line in the table. You  
     274can set a default value for each row using the usual default value  
     275parameter, with one value for each row (in this case, 'never,often,sometimes'). 
     276 
     277As with other examples, you can set labels using associative arrays. In this case: 
     278 
     279prfext_choices('activities','profile_radio_group','n,o,s', 
     280array( 
     281    array('sm'=>'Smoke','m'=>'Move','sl'=>'Sleep'), 
     282    array('o'=>'Often','s'=>'Sometimes','n'=>'Never') 
     283    ) 
     284); 
    231285 
    232286*Select with option groups* 
     
    246300Italy. 
    247301 
    248 prfext_display('countryregion','profile_selectg',false,'it', 
     302prfext_choices('countryregion','profile_selectg','it', 
    249303            array( 
    250304            'Northwestern Europe' =>  array( 
  • prfext/function_search.php

    r221 r265  
    3232        $field_type = $CFG->prfext_keyword_field_names[$parameter[0]]->field_type; 
    3333        $field_display_name = $CFG->prfext_keyword_field_names[$parameter[0]]->field_display_name; 
    34         $d = $CFG->prfext_data[$field_type]; 
    35         $options = $d->options; 
    36         $display_type = $d->display_type; 
    37         if ($display_type == 'profile_selectg') { 
    38             foreach ($options as $option) { 
    39                 if (prfext_array_is_associative($option)) { 
    40                     foreach($option as $k => $v) { 
    41                         if ($tag == $k) { 
    42                             $display_value = $v; 
    43                             break 2; 
     34        if (isset($CFG->prfext_data[$field_type])) { 
     35            $d = $CFG->prfext_data[$field_type]; 
     36            $options = $d->options; 
     37            $display_type = $d->display_type; 
     38            if ($display_type == 'profile_selectg') { 
     39                foreach ($options as $option) { 
     40                    if (prfext_array_is_associative($option)) { 
     41                        foreach($option as $k => $v) { 
     42                            if ($tag == $k) { 
     43                                $display_value = $v; 
     44                                break 2; 
     45                            } 
    4446                        } 
    4547                    } 
    4648                } 
     49            } else if ($display_type == 'profile_radio_group') { 
     50                $rows = $options[0]; 
     51                $cols = $options[1]; 
     52                if (prfext_array_is_associative($rows)) { 
     53                    $tp = explode(":",$tag); 
     54                    $display_value = $rows[$tp[0]] .': '. $cols[$tp[1]]; 
     55                } 
     56            } else if (prfext_array_is_associative($options)) { 
     57                $display_value = $options[$tag]; 
    4758            } 
    48         } else if (prfext_array_is_associative($options)) { 
    49             $display_value = $options[$tag]; 
    5059        } 
    5160 
  • prfext/lib.php

    r221 r265  
    2929profile_radio, 
    3030profile_vertical_radio, 
     31profile_radio_group, 
    3132profile_checkbox, 
    3233profile_vertical_checkbox, 
     
    6869    foreach($data['profile:details'] as $field) { 
    6970        $field_type = $field->field_type; 
    70         if (isset($CFG->prfext_data[$field_type]) && $CFG->prfext_data[$field_type]->is_keyword_tag) { 
     71        if ((isset($field->keyword_tag) && $field->keyword_tag) 
     72            || (isset($CFG->prfext_data[$field_type]) && isset($CFG->prfext_data[$field_type]->is_keyword_tag)  
     73                && $CFG->prfext_data[$field_type]->is_keyword_tag)) { 
    7174            $d = new stdClass; 
    7275            $d->field_type = $field_type; 
     
    8487                                    'display_type' =>  $display_type, 
    8588                                    'is_keyword_tag' => $is_keyword_tag, 
     89                                    'default_value' => $default_value, 
     90                                    'options' => $options 
     91                                    ); 
     92    $CFG->display_field_module[$name] = 'prfext'; 
     93     
     94} 
     95 
     96function prfext_choices($name,$display_type,$default_value,$options) { 
     97    global $CFG; 
     98     
     99    $CFG->prfext_data[$name] = (object) array( 
     100                                    'display_type' =>  $display_type, 
    86101                                    'default_value' => $default_value, 
    87102                                    'options' => $options 
     
    312327        $run_result .= "<textarea name=\"".$cleanid."\" id=\"".$cleanid."\" style=\"width: 250px; height: 100px\">".htmlspecialchars(stripslashes($default_value), ENT_COMPAT, 'utf-8')."</textarea>"; 
    313328      break; 
     329       
     330      case "profile_radio_group": 
     331          $r = rand(); 
     332          $form_name_prefix = 'g'.$r; 
     333          $rowsLen = count($options[0]); 
     334          $columnsLen = count($options[1]); 
     335     
     336          $run_result .= " 
     337              <script language=\"JavaScript\"> 
     338              function select_field$form_name_prefix(form){ 
     339                var result; 
     340                var contentFlag = false; 
     341                result = ''; 
     342                for(i=0;i<$rowsLen;i++) { 
     343                  result += ','; 
     344                  for(j=0;j<$columnsLen;j++) { 
     345                    if (form['$form_name_prefix'+'_'+i][j].checked == true){ 
     346                      result += form['$form_name_prefix'+'_'+i][j].value; 
     347                      contentFlag=true; 
     348                    } 
     349                  } 
     350                } 
     351     
     352                if (!contentFlag){ 
     353                  result = ''; 
     354                } 
     355     
     356                form['$cleanid'].value = result.substr(1); 
     357                if (form['$cleanid'].value == '') { 
     358                    form['$cleanid'].value = 'null'; 
     359                } 
     360              } 
     361              </script>"; 
     362 
     363          if ($default_value) { 
     364              $default_array_temp = explode(",",$default_value); 
     365              $default_array = array(); 
     366              foreach($default_array_temp as $value) { 
     367                  if (count($v2 = explode(":",$value)) > 1) { 
     368                      $default_array[] = $v2[1]; 
     369                  } else { 
     370                      $default_array[] = $value; 
     371                  } 
     372              }                       
     373          } 
     374 
     375          $flagCorner = true; 
     376          $i = 0; 
     377          $rows = $options[0]; 
     378          $columns = $options[1]; 
     379 
     380          $run_result .= '<table width=100%>'; 
     381          $run_result .= '<tr><td>&nbsp;</td>'; 
     382           
     383          $has_labels = prfext_array_is_associative($columns); 
     384           
     385          if ($has_labels) { 
     386              foreach($columns as $col_k => $col_v){ 
     387                $run_result .= '<td>'.$col_v.'</td>'; 
     388              } 
     389     
     390              foreach($rows as $row_k => $row_v){ 
     391                $run_result .= '<tr><td>'.$row_v.'</td>'; 
     392     
     393                foreach($columns as $col_k => $col_v){ 
     394                    $run_result .= '<td>'."<input type=\"radio\" onclick=\"select_field$form_name_prefix(this.form)\" name=\"".$form_name_prefix.'_'.$i."\" value=\"".$row_k.':'.$col_k."\" id=\"".$form_name_prefix.$i."\" "; 
     395                    if($default_value && $col_k == $default_array[$i]){ 
     396                      $run_result .= " checked=\"true\" "; 
     397                    } 
     398                    $run_result .='></input></td>'; 
     399                } 
     400     
     401                $i++; 
     402                $run_result .='</tr>'; 
     403              } 
     404          } else {          
     405 
     406              foreach($columns as $column){ 
     407                $run_result .= '<td>'.$column.'</td>'; 
     408              } 
     409     
     410              foreach($rows as $row){ 
     411                $run_result .= '<tr><td>'.$row.'</td>'; 
     412     
     413                foreach($columns as $column){ 
     414                    $run_result .= '<td>'."<input type=\"radio\" onclick=\"select_field$form_name_prefix(this.form)\" name=\"".$form_name_prefix.'_'.$i."\" value=\"".$row.':'.$column."\" id=\"".$form_name_prefix.$i."\" "; 
     415                    if($default_value && $column == $default_array[$i]){ 
     416                      $run_result .= " checked=\"true\" "; 
     417                    } 
     418                    $run_result .='></input></td>'; 
     419                } 
     420     
     421                $i++; 
     422                $run_result .='</tr>'; 
     423              } 
     424          } 
     425 
     426          $run_result .=" 
     427          </table> 
     428          <input type=\"hidden\" name=\"".$cleanid."\" value=\"".$parameter[1]."\" id=\"".$cleanid."\" />"; 
     429 
     430      break; 
    314431     
    315432   } 
     
    319436 
    320437/* 
    321 Note: the comment in datalib.php for Elgg 0.9 is incorrect for the  
     438Note: the comment in displaylib.php for Elgg 0.9 is incorrect for the  
    322439display_output_field parameter array 
    323440 
     
    328445                        0 => data 
    329446                        1 => type of input field 
    330                         2 => input name to display (for forms etc
    331                         3 => reference name (for tag fields and so on
     447                        2 => reference name (for tag fields and so on
     448                        3 => input name to display (for forms etc
    332449                        4 => ID number (if any) 
    333450                        5 => Owner (if not specified, current $page_owner is assumed) 
     
    348465        $value = empty($parameter[0])?$d->default_value:$parameter[0]; 
    349466        $display_type = $d->display_type; 
    350         $is_keyword_tag = $d->is_keyword_tag; 
    351467    } else { 
    352468        $value = $parameter[0]; 
    353469        $display_type = $name; 
     470    } 
     471    if (!isset($CFG->prfext_keyword_field_names)) { 
     472        prfext_set_keyword_cache(); 
     473    } 
     474     
     475    if (isset($CFG->prfext_keyword_field_names[$parameter[2]])) { 
     476        $is_keyword_tag = true; 
     477    } else {         
    354478        $is_keyword_tag = false; 
    355479    } 
     
    430554      break; 
    431555       
     556      case 'profile_radio_group': 
     557        $final = ''; 
     558        $options = $d->options; 
     559        $rows = $options[0]; 
     560        $columns = $options[1]; 
     561        $rowsLen = count($rows); 
     562        $rowsValue = explode(',',$value); 
     563         
     564        $has_labels = prfext_array_is_associative($rows); 
     565         
     566        if ($has_labels) { 
     567            for ($i = 0; $i < $rowsLen; $i++){ 
     568                $rv = $rowsValue[$i]; 
     569                if ($rv != ''){ 
     570                    $v = explode(':',$rv); 
     571                    $item = $rows[$v[0]] . ' : <i>'. $columns[$v[1]] .'</i>'; 
     572                    if ($is_keyword_tag && prfext_tag_count($rv) > 1) { 
     573                        $item = prfext_add_tag_link($item,$name,$rv); 
     574                    } 
     575                    $final .= ', ' . $item; 
     576                } 
     577            } 
     578        } else { 
     579            $i = 0; 
     580            foreach ($rows as $option){ 
     581                $rv = $rowsValue[$i]; 
     582                if ($rv != ''){ 
     583                    $v = explode(':',$rv); 
     584                    $item = $option . ' : <i>'. $v[1] .'</i>'; 
     585                    if ($is_keyword_tag && prfext_tag_count($rv) > 1) { 
     586                        $item = prfext_add_tag_link($item,$name,$rv); 
     587                    } 
     588                    $final .= ', ' . $item; 
     589                } 
     590                $i++; 
     591            } 
     592        } 
     593 
     594        return substr($final,1); 
     595 
     596      break; 
     597       
    432598      default: 
    433         $options = $d->options; 
    434         if (prfext_array_is_associative($options)) { 
     599        if (isset($d->options) && prfext_array_is_associative($d->options)) { 
     600            $options = $d->options; 
    435601            if ($is_keyword_tag && prfext_tag_count($value) > 1) { 
    436602                return prfext_add_tag_link($options[$value],$name,$value); 
     
    453619    global $CFG,$page_owner; 
    454620    return "<a href=\"".$CFG->wwwroot."search/index.php?".$tagtype 
    455         ."=".$value."&amp;owner=".$page_owner."\" >".$text."</a>\n"; 
     621        ."=".$value."&amp;owner=".$page_owner."\" >".$text."</a>"; 
    456622}     
    457623