Ignore:
Timestamp:
03/17/2009 01:57:58 PM (3 years ago)
Author:
ofer
Message:

Updated available languages, added auto-translate to administartion (not yet active)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/transposh_admin.php

    r54 r69  
    1919/* 
    2020 * Provide the admin page for configuring the translation options. eg.  what languages ? 
    21  * who is allowed to translate ?  
    22  *  
     21 * who is allowed to translate ? 
     22 * 
    2323 */ 
    2424 
     
    2828 
    2929/* 
    30  * Add transposh to the admin menu.  
     30 * Add transposh to the admin menu. 
    3131 * 
    3232 */ 
    3333function transposh_admin_menu() 
    3434{ 
    35     add_options_page('Transposh','Transposh', 6,'Transposh', 'transposh_admin_page'); 
    36 } 
    37  
    38  
    39 /* 
    40  * Create the admin page.  
     35        add_options_page('Transposh','Transposh', 6,'Transposh', 'transposh_admin_page'); 
     36} 
     37 
     38 
     39/* 
     40 * Create the admin page. 
    4141 * 
    4242 */ 
    4343function transposh_admin_page() 
    4444{ 
    45     logger('Entry ' . __METHOD__ , 4); 
    46  
    47     if($_POST['transposh_admin_posted']) 
    48     { 
     45        logger('Entry ' . __METHOD__ , 4); 
     46 
     47        if($_POST['transposh_admin_posted']) 
     48        { 
    4949                update_admin_options(); 
    5050        } 
    5151 
    52     echo '<div class="wrap alternate"> 
    53          <h2>Transposh</h2> 
    54          <form action="?page=Transposh" method="post"> 
    55          <h3>Supported Languages</h3>'; 
    56  
    57     insert_supported_langs(); 
    58     echo '<br/> <h3>Who can translate ?</h3>'; 
    59     insert_permissions(); 
    60  
    61     echo '<br/> <h3>Rewrite URLs </h3>'; 
    62     insert_permalink_rewrite_option(); 
    63  
    64     echo '<input type="hidden" name="transposh_admin_posted" value="1" /> 
    65           <p class="submit"><input type="submit" value="Save Changes" /></p> 
    66           </form> 
    67           </div>'; 
     52        echo '<div class="wrap alternate">'. 
     53         '<h2>Transposh</h2>'. 
     54         '<form action="?page=Transposh" method="post">'. 
     55         '<h3>Supported Languages</h3>'; 
     56 
     57        insert_supported_langs(); 
     58        echo '<br/><h3>Who can translate ?</h3>'; 
     59        insert_permissions(); 
     60 
     61        echo '<br/><h3>Rewrite URLs</h3>'; 
     62        insert_permalink_rewrite_option(); 
     63 
     64        echo '<br/><h3>Enable automatic translation</h3>'; 
     65        insert_auto_translate_option(); 
     66 
     67        echo '<input type="hidden" name="transposh_admin_posted" value="1" />'. 
     68                 '<p class="submit"><input type="submit" value="Save Changes" /></p>'. 
     69                 '</form>'. 
     70                 '</div>'; 
    6871} 
    6972 
     
    7477function insert_supported_langs() 
    7578{ 
    76     global $languages, $plugin_url; 
    77      
    78     echo ' 
     79        global $languages, $plugin_url; 
     80 
     81        echo ' 
    7982    <script type="text/javascript" > 
    8083        function chbx_change(lang) 
     
    8689                document.getElementById(edit).checked = true; 
    8790            } 
    88              
     91 
    8992        } 
    9093    </script> 
    91            
     94 
    9295    <table> 
    9396    <tr>'; 
    94      
    95  
    96     $columns = 2; 
    97      
    98     for($hdr=0; $hdr < $columns; $hdr++) 
    99     { 
    100         echo '<th>Language </th> <th>Viewable </th> <th>Translatable </th> 
    101               <th>Default</th><th style="padding-right: 80px"> </th>'; 
    102     } 
    103      
    104     echo '</tr>'; 
    105      
    106     foreach($languages as $code => $lang) 
    107     { 
    108         list ($language,$flag) = explode (",",$lang); 
    109         if($i % $columns == 0) 
    110         { 
    111             echo '</tr>'; 
    112         } 
    113         echo "\n"; 
    114          
    115         $i++; 
    116          
    117         echo "<td><img src=\"$plugin_url/flags/$flag.png\"/>&nbsp;$language</td>"; 
    118         echo '<td align="center">  <input type="checkbox" id="' . $code .'_view" name="' . 
    119             $code . '_view" onChange="chbx_change(\'' . $code . '\')"' . is_viewable($code) . '/></td>'; 
    120         echo "\n"; 
    121         echo '<td align="center">  <input type="checkbox" id="' . $code . '_edit" name="' . 
    122             $code . '_edit" ' . is_editable($code). '/></td>'; 
    123         echo "\n"; 
    124         echo "<td align=\"center\"><input type=\"radio\" name=\"default_lang\" value=\"$code\"" .  
    125                is_default_lang($code). "/> </td>"; 
    126                  
    127         if($i % $columns == 0) 
    128         { 
    129             echo '</tr>'; 
    130         } 
    131         else 
    132         { 
    133             echo "<td><style padding-right: 60px></style></td>"; 
    134         } 
    135          
    136         echo "\n"; 
    137          
    138     } 
    139  
    140     echo '</table>'; 
     97 
     98 
     99        $columns = 2; 
     100 
     101        for($hdr=0; $hdr < $columns; $hdr++) 
     102        { 
     103                echo '<th>Language</th><th>Viewable</th><th>Translatable</th> 
     104              <th>Default</th><th style="padding-right: 80px"></th>'; 
     105        } 
     106 
     107        echo '</tr>'; 
     108 
     109        foreach($languages as $code => $lang) 
     110        { 
     111                list ($language,$flag) = explode (",",$lang); 
     112                if($i % $columns == 0) 
     113                { 
     114                        echo '</tr>'; 
     115                } 
     116                echo "\n"; 
     117 
     118                $i++; 
     119 
     120                echo "<td><img src=\"$plugin_url/flags/$flag.png\"/>&nbsp;$language</td>"; 
     121                echo '<td align="center">  <input type="checkbox" id="' . $code .'_view" name="' . 
     122                $code . '_view" onChange="chbx_change(\'' . $code . '\')"' . is_viewable($code) . '/></td>'; 
     123                echo "\n"; 
     124                echo '<td align="center">  <input type="checkbox" id="' . $code . '_edit" name="' . 
     125                $code . '_edit" ' . is_editable($code). '/></td>'; 
     126                echo "\n"; 
     127                echo "<td align=\"center\"><input type=\"radio\" name=\"default_lang\" value=\"$code\"" . 
     128                is_default_lang($code). "/> </td>"; 
     129 
     130                if($i % $columns == 0) 
     131                { 
     132                        echo '</tr>'; 
     133                } 
     134                else 
     135                { 
     136                        echo "<td><style padding-right: 60px></style></td>"; 
     137                } 
     138                echo "\n"; 
     139        } 
     140 
     141        echo '</table>'; 
    141142} 
    142143 
     
    148149function is_editable($code) 
    149150{ 
    150     $langs = get_option(EDITABLE_LANGS); 
    151      
    152     if(strstr($langs, $code)) 
    153     { 
    154         return "checked"; 
    155     } 
    156      
    157     return ""; 
    158 } 
    159  
     151        $langs = get_option(EDITABLE_LANGS); 
     152 
     153        if(strstr($langs, $code)) 
     154        { 
     155                return "checked"; 
     156        } 
     157 
     158        return ""; 
     159} 
    160160 
    161161/* 
     
    165165function is_viewable($code) 
    166166{ 
    167     $langs = get_option(VIEWABLE_LANGS); 
    168     if(strstr($langs, $code)) 
    169     { 
    170         return "checked"; 
    171     } 
    172      
    173     return ""; 
     167        $langs = get_option(VIEWABLE_LANGS); 
     168        if(strstr($langs, $code)) 
     169        { 
     170                return "checked"; 
     171        } 
     172 
     173        return ""; 
    174174} 
    175175 
     
    180180function is_default_lang($code) 
    181181{ 
    182     global $languages; 
    183      
    184     $default = get_option(DEFAULT_LANG); 
    185  
    186     if(!$languages[$default]) 
    187     { 
    188         $default = "en"; 
    189     } 
    190      
    191     if($default ==  $code) 
    192     { 
    193         return "checked"; 
    194     } 
    195  
    196     return ""; 
     182        global $languages; 
     183 
     184        $default = get_option(DEFAULT_LANG); 
     185 
     186        if(!$languages[$default]) 
     187        { 
     188                $default = "en"; 
     189        } 
     190 
     191        if($default ==  $code) 
     192        { 
     193                return "checked"; 
     194        } 
     195 
     196        return ""; 
    197197} 
    198198 
     
    203203function insert_permissions() 
    204204{ 
    205     global $wp_roles; 
    206  
    207     //display known roles and their permission to translate 
    208     foreach($wp_roles->get_names() as $role_name => $something) 
    209     { 
    210         echo '<input type="checkbox" value="1" name="' . $role_name . '" ' . can_translate($role_name) . 
     205        global $wp_roles; 
     206 
     207        //display known roles and their permission to translate 
     208        foreach($wp_roles->get_names() as $role_name => $something) 
     209        { 
     210                echo '<input type="checkbox" value="1" name="' . $role_name . '" ' . can_translate($role_name) . 
    211211             '" />' . $role_name . '&nbsp&nbsp&nbsp</input>'; 
    212     } 
    213  
    214     //Add our own custom role 
    215     echo '<input type="checkbox" value="1" name="anonymous"'     . 
    216             can_translate('anonymous') . '" /> Anonymous</input>'; 
    217 } 
    218  
     212        } 
     213 
     214        //Add our own custom role 
     215        echo '<input type="checkbox" value="1" name="anonymous"'     . 
     216        can_translate('anonymous') . '" /> Anonymous</input>'; 
     217} 
    219218 
    220219/* 
     
    225224function insert_permalink_rewrite_option() 
    226225{ 
    227     $checked = ""; 
    228     if(get_option(ENABLE_PERMALINKS_REWRITE)) 
    229     { 
    230         $checked = 'checked'; 
    231     } 
    232  
    233     echo '<input type="checkbox" value="1" name="enable_permalinks"'     . 
    234             $checked . '" /> Rewrite URLs to be search engine friendly, 
    235             e.g.  (http://wordpress.org/<strong> en</strong>).  
    236             Requires that permalinks will be enabled. </input>'; 
    237 } 
    238  
    239  
    240 /* 
    241  * Indicates whether the given role can translate.  
    242  * Return either "checked" or ""  
     226        $checked = ""; 
     227        if(get_option(ENABLE_PERMALINKS_REWRITE)) 
     228        { 
     229                $checked = 'checked'; 
     230        } 
     231 
     232        echo '<input type="checkbox" value="1" name="enable_permalinks"'. $checked . '"/>'. 
     233                 'Rewrite URLs to be search engine friendly, '. 
     234                 'e.g.  (http://wordpress.org/<strong> en</strong>). '. 
     235         'Requires that permalinks will be enabled.'. 
     236         '</input>'; 
     237} 
     238 
     239/* 
     240 * Insert the option to enable/disable automatic translation. 
     241 * Enabled by default. 
     242 * 
     243 */ 
     244function insert_auto_translate_option() 
     245{ 
     246        $checked = ""; 
     247        if(get_option(ENABLE_AUTO_TRANSLATE,1)) 
     248        { 
     249                $checked = 'checked'; 
     250        } 
     251 
     252        echo '<input type="checkbox" value="1" name="enable_autotranslate"'.$checked.'"/>'. 
     253             'Allow automatic translation of pages (currently using Google Translate).'. 
     254             '</input>'; 
     255} 
     256 
     257/* 
     258 * Indicates whether the given role can translate. 
     259 * Return either "checked" or "" 
    243260 */ 
    244261function can_translate($role_name) 
    245262{ 
    246     global $wp_roles; 
    247     if($role_name != 'anonymous') 
    248     { 
    249         $role = $wp_roles->get_role($role_name); 
    250         if(isset($role) && $role->has_cap(TRANSLATOR)) 
    251         { 
    252             return 'checked'; 
    253         } 
    254     } 
    255     else 
    256     { 
    257         $allow_anonymous = get_option(ANONYMOUS_TRANSLATION); 
    258         if($allow_anonymous == "1") 
    259         { 
    260             return 'checked'; 
    261         } 
    262     } 
    263      
    264     return ""; 
    265 } 
    266  
    267 /* 
    268  * Handle newly posted admin options.  
    269  * 
    270  */  
     263        global $wp_roles; 
     264        if($role_name != 'anonymous') 
     265        { 
     266                $role = $wp_roles->get_role($role_name); 
     267                if(isset($role) && $role->has_cap(TRANSLATOR)) 
     268                { 
     269                        return 'checked'; 
     270                } 
     271        } 
     272        else 
     273        { 
     274                $allow_anonymous = get_option(ANONYMOUS_TRANSLATION); 
     275                if($allow_anonymous == "1") 
     276                { 
     277                        return 'checked'; 
     278                } 
     279        } 
     280 
     281        return ""; 
     282} 
     283 
     284/* 
     285 * Handle newly posted admin options. 
     286 * 
     287 */ 
    271288function update_admin_options() 
    272289{ 
    273     logger('Entry ' . __METHOD__, 4); 
    274     global $wp_roles, $languages; 
    275     $viewable_langs = array(); 
    276     $editable_langs = array(); 
    277      
    278     //update roles and capabilities 
    279     foreach($wp_roles->get_names() as $role_name => $something) 
    280     { 
    281         $role = $wp_roles->get_role($role_name); 
    282         if($_POST[$role_name] == "1") 
    283         { 
    284             $role->add_cap(TRANSLATOR); 
    285         } 
    286         else 
    287         { 
    288             $role->remove_cap(TRANSLATOR); 
    289         } 
    290     } 
    291  
    292     //Anonymous needs to be handled differently as it does not have a role 
    293     if($_POST['anonymous'] == "1") 
    294     { 
    295         update_option(ANONYMOUS_TRANSLATION, 1); 
    296     } 
    297     else 
    298     { 
    299         update_option(ANONYMOUS_TRANSLATION, 0); 
    300     } 
    301  
    302      
    303     //Update the list of supported/editable languages 
    304     foreach($languages as $code => $lang) 
    305     { 
    306         if($_POST[$code . '_view']) 
    307         { 
    308             $viewable_langs[] = $code; 
    309         } 
    310  
    311         if($_POST[$code . '_edit']) 
    312         { 
    313             $editable_langs[] = $code; 
    314         } 
    315     } 
    316  
    317     update_option(VIEWABLE_LANGS, implode(',', $viewable_langs)); 
    318     update_option(EDITABLE_LANGS, implode(',', $editable_langs)); 
    319     update_option(DEFAULT_LANG,   $_POST['default_lang']); 
    320  
    321     if(get_option(ENABLE_PERMALINKS_REWRITE) != $_POST['enable_permalinks']) 
    322     { 
    323         global $wp_rewrite; 
    324         update_option(ENABLE_PERMALINKS_REWRITE, $_POST['enable_permalinks']); 
    325  
    326         //rewrite rules 
    327         add_filter('rewrite_rules_array', 'update_rewrite_rules'); 
    328         $wp_rewrite->flush_rules(); 
    329     } 
    330      
    331      
    332     echo '<div id="message"class="updated fade">';       
    333     echo ('<p> Changes saved</p>');                      
    334     echo '</div>'; 
     290        logger('Entry ' . __METHOD__, 4); 
     291        global $wp_roles, $languages; 
     292        $viewable_langs = array(); 
     293        $editable_langs = array(); 
     294 
     295        //update roles and capabilities 
     296        foreach($wp_roles->get_names() as $role_name => $something) 
     297        { 
     298                $role = $wp_roles->get_role($role_name); 
     299                if($_POST[$role_name] == "1") 
     300                { 
     301                        $role->add_cap(TRANSLATOR); 
     302                } 
     303                else 
     304                { 
     305                        $role->remove_cap(TRANSLATOR); 
     306                } 
     307        } 
     308 
     309        //Anonymous needs to be handled differently as it does not have a role 
     310        if($_POST['anonymous'] == "1") 
     311        { 
     312                update_option(ANONYMOUS_TRANSLATION, 1); 
     313        } 
     314        else 
     315        { 
     316                update_option(ANONYMOUS_TRANSLATION, 0); 
     317        } 
     318 
     319 
     320        //Update the list of supported/editable languages 
     321        foreach($languages as $code => $lang) 
     322        { 
     323                if($_POST[$code . '_view']) 
     324                { 
     325                        $viewable_langs[] = $code; 
     326                } 
     327 
     328                if($_POST[$code . '_edit']) 
     329                { 
     330                        $editable_langs[] = $code; 
     331                } 
     332        } 
     333 
     334        update_option(VIEWABLE_LANGS, implode(',', $viewable_langs)); 
     335        update_option(EDITABLE_LANGS, implode(',', $editable_langs)); 
     336        update_option(DEFAULT_LANG,   $_POST['default_lang']); 
     337 
     338        if(get_option(ENABLE_PERMALINKS_REWRITE) != $_POST['enable_permalinks']) 
     339        { 
     340                global $wp_rewrite; 
     341                update_option(ENABLE_PERMALINKS_REWRITE, $_POST['enable_permalinks']); 
     342 
     343                //rewrite rules 
     344                add_filter('rewrite_rules_array', 'update_rewrite_rules'); 
     345                $wp_rewrite->flush_rules(); 
     346        } 
     347 
     348        if(get_option(ENABLE_AUTO_TRANSLATE,1) != $_POST['enable_autotranslate']) 
     349        { 
     350                update_option(ENABLE_AUTO_TRANSLATE, $_POST['enable_autotranslate']); 
     351        } 
     352 
     353        echo '<div id="message"class="updated fade">'; 
     354        echo ('<p> Changes saved</p>'); 
     355        echo '</div>'; 
    335356} 
    336357 
    337358add_action('admin_menu', 'transposh_admin_menu'); 
    338  
    339359?> 
Note: See TracChangeset for help on using the changeset viewer.