Dotclear

Changeset 1070:64699c5b7cc9


Ignore:
Timestamp:
12/17/12 00:15:04 (13 years ago)
Author:
JcDenis
Branch:
twig
Message:
  • First draft for the use of Twig on plugins
  • aboutConfig is now "Twig compliant"
Files:
4 added
4 edited

Legend:

Unmodified
Added
Removed
  • admin/plugin.php

    r500 r1070  
    1515dcPage::check('usage,contentadmin'); 
    1616 
     17//* TODO: Do it better later, required by some javascripts 
     18$some_globals = array( 
     19     'rtl' => l10n::getTextDirection($_lang) == 'rtl', 
     20     'Nonce' => $core->getNonce(), 
     21     'sess_id' => session_id(), 
     22     'sess_uid' => $_SESSION['sess_browser_uid'], 
     23     'media_manage' => $core->auth->check('media,media_admin',$core->blog->id), 
     24     'enable_wysiwyg' => isset($core->auth) && $core->auth->getOption('enable_wysiwyg'), 
     25     'edit_size' => $core->auth->getOption('edit_size') 
     26); 
     27foreach($some_globals as $name => $value) { 
     28     $_ctx->$name = $value; 
     29}; 
     30//*/ 
     31 
     32$has_content = false; 
    1733$p_file = ''; 
    1834$p = !empty($_REQUEST['p']) ? $_REQUEST['p'] : null; 
    19 $popup = (integer) !empty($_REQUEST['popup']); 
    20  
    21 if ($popup) { 
    22      $open_f = array('dcPage','openPopup'); 
    23      $close_f = array('dcPage','closePopup'); 
    24 } else { 
    25      $open_f = array('dcPage','open'); 
    26      $close_f = array('dcPage','close'); 
    27 } 
     35$popup = $_ctx->popup = (integer) !empty($_REQUEST['popup']); 
    2836 
    2937if ($core->plugins->moduleExists($p)) { 
    3038     $p_file = $core->plugins->moduleRoot($p).'/index.php'; 
    3139} 
     40if (file_exists($p_file)) { 
    3241 
    33 if (file_exists($p_file)) 
    34 { 
    35      # Loading plugin 
     42//* Keep this for old style plugins using dcPage 
     43     if ($popup) { 
     44          $open_f = array('dcPage','openPopup'); 
     45          $close_f = array('dcPage','closePopup'); 
     46     } else { 
     47          $open_f = array('dcPage','open'); 
     48          $close_f = array('dcPage','close'); 
     49     } 
     50      
    3651     $p_info = $core->plugins->getModules($p); 
    37       
    3852     $p_url = 'plugin.php?p='.$p; 
    39       
    40      $p_title = 'no content - plugin'; 
    41      $p_head = ''; 
    42      $p_content = '<p>'.__('No content found on this plugin.').'</p>'; 
    43       
     53     $p_title = $p_head = $p_content = ''; 
     54//*/  
     55     # Get page content 
    4456     ob_start(); 
    4557     include $p_file; 
    4658     $res = ob_get_contents(); 
    4759     ob_end_clean(); 
    48       
    49      if (preg_match('|<head>(.*?)</head|ms',$res,$m)) { 
    50           if (preg_match('|<title>(.*?)</title>|ms',$m[1],$mt)) { 
    51                $p_title = $mt[1]; 
    52           } 
     60 
     61//* Keep this for old style plugins using dcPage 
     62     if (!$_ctx->hasPageTitle()) { 
    5363           
    54           if (preg_match_all('|(<script.*?>.*?</script>)|ms',$m[1],$ms)) { 
    55                foreach ($ms[1] as $v) { 
    56                     $p_head .= $v."\n"; 
     64          if (preg_match('|<head>(.*?)</head|ms',$res,$m)) { 
     65               if (preg_match('|<title>(.*?)</title>|ms',$m[1],$mt)) { 
     66                    $p_title = $mt[1]; 
     67               } 
     68                
     69               if (preg_match_all('|(<script.*?>.*?</script>)|ms',$m[1],$ms)) { 
     70                    foreach ($ms[1] as $v) { 
     71                         $p_head .= $v."\n"; 
     72                    } 
     73               } 
     74                
     75               if (preg_match_all('|(<style.*?>.*?</style>)|ms',$m[1],$ms)) { 
     76                    foreach ($ms[1] as $v) { 
     77                         $p_head .= $v."\n"; 
     78                    } 
     79               } 
     80                
     81               if (preg_match_all('|(<link.*?/>)|ms',$m[1],$ms)) { 
     82                    foreach ($ms[1] as $v) { 
     83                         $p_head .= $v."\n"; 
     84                    } 
    5785               } 
    5886          } 
    5987           
    60           if (preg_match_all('|(<style.*?>.*?</style>)|ms',$m[1],$ms)) { 
    61                foreach ($ms[1] as $v) { 
    62                     $p_head .= $v."\n"; 
    63                } 
    64           } 
    65            
    66           if (preg_match_all('|(<link.*?/>)|ms',$m[1],$ms)) { 
    67                foreach ($ms[1] as $v) { 
    68                     $p_head .= $v."\n"; 
    69                } 
     88          if (preg_match('|<body.*?>(.+)</body>|ms',$res,$m)) { 
     89               $p_content = $m[1]; 
     90                
     91               call_user_func($open_f,$p_title,$p_head); 
     92               echo $p_content; 
     93               call_user_func($close_f); 
     94                
     95               $has_content = true; 
    7096          } 
    7197     } 
    72       
    73      if (preg_match('|<body.*?>(.+)</body>|ms',$res,$m)) { 
    74           $p_content = $m[1]; 
     98//*/ 
     99     # Check context and display 
     100     if ($_ctx->hasPageTitle() && !empty($res)) { 
     101          $has_content = true; 
     102          echo $res; 
    75103     } 
    76       
    77      call_user_func($open_f,$p_title,$p_head); 
    78      echo $p_content; 
    79      call_user_func($close_f); 
    80104} 
    81 else 
    82 { 
    83      call_user_func($open_f,__('Plugin not found')); 
    84       
    85      echo '<h2 class="page-title">'.__('Plugin not found').'</h2>'; 
    86       
    87      echo '<p>'.__('The plugin you reached does not exist or does not have an admin page.').'</p>'; 
    88       
    89      call_user_func($close_f); 
     105# No plugin or content found 
     106if (!$has_content) { 
     107     $_ctx->setPageTitle(__('Plugin not found')); 
     108     $_ctx->addError(__('The plugin you reached does not exist or does not have an admin page.')); 
     109     $core->tpl->display('plugin.html.twig'); 
    90110} 
    91111?> 
  • inc/admin/class.dc.admincontext.php

    r1064 r1070  
    3939               'admin_url'    => DC_ADMIN_URL, 
    4040               'theme_url'    => DC_ADMIN_URL.'index.php?tf=', 
     41               'plugin_url'   => DC_ADMIN_URL.'index.php?pf=', 
    4142                
    4243               'version'           => DC_VERSION, 
     
    201202     { 
    202203          $this->protected_globals['page_title'] = $title; 
     204     } 
     205      
     206     /** 
     207     Check if a page title is set 
     208     */ 
     209     public function hasPageTitle() 
     210     { 
     211          return !empty($this->protected_globals['page_title']); 
    203212     } 
    204213      
  • plugins/aboutConfig/_admin.php

    r270 r1070  
    1515          preg_match('/plugin.php\?p=aboutConfig(&.*)?$/',$_SERVER['REQUEST_URI']), 
    1616          $core->auth->isSuperAdmin()); 
     17 
     18$core->tpl->getLoader()->addPath(dirname(__FILE__).'/admtpl/','aboutConfig'); 
    1719?> 
  • plugins/aboutConfig/index.php

    r907 r1070  
    1212if (!defined('DC_CONTEXT_ADMIN')) { return; } 
    1313 
    14 # Local navigation 
    15 if (!empty($_POST['gs_nav'])) { 
    16      http::redirect($p_url.$_POST['gs_nav']); 
    17      exit; 
    18 } 
    19 if (!empty($_POST['ls_nav'])) { 
    20      http::redirect($p_url.$_POST['ls_nav']); 
    21      exit; 
    22 } 
    23  
    24 # Local settings update 
    25 if (!empty($_POST['s']) && is_array($_POST['s'])) 
     14class adminPageAboutConfig 
    2615{ 
    27      try 
     16     public static $p_url = 'plugin.php?p=aboutConfig'; 
     17      
     18     # Update local settings 
     19     public static function updLocal($form) 
    2820     { 
    29           foreach ($_POST['s'] as $ns => $s) 
    30           { 
    31                $core->blog->settings->addNamespace($ns); 
     21          self::updSettings($form); 
     22     } 
     23      
     24     # Update global settings 
     25     public static function updGlobal($form) 
     26     { 
     27          self::updSettings($form,true); 
     28     } 
     29      
     30     # Update settings 
     31     protected static function updSettings($form,$global=false) 
     32     { 
     33          global $core,$_ctx; 
     34           
     35          $part = $global ? 'global' : 'local'; 
     36          $prefix = $part.'_'; 
     37           
     38          try { 
     39               foreach ($core->blog->settings->dumpNamespaces() as $ns => $namespace) { 
     40                    $core->blog->settings->addNamespace($ns); 
     41                    $ns_settings = $global ?  
     42                         $namespace->dumpGlobalSettings() : $namespace->dumpSettings(); 
     43                     
     44                    foreach ($ns_settings as $k => $v) { 
     45                         // need to cast type 
     46                         $f = (string) $form->{$prefix.$ns.'_'.$k}; 
     47                         settype($f,$v['type']); 
     48                          
     49                         $core->blog->settings->$ns->put($k,$f,null,null,true,$global); 
     50                         $form->{$prefix.$ns.'_'.$k} = $f; 
     51                    } 
     52               } 
     53               $core->blog->triggerBlog(); 
    3254                
    33                foreach ($s as $k => $v)      { 
    34                     $core->blog->settings->$ns->put($k,$v); 
    35                } 
    36                 
    37                $core->blog->triggerBlog(); 
     55               http::redirect(self::$p_url.'&upd=1&part='.$part); 
     56          } 
     57          catch (Exception $e) { 
     58               $_ctx->addError($e->getMessage()); 
     59          } 
     60     } 
     61      
     62     # Set nav and settings forms 
     63     public static function setForms($global=false) 
     64     { 
     65          global $core, $_ctx; 
     66           
     67          $prefix = $global ? 'global_' : 'local_'; 
     68          $action = $global ? 'updGlobal' : 'updLocal'; 
     69           
     70          if (!empty($_POST[$prefix.'nav'])) { 
     71               http::redirect(self::$p_url.$_POST[$prefix.'nav']); 
     72               exit; 
    3873          } 
    3974           
    40           http::redirect($p_url.'&upd=1'); 
    41      } 
    42      catch (Exception $e) 
    43      { 
    44           $core->error->add($e->getMessage()); 
     75          $nav_form = new dcForm($core,$prefix.'nav_form','plugin.php'); 
     76          $settings_form = new dcForm($core,$prefix.'settings_form','plugin.php'); 
     77           
     78          $settings = $combo = array(); 
     79          foreach ($core->blog->settings->dumpNamespaces() as $ns => $namespace) { 
     80               $ns_settings = $global ?  
     81                    $namespace->dumpGlobalSettings() : $namespace->dumpSettings(); 
     82                
     83               foreach ($ns_settings as $k => $v) { 
     84                    $settings[$ns][$k] = $v; 
     85               } 
     86          } 
     87           
     88          ksort($settings); 
     89          foreach ($settings as $ns => $s) { 
     90               $combo['#'.$prefix.$ns] = $ns; 
     91               ksort($s); 
     92               foreach ($s as $k => $v) { 
     93                    if ($v['type'] == 'boolean') { 
     94                         $settings_form->addField( 
     95                              new dcFieldCombo($prefix.$ns.'_'.$k, 
     96                                   '',array(1 => __('yes'),0 => __('no')))); 
     97                    } 
     98                    else { 
     99                         $settings_form->addField( 
     100                              new dcFieldText($prefix.$ns.'_'.$k,'')); 
     101                    } 
     102                    $settings_form->{$prefix.$ns.'_'.$k} = $v['value']; 
     103               } 
     104          } 
     105           
     106          $nav_form 
     107               ->addField( 
     108                    new dcFieldCombo($prefix.'nav','',$combo,array( 
     109                         "label" => __('Goto:')))) 
     110               ->addField( 
     111                    new dcFieldSubmit($prefix.'nav_submit',__('OK'))) 
     112               ->addField( 
     113                    new dcFieldHidden ('p','aboutConfig')) 
     114               ; 
     115           
     116          $settings_form 
     117               ->addField( 
     118                    new dcFieldSubmit($prefix.'submit',__('Save'),array( 
     119                         'action' => array('adminPageAboutConfig',$action)))) 
     120               ->addField( 
     121                    new dcFieldHidden ('p','aboutConfig')) 
     122               ; 
     123           
     124          $_ctx->{$prefix.'settings'} = $settings; 
     125           
     126          $nav_form->setup(); 
     127          $settings_form->setup(); 
    45128     } 
    46129} 
    47130 
    48 # Global settings update 
    49 if (!empty($_POST['gs']) && is_array($_POST['gs'])) 
    50 { 
    51      try 
    52      { 
    53           foreach ($_POST['gs'] as $ns => $s) 
    54           { 
    55                $core->blog->settings->addNamespace($ns); 
    56                 
    57                foreach ($s as $k => $v)      { 
    58                     $core->blog->settings->$ns->put($k,$v,null,null,true,true); 
    59                } 
    60                 
    61                $core->blog->triggerBlog(); 
    62           } 
    63            
    64           http::redirect($p_url.'&upd=1&part=global'); 
    65      } 
    66      catch (Exception $e) 
    67      { 
    68           $core->error->add($e->getMessage()); 
    69      } 
     131# Local settings forms 
     132adminPageAboutConfig::setForms(); 
     133 
     134# Global settings forms 
     135adminPageAboutConfig::setForms(true); 
     136 
     137# Commons 
     138if (!empty($_GET['upd'])) { 
     139     $_ctx->setMessage(__('Configuration successfully updated')); 
    70140} 
    71  
    72 $part = !empty($_GET['part']) && $_GET['part'] == 'global' ? 'global' : 'local'; 
    73  
    74 function settingLine($id,$s,$ns,$field_name,$strong_label) 
    75 { 
    76      if ($s['type'] == 'boolean') { 
    77           $field = form::combo(array($field_name.'['.$ns.']['.$id.']',$field_name.'_'.$id), 
    78           array(__('yes') => 1, __('no') => 0),$s['value']); 
    79      } else { 
    80           $field = form::field(array($field_name.'['.$ns.']['.$id.']',$field_name.'_'.$id),40,null, 
    81           html::escapeHTML($s['value'])); 
    82      } 
    83       
    84      $slabel = $strong_label ? '<strong>%s</strong>' : '%s'; 
    85       
    86      return 
    87      '<tr>'. 
    88      '<td scope="raw"><label for="s_'.$id.'">'.sprintf($slabel,html::escapeHTML($id)).'</label></td>'. 
    89      '<td>'.$field.'</td>'. 
    90      '<td>'.$s['type'].'</td>'. 
    91      '<td>'.html::escapeHTML($s['label']).'</td>'. 
    92      '</tr>'; 
     141if (!empty($_GET['upda'])) { 
     142     $_ctx->setMessage(__('Settings definition successfully updated')); 
    93143} 
     144$_ctx->default_tab = !empty($_GET['part']) && $_GET['part'] == 'global' ? 'global' : 'local'; 
     145$_ctx->setPageTitle('about:config'); 
     146$core->tpl->display('@aboutConfig/index.html.twig'); 
    94147?> 
    95 <html> 
    96 <head> 
    97   <title>about:config</title> 
    98   <?php echo dcPage::jsPageTabs($part); ?> 
    99   <style type="text/css"> 
    100   table.settings { border: 1px solid #999; margin-bottom: 2em; } 
    101   table.settings th { background: #f5f5f5; color: #444; padding-top: 0.3em; padding-bottom: 0.3em; } 
    102   p.anchor-nav {float: right; } 
    103   </style> 
    104      <script type="text/javascript"> 
    105      //<![CDATA[ 
    106      $(function() { 
    107           $("#gs_submit").hide(); 
    108           $("#ls_submit").hide(); 
    109           $("#gs_nav").change(function() { 
    110                window.location = $("#gs_nav option:selected").val(); 
    111           }) 
    112           $("#ls_nav").change(function() { 
    113                window.location = $("#ls_nav option:selected").val(); 
    114           }) 
    115      }); 
    116      //]]> 
    117      </script> 
    118 </head> 
    119  
    120 <body> 
    121 <?php 
    122 if (!empty($_GET['upd'])) { 
    123      dcPage::message(__('Configuration successfully updated')); 
    124 } 
    125  
    126 if (!empty($_GET['upda'])) { 
    127      dcPage::message(__('Settings definition successfully updated')); 
    128 } 
    129 ?> 
    130 <h2><?php echo html::escapeHTML($core->blog->name); ?> &rsaquo; <span class="page-title">about:config</span></h2> 
    131  
    132 <div id="local" class="multi-part" title="<?php echo __('blog settings'); ?>"> 
    133  
    134 <?php  
    135 $table_header = '<table class="settings" id="%s"><caption>%s</caption>'. 
    136 '<thead>'. 
    137 '<tr>'."\n". 
    138 '  <th class="nowrap">Setting ID</th>'."\n". 
    139 '  <th>'.__('Value').'</th>'."\n". 
    140 '  <th>'.__('Type').'</th>'."\n". 
    141 '  <th class="maximalx">'.__('Description').'</th>'."\n". 
    142 '</tr>'."\n". 
    143 '</thead>'."\n". 
    144 '<tbody>'; 
    145 $table_footer = '</tbody></table>'; 
    146  
    147 $settings = array(); 
    148 foreach ($core->blog->settings->dumpNamespaces() as $ns => $namespace) { 
    149      foreach ($namespace->dumpSettings() as $k => $v) { 
    150           $settings[$ns][$k] = $v; 
    151      } 
    152 } 
    153 ksort($settings); 
    154 if (count($settings) > 0) { 
    155      $ns_combo = array(); 
    156      foreach ($settings as $ns => $s) { 
    157           $ns_combo[$ns] = '#l_'.$ns; 
    158      } 
    159      echo  
    160           '<form action="plugin.php" method="post">'. 
    161           '<p class="anchor-nav">'. 
    162           '<label for="ls_nav" class="classic">'.__('Goto:').'</label> '.form::combo('ls_nav',$ns_combo). 
    163           ' <input type="submit" value="'.__('Ok').'" id="ls_submit" />'. 
    164           '<input type="hidden" name="p" value="aboutConfig" />'. 
    165           $core->formNonce().'</p></form>'; 
    166 } 
    167 ?> 
    168  
    169 <form action="plugin.php" method="post"> 
    170  
    171 <?php 
    172 foreach ($settings as $ns => $s) 
    173 { 
    174      ksort($s); 
    175      echo sprintf($table_header,'l_'.$ns,$ns); 
    176      foreach ($s as $k => $v) 
    177      { 
    178           echo settingLine($k,$v,$ns,'s',!$v['global']); 
    179      } 
    180      echo $table_footer; 
    181 } 
    182 ?> 
    183  
    184 <p><input type="submit" value="<?php echo __('Save'); ?>" /> 
    185 <input type="hidden" name="p" value="aboutConfig" /> 
    186 <?php echo $core->formNonce(); ?></p> 
    187 </form> 
    188 </div> 
    189  
    190 <div id="global" class="multi-part" title="<?php echo __('global settings'); ?>"> 
    191  
    192 <?php 
    193 $settings = array(); 
    194  
    195 foreach ($core->blog->settings->dumpNamespaces() as $ns => $namespace) { 
    196      foreach ($namespace->dumpGlobalSettings() as $k => $v) { 
    197           $settings[$ns][$k] = $v; 
    198      } 
    199 } 
    200  
    201 ksort($settings); 
    202  
    203 if (count($settings) > 0) { 
    204      $ns_combo = array(); 
    205      foreach ($settings as $ns => $s) { 
    206           $ns_combo[$ns] = '#g_'.$ns; 
    207      } 
    208      echo  
    209           '<form action="plugin.php" method="post">'. 
    210           '<p class="anchor-nav">'. 
    211           '<label for="gs_nav" class="classic">'.__('Goto:').'</label> '.form::combo('gs_nav',$ns_combo). 
    212           ' <input type="submit" value="'.__('Ok').'" id="gs_submit" />'. 
    213           '<input type="hidden" name="p" value="aboutConfig" />'. 
    214           $core->formNonce().'</p></form>'; 
    215 } 
    216 ?> 
    217  
    218 <form action="plugin.php" method="post"> 
    219  
    220 <?php 
    221 foreach ($settings as $ns => $s) 
    222 { 
    223      ksort($s); 
    224      echo sprintf($table_header,'g_'.$ns,$ns); 
    225      foreach ($s as $k => $v) 
    226      { 
    227           echo settingLine($k,$v,$ns,'gs',false); 
    228      } 
    229      echo $table_footer; 
    230 } 
    231 ?> 
    232  
    233 <p><input type="submit" value="<?php echo __('Save'); ?>" /> 
    234 <input type="hidden" name="p" value="aboutConfig" /> 
    235 <?php echo $core->formNonce(); ?></p> 
    236 </form> 
    237 </div> 
    238  
    239 </body> 
    240 </html> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map