Dotclear


Ignore:
Timestamp:
10/02/13 17:00:38 (12 years ago)
Author:
Denis Jean-Chirstian <contact@…>
Branch:
dcRepo
Message:

Calls to dcModules and dcRepository from plugins and themes page are now through adminModulesList, cleaner and healthy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/lib.moduleslist.php

    r2195 r2215  
    11<?php 
     2# -- BEGIN LICENSE BLOCK --------------------------------------- 
     3# 
     4# This file is part of Dotclear 2. 
     5# 
     6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear 
     7# Licensed under the GPL version 2.0 license. 
     8# See LICENSE file or 
     9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
     10# 
     11# -- END LICENSE BLOCK ----------------------------------------- 
     12if (!defined('DC_ADMIN_CONTEXT')) { return; } 
    213 
    314class adminModulesList 
     
    516     public $core; 
    617     public $modules; 
    7  
    8      public static  $allow_multi_install; 
     18     public $repository; 
     19 
     20     public static $allow_multi_install = false; 
    921     public static $distributed_modules = array(); 
    1022 
    1123     protected $list_id = 'unknow'; 
     24     protected $data = array(); 
    1225 
    1326     protected $config_module = ''; 
     
    3043     protected $sort_asc = true; 
    3144 
    32      public function __construct($core, $root, $allow_multi_install=false) 
    33      { 
    34           $this->core = $core; 
    35           self::$allow_multi_install = (boolean) $allow_multi_install; 
    36           $this->setPathInfo($root); 
     45     public function __construct(dcModules $modules, $modules_root, $xml_url) 
     46     { 
     47          $this->core = $modules->core; 
     48          $this->modules = $modules; 
     49          $this->repository = new dcRepository($modules, $xml_url); 
     50 
     51          $this->setPathInfo($modules_root); 
    3752          $this->setNavSpecial(__('other')); 
    38  
    39           $this->init(); 
    40      } 
    41  
    42      protected function init() 
    43      { 
    44           return null; 
    4553     } 
    4654 
    4755     public function newList($list_id) 
    4856     { 
    49           $this->modules = array(); 
     57          $this->data = array(); 
    5058          $this->page_tab = ''; 
    5159          $this->list_id = $list_id; 
     
    124132          $query = $this->getSearchQuery(); 
    125133 
    126           if (empty($this->modules) && $query === null) { 
     134          if (empty($this->data) && $query === null) { 
    127135               return $this; 
    128136          } 
     
    140148               echo  
    141149               '<p class="message">'.sprintf( 
    142                     __('Found %d result for search "%s":', 'Found %d results for search "%s":', count($this->modules)),  
    143                     count($this->modules), html::escapeHTML($query) 
     150                    __('Found %d result for search "%s":', 'Found %d results for search "%s":', count($this->data)),  
     151                    count($this->data), html::escapeHTML($query) 
    144152                    ). 
    145153               '</p>'; 
     
    163171     public function displayNavMenu() 
    164172     { 
    165           if (empty($this->modules) || $this->getSearchQuery() !== null) { 
     173          if (empty($this->data) || $this->getSearchQuery() !== null) { 
    166174               return $this; 
    167175          } 
     
    169177          # Fetch modules required field 
    170178          $indexes = array(); 
    171           foreach ($this->modules as $id => $module) { 
     179          foreach ($this->data as $id => $module) { 
    172180               if (!isset($module[$this->sort_field])) { 
    173181                    continue; 
     
    246254     public function setModules($modules) 
    247255     { 
    248           $this->modules = array(); 
     256          $this->data = array(); 
    249257          if (!empty($modules) && is_array($modules)) { 
    250258               foreach($modules as $id => $module) { 
    251                     $this->modules[$id] = self::parseModuleInfo($id, $module); 
     259                    $this->data[$id] = self::parseModuleInfo($id, $module); 
    252260               } 
    253261          } 
     
    257265     public function getModules() 
    258266     { 
    259           return $this->modules; 
     267          return $this->data; 
    260268     } 
    261269 
     
    297305     } 
    298306 
    299      public static function setDistributedModules($modules) 
    300      { 
    301           self::$distributed_modules = $modules; 
    302      } 
    303  
    304307     public static function isDistributedModule($module) 
    305308     { 
     
    361364          $sort_field = $this->getSortQuery(); 
    362365 
    363           # Sort modules by id 
     366          # Sort modules by $sort_field (default sname) 
    364367          $modules = $this->getSearchQuery() === null ? 
    365                self::sortModules($this->modules, $sort_field, $this->sort_asc) : 
    366                $this->modules; 
     368               self::sortModules($this->data, $sort_field, $this->sort_asc) : 
     369               $this->data; 
    367370 
    368371          $count = 0; 
     
    425428 
    426429               if (!empty($actions) && $this->core->auth->isSuperAdmin()) { 
     430                    $buttons = $this->getActions($id, $module, $actions); 
     431 
    427432                    echo  
    428                     '<td class="module-actions nowrap">'; 
    429  
    430                     $this->displayLineActions($id, $module, $actions); 
    431  
    432                     echo 
     433                    '<td class="module-actions nowrap">'. 
     434 
     435                    '<form action="'.$this->getPageURL().'" method="post">'. 
     436                    '<div>'. 
     437                    $this->core->formNonce(). 
     438                    form::hidden(array('module'), html::escapeHTML($id)). 
     439                    form::hidden(array('tab'), $this->page_tab). 
     440 
     441                    implode(' ', $buttons). 
     442 
     443                    '</div>'. 
     444                    '</form>'. 
     445 
    433446                    '</td>'; 
    434447               } 
     
    448461     } 
    449462 
    450      protected function displayLineActions($id, $module, $actions, $echo=true) 
     463     protected function getActions($id, $module, $actions) 
    451464     { 
    452465          $submits = array(); 
    453466 
    454           # Update (from repository) 
    455           if (in_array('update', $actions) && $this->path_writable) { 
    456                $submits[] = '<input type="submit" name="update" value="'.__('Update').'" />'; 
    457           } 
    458  
    459           # Install (form repository) 
    460           if (in_array('install', $actions) && $this->path_writable) { 
    461                $submits[] = '<input type="submit" name="install" value="'.__('Install').'" />'; 
    462           } 
    463            
    464           $tmp = $this->displayOtherLineAction($id, $module, $actions); 
    465           if (!empty($tmp) && is_array($tmp)) { 
    466                $submits = array_merge($submits, $tmp); 
    467           } 
    468  
    469           # Activate 
    470           if (in_array('deactivate', $actions) && $module['root_writable']) { 
    471                $submits[] = '<input type="submit" name="deactivate" value="'.__('Deactivate').'" class="reset" />'; 
    472           } 
    473  
    474           # Deactivate 
    475           if (in_array('activate', $actions) && $module['root_writable']) { 
    476                $submits[] = '<input type="submit" name="activate" value="'.__('Activate').'" />'; 
    477           } 
    478  
    479           # Delete 
    480           if (in_array('delete', $actions) && $this->isPathDeletable($module['root'])) { 
    481                $submits[] = '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" />'; 
    482           } 
    483  
    484           # Parse form 
    485           if (!empty($submits)) { 
    486                $res =  
    487                '<form action="'.$this->getPageURL().'" method="post">'. 
    488                '<div>'. 
    489                $this->core->formNonce(). 
    490                form::hidden(array('module'), html::escapeHTML($id)). 
    491                form::hidden(array('tab'), $this->page_tab). 
    492                implode(' ', $submits). 
    493                '</div>'. 
    494                '</form>'; 
    495  
    496                if (!$echo) { 
    497                     return $res; 
    498                } 
    499                echo $res; 
    500           } 
    501      } 
    502  
    503      protected function displayOtherLineAction($id, $module, $actions) 
    504      { 
    505           return null; 
    506      } 
    507  
    508      public function executeAction($prefix, dcModules $modules, dcRepository $repository) 
    509      { 
    510           if (!$this->core->auth->isSuperAdmin() || !$this->isPathWritable()) { 
     467          # Use loop to keep requested order 
     468          foreach($actions as $action) { 
     469               switch($action) { 
     470 
     471                    # Deactivate 
     472                    case 'activate': if ($module['root_writable']) { 
     473                         $submits[] =  
     474                         '<input type="submit" name="activate" value="'.__('Activate').'" />'; 
     475                    } break; 
     476 
     477                    # Activate 
     478                    case 'deactivate': if ($module['root_writable']) { 
     479                         $submits[] =  
     480                         '<input type="submit" name="deactivate" value="'.__('Deactivate').'" class="reset" />'; 
     481                    } break; 
     482 
     483                    # Delete 
     484                    case 'delete': if ($this->isPathDeletable($module['root'])) { 
     485                         $submits[] =  
     486                         '<input type="submit" class="delete" name="delete" value="'.__('Delete').'" />'; 
     487                    } break; 
     488 
     489                    # Install (from repository) 
     490                    case 'install': if ($this->path_writable) { 
     491                         $submits[] =  
     492                         '<input type="submit" name="install" value="'.__('Install').'" />'; 
     493                    } break; 
     494 
     495                    # Update (from repository) 
     496                    case 'update': if ($this->path_writable) { 
     497                         $submits[] =  
     498                         '<input type="submit" name="update" value="'.__('Update').'" />'; 
     499                    } break; 
     500               } 
     501          } 
     502 
     503          return $submits; 
     504     } 
     505 
     506     public function doActions($prefix) 
     507     { 
     508          if (empty($_POST) || !empty($_REQUEST['conf'])  
     509          || !$this->core->auth->isSuperAdmin() || !$this->isPathWritable()) { 
    511510               return null; 
    512511          } 
     
    519518               if (!empty($_POST['activate'])) { 
    520519 
    521                     $enabled = $modules->getDisabledModules(); 
     520                    $enabled = $this->modules->getDisabledModules(); 
    522521                    if (!isset($enabled[$id])) { 
    523522                         throw new Exception(__('No such module.')); 
     
    527526                    $this->core->callBehavior($prefix.'BeforeActivate', $id); 
    528527 
    529                     $modules->activateModule($id); 
     528                    $this->modules->activateModule($id); 
    530529 
    531530                    # --BEHAVIOR-- moduleAfterActivate 
     
    537536               elseif (!empty($_POST['deactivate'])) { 
    538537 
    539                     if (!$modules->moduleExists($id)) { 
     538                    if (!$this->modules->moduleExists($id)) { 
    540539                         throw new Exception(__('No such module.')); 
    541540                    } 
    542541 
    543                     $module = $modules->getModules($id); 
     542                    $module = $this->modules->getModules($id); 
    544543                    $module['id'] = $id; 
    545544 
     
    551550                    $this->core->callBehavior($prefix.'BeforeDeactivate', $module); 
    552551 
    553                     $modules->deactivateModule($id); 
     552                    $this->modules->deactivateModule($id); 
    554553 
    555554                    # --BEHAVIOR-- moduleAfterDeactivate 
     
    561560               elseif (!empty($_POST['delete'])) { 
    562561 
    563                     $disabled = $modules->getDisabledModules(); 
     562                    $disabled = $this->modules->getDisabledModules(); 
    564563                    if (!isset($disabled[$id])) { 
    565564 
    566                          if (!$modules->moduleExists($id)) { 
     565                         if (!$this->modules->moduleExists($id)) { 
    567566                              throw new Exception(__('No such module.')); 
    568567                         } 
    569568 
    570                          $module = $modules->getModules($id); 
     569                         $module = $this->modules->getModules($id); 
    571570                         $module['id'] = $id; 
    572571 
     
    578577                         $this->core->callBehavior($prefix.'BeforeDelete', $module); 
    579578 
    580                          $modules->deleteModule($id); 
     579                         $this->modules->deleteModule($id); 
    581580 
    582581                         # --BEHAVIOR-- moduleAfterDelete 
     
    584583                    } 
    585584                    else { 
    586                          $modules->deleteModule($id, true); 
     585                         $this->modules->deleteModule($id, true); 
    587586                    } 
    588587 
    589588                    http::redirect($this->getPageURL('msg=delete')); 
     589               } 
     590 
     591               elseif (!empty($_POST['install'])) { 
     592 
     593                    $updated = $this->repository->get(); 
     594                    if (!isset($updated[$id])) { 
     595                         throw new Exception(__('No such module.')); 
     596                    } 
     597 
     598                    $module = $updated[$id]; 
     599                    $module['id'] = $id; 
     600 
     601                    $dest = $this->getPath().'/'.basename($module['file']); 
     602 
     603                    # --BEHAVIOR-- moduleBeforeAdd 
     604                    $this->core->callBehavior($prefix.'BeforeAdd', $module); 
     605 
     606                    $ret_code = $this->repository->process($module['file'], $dest); 
     607 
     608                    # --BEHAVIOR-- moduleAfterAdd 
     609                    $this->core->callBehavior($prefix.'AfterAdd', $module); 
     610 
     611                    http::redirect($this->getPageURL('msg='.($ret_code == 2 ? 'update' : 'install'))); 
    590612               } 
    591613 
     
    597619                    } 
    598620 
    599                     if (!$modules->moduleExists($id)) { 
     621                    if (!$this->modules->moduleExists($id)) { 
    600622                         throw new Exception(__('No such module.')); 
    601623                    } 
     
    617639                    $this->core->callBehavior($prefix.'BeforeUpdate', $module); 
    618640 
    619                     $repository->process($module['file'], $dest); 
     641                    $this->repository->process($module['file'], $dest); 
    620642 
    621643                    # --BEHAVIOR-- moduleAfterUpdate 
     
    623645 
    624646                    http::redirect($this->getPageURL('msg=upadte')); 
    625                } 
    626  
    627                elseif (!empty($_POST['install'])) { 
    628  
    629                     $updated = $repository->get(); 
    630                     if (!isset($updated[$id])) { 
    631                          throw new Exception(__('No such module.')); 
    632                     } 
    633  
    634                     $module = $updated[$id]; 
    635                     $module['id'] = $id; 
    636  
    637                     $dest = $this->getPath().'/'.basename($module['file']); 
    638  
    639                     # --BEHAVIOR-- moduleBeforeAdd 
    640                     $this->core->callBehavior($prefix.'BeforeAdd', $module); 
    641  
    642                     $ret_code = $repository->process($module['file'], $dest); 
    643  
    644                     # --BEHAVIOR-- moduleAfterAdd 
    645                     $this->core->callBehavior($prefix.'AfterAdd', $module); 
    646  
    647                     http::redirect($this->getPageURL('msg='.($ret_code == 2 ? 'update' : 'install'))); 
    648647               } 
    649648          } 
     
    667666                    $url = urldecode($_POST['pkg_url']); 
    668667                    $dest = $this->getPath().'/'.basename($url); 
    669                     $repository->download($url, $dest); 
     668                    $this->repository->download($url, $dest); 
    670669               } 
    671670 
     
    673672               $this->core->callBehavior($prefix.'BeforeAdd', null); 
    674673 
    675                $ret_code = $repository->install($dest); 
     674               $ret_code = $this->repository->install($dest); 
    676675 
    677676               # --BEHAVIOR-- moduleAfterAdd 
     
    681680          } 
    682681 
    683           return $this->executeOtherAction($prefix, $modules, $repository); 
    684      } 
    685  
    686      /** 
    687       *  
    688       * Way for child class to execute their own actions  
    689       * whitout rewriting all standard actions. 
    690       */ 
    691      protected function executeOtherAction($prefix, dcModules $modules, dcRepository $repository) 
    692      { 
    693682          return null; 
    694683     } 
     
    740729      * 
    741730      */ 
    742      public function setConfigurationFile(dcModules $modules, $id=null) 
     731     public function setConfigurationFile($id=null) 
    743732     { 
    744733          if (empty($_REQUEST['conf']) || empty($_REQUEST['module']) && !$id) { 
     
    750739          } 
    751740 
    752           if (!$modules->moduleExists($id)) { 
     741          if (!$this->modules->moduleExists($id)) { 
    753742               $core->error->add(__('Unknow module ID')); 
    754743               return false; 
    755744          } 
    756745 
    757           $module = $modules->getModules($id); 
     746          $module = $this->modules->getModules($id); 
    758747          $module = self::parseModuleInfo($id, $module); 
    759748          $file = path::real($module['root'].'/_config.php'); 
     
    842831          # Sort modules by id 
    843832          $modules = $this->getSearchQuery() === null ? 
    844                self::sortModules($this->modules, $sort_field, $this->sort_asc) : 
    845                $this->modules; 
     833               self::sortModules($this->data, $sort_field, $this->sort_asc) : 
     834               $this->data; 
    846835 
    847836          $res = ''; 
     
    861850 
    862851               $current = $this->core->blog->settings->system->theme == $id; 
    863  
    864                if (preg_match('#^http(s)?://#', $this->core->blog->settings->system->themes_url)) { 
    865                     $theme_url = http::concatURL($this->core->blog->settings->system->themes_url, '/'.$id); 
    866                } else { 
    867                     $theme_url = http::concatURL($this->core->blog->url, $this->core->blog->settings->system->themes_url.'/'.$id); 
    868                } 
     852               $distrib = self::isDistributedModule($id) ? ' dc-box' : ''; 
     853 
     854               $theme_url = preg_match('#^http(s)?://#', $this->core->blog->settings->system->themes_url) ? 
     855                    http::concatURL($this->core->blog->settings->system->themes_url, '/'.$id) : 
     856                    http::concatURL($this->core->blog->url, $this->core->blog->settings->system->themes_url.'/'.$id); 
    869857 
    870858               $has_conf = file_exists(path::real($this->core->blog->themes_path.'/'.$id).'/_config.php'); 
     
    877865 
    878866               $line =  
    879                '<div class="box '.($current ? 'medium current-theme' : 'small theme').'">'; 
     867               '<div class="box '.($current ? 'medium current-theme' : 'small theme').$distrib.'">'; 
    880868 
    881869               if (in_array('name', $cols)) { 
     
    899887 
    900888                    $line .=  
    901                     '<div class="module-sshot"><img src="'.$sshot.'" alt="'.__('screenshot.').'" /></div>'; 
     889                    '<div class="module-sshot"><img src="'.$sshot.'" alt="'. 
     890                    sprintf(__('%s screenshot.'), html::escapeHTML($module['name'])).'" /></div>'; 
    902891               } 
    903892 
     
    930919                         '<span class="module-parent-missing">'.sprintf(__('(requires "%s")'),html::escapeHTML($parent)).'</span> '; 
    931920                    } 
     921               } 
     922 
     923               if (in_array('version', $cols)) { 
     924                    $line .=  
     925                    '<span class="module-version">'.sprintf(__('version %s'),html::escapeHTML($module['version'])).'</span> '; 
     926               } 
     927 
     928               $has_details = in_array('details', $cols) && !empty($module['details']); 
     929               $has_support = in_array('support', $cols) && !empty($module['support']); 
     930               if ($has_details || $has_support) { 
     931                    $line .= 
     932                    '<span class="mod-more">'.__('Help:').' '; 
     933 
     934                    if ($has_details) { 
     935                         $line .=  
     936                         '<a class="module-details" href="'.$module['details'].'">'.__('Details').'</a>'; 
     937                    } 
     938 
     939                    if ($has_support) { 
     940                         $line .=  
     941                         ' - <a class="module-support" href="'.$module['support'].'">'.__('Support').'</a>'; 
     942                    } 
     943 
     944                    $line .= 
     945                    '</span>'; 
    932946               } 
    933947 
     
    958972 
    959973               # _POST actions 
    960                if (!empty($actions) && $this->core->auth->isSuperAdmin()) { 
     974               if (!empty($actions)) { 
    961975                    $line .= 
    962                     $this->displayLineActions($id, $module, $actions, false); 
     976                    '<form action="'.$this->getPageURL().'" method="post">'. 
     977                    '<div>'. 
     978                    $this->core->formNonce(). 
     979                    form::hidden(array('module'), html::escapeHTML($id)). 
     980                    form::hidden(array('tab'), $this->page_tab). 
     981 
     982                    implode(' ', $this->getActions($id, $module, $actions)). 
     983  
     984                    '</div>'. 
     985                    '</form>'; 
    963986               } 
    964987 
     
    9831006     } 
    9841007 
    985      protected function displayOtherLineAction($id, $module, $actions) 
     1008     protected function getActions($id, $module, $actions) 
    9861009     { 
    9871010          $submits = array(); 
    9881011 
    9891012          $this->core->blog->settings->addNamespace('system'); 
    990           if ($id == $this->core->blog->settings->system->theme) { 
    991                return null; 
    992           } 
    993  
    994           # Select theme to use on curent blog 
    995           if (in_array('select', $actions) && $this->path_writable) { 
    996                $submits[] = '<input type="submit" name="select" value="'.__('Choose').'" />'; 
    997           } 
    998  
    999           return $submits; 
    1000      } 
    1001       
    1002      protected function executeOtherAction($prefix, dcModules $modules, dcRepository $repository) 
    1003      { 
    1004           # Select theme to use on curent blog 
    1005           if (!empty($_POST['module']) && !empty($_POST['select'])) { 
    1006                $id = $_POST['module']; 
    1007  
    1008                if (!$modules->moduleExists($id)) { 
    1009                     throw new Exception(__('No such module.')); 
    1010                } 
    1011  
    1012                $this->core->blog->settings->addNamespace('system'); 
    1013                $this->core->blog->settings->system->put('theme',$id); 
    1014                $this->core->blog->triggerBlog(); 
    1015  
    1016                http::redirect($this->getPageURL('msg=select').'#themes'); 
    1017           } 
    1018  
    1019           return null; 
     1013          if ($id != $this->core->blog->settings->system->theme) { 
     1014 
     1015               # Select theme to use on curent blog 
     1016               if (in_array('select', $actions) && $this->path_writable) { 
     1017                    $submits[] = '<input type="submit" name="select" value="'.__('Choose').'" />'; 
     1018               } 
     1019          } 
     1020 
     1021          return array_merge( 
     1022               $submits, 
     1023               parent::getActions($id, $module, $actions) 
     1024          ); 
     1025     } 
     1026 
     1027     public function doActions($prefix) 
     1028     { 
     1029          if (!empty($_POST) && empty($_REQUEST['conf']) && $this->isPathWritable()) { 
     1030 
     1031               # Select theme to use on curent blog 
     1032               if (!empty($_POST['module']) && !empty($_POST['select'])) { 
     1033                    $id = $_POST['module']; 
     1034 
     1035                    if (!$this->modules->moduleExists($id)) { 
     1036                         throw new Exception(__('No such module.')); 
     1037                    } 
     1038 
     1039                    $this->core->blog->settings->addNamespace('system'); 
     1040                    $this->core->blog->settings->system->put('theme',$id); 
     1041                    $this->core->blog->triggerBlog(); 
     1042 
     1043                    http::redirect($this->getPageURL('msg=select').'#themes'); 
     1044               } 
     1045          } 
     1046 
     1047          return parent::doActions($prefix); 
    10201048     } 
    10211049} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map