From f8c4c1d0e767f83106e2ea450fcdffd251f8f853 Mon Sep 17 00:00:00 2001 From: Dany De Bontridder Date: Tue, 18 Nov 2014 01:34:05 +0100 Subject: [PATCH] Task #1052 - Automatic integration of plugin Plugin management --- include/cfgplugin.inc.php | 154 ++++++++++++++++++++++++++++++++++++ include/class_extension.php | 119 ++++++++++++++++++++++++++-- include/menu.inc.php | 3 +- sql/upgrade.sql | 5 ++ 4 files changed, 275 insertions(+), 6 deletions(-) create mode 100644 include/cfgplugin.inc.php diff --git a/include/cfgplugin.inc.php b/include/cfgplugin.inc.php new file mode 100644 index 000000000..41134598f --- /dev/null +++ b/include/cfgplugin.inc.php @@ -0,0 +1,154 @@ + + +if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis'); +require_once 'class_extension.php'; + +/** + * @file + * @brief Automatic installation of plugins and activation + */ +global $cn; + +/****************************************************************************** + * Scan the plugin folder and file in each subfolder a property file and + * store them into an array a_plugin + ****************************************************************************** + */ +$dirscan=scandir('../include/ext/'); +$nb_dirscan=count($dirscan); +$a_plugin=array(); +for ($e=0;$e<$nb_dirscan;$e++) { + if ($dirscan[$e] != '.' && $dirscan[$e]!='..' && is_dir('../include/ext/'.$dirscan[$e])) { + $dir_plugin=$dirscan[$e]; + if (file_exists('../include/ext/'.$dir_plugin.'/plugin.xml')) { + + $extension=Extension::read_definition('../include/ext/'.$dir_plugin.'/plugin.xml'); + for ($i=0;$iget_array('select p_id,p_name from profile where p_id > 0 order by p_name'); +$nb_profile=count($a_profile); +/****************************************************************************** + * save + ******************************************************************************/ +if ( isset ($_POST['save_plugin'])){ + // retrieve array of plugin + $plugin=HtmlInput::default_value_post('plugin', array()); + // for each extension + for ($i=0;$i<$nb_plugin;$i++) { + + $code=$a_plugin[$i]->me_code; + // for each profile + for ($e=0;$e<$nb_profile;$e++) + { + $profile=$a_profile[$e]['p_id']; + if ( isset ($plugin[$code][$profile])) { + // insert or update into db + $count = $cn->get_value("select count(*) from menu_ref where me_code=$1", array($code)); + if ( $count == 0 ) { + $a_plugin[$i]->insert(); + } + $a_plugin[$i]->insert_profile_menu($profile,'EXT'); + } else { + // delete + $a_plugin[$i]->remove_from_profile_menu ($profile); + } + } + } +} +/****************************************************************************** + * Display the Plugin and for each profile were it is installed or not + ******************************************************************************/ + + +?> +
+ +
+ + + + + + + + + get_array("select distinct + p_id,p_name, + (select count(*) from profile_menu as a where a.p_id=b.p_id and me_code=$1 )+ + (select count(*) from menu_ref as c join profile_menu as d on (d.me_code=c.me_code) where d.p_id=b.p_id and me_file=$2 ) as cnt + from profile as b + where p_id > 0 + order by p_name",array($a_plugin[$e]->me_code,$a_plugin[$e]->me_file)); + + $class=($e%2==0)?'odd':'even'; + ?> + + + + + + + + + +
+ me_code); ?> + + me_menu); ?> + + me_description); ?> + + me_file); ?> + + + + + me_code.']['.$a_profile[$w]['p_id'].']'); + if ($a_profile[$w]['cnt']>0) $a->selected=true; + echo $a->input(); + echo $a_profile[$w]['p_name']; + ?> + + +
+ +
+
+ diff --git a/include/class_extension.php b/include/class_extension.php index 6188e6639..cadce76f6 100644 --- a/include/class_extension.php +++ b/include/class_extension.php @@ -36,6 +36,9 @@ * - filepath (extension.ex_file) */ require_once 'class_menu_ref_sql.php'; +include_once 'class_profile_sql.php'; +require_once 'class_menu_ref.php'; +require_once 'class_profile_menu.php'; class Extension extends Menu_Ref_sql { @@ -96,6 +99,53 @@ class Extension extends Menu_Ref_sql } Extension::check_plugin_version(); } + /** + * insert into the table profile_menu for the given profile id and depending + * of the module $p_module + * @global type $cn + * @param type $p_id profile.p_id + * @param type $p_module menu_ref.me_code + * @throws Exception 10 : profile absent 20 module absent + */ + function insert_profile_menu($p_id=1,$p_module='EXT') + { + global $cn; + //profile exists ? + $profile=new Profile_sql($cn,$p_id); + if ( $profile->p_id != $p_id) { + throw new Exception(_('Profil inexistant'),10); + } + // Menu exists + $module=new Menu_Ref($cn,$p_module); + if ($module->me_code==null) { + throw new Exception(_('Module inexistant'),20); + } + + $profil_menu=new Profile_Menu($cn); + $profil_menu->me_code=$this->me_code; + $profil_menu->me_code_dep=$p_module; + $profil_menu->p_type_display='S'; + $profil_menu->p_id=$p_id; + $cnt=$profil_menu->count(' where p_id=$1 and me_code = $2',array($p_id,$this->me_code)); + if ( $cnt==0) { + $profil_menu->insert(); + } + + + } + function remove_from_profile_menu($p_id) + { + global $cn; + + $cn->exec_sql('delete from profile_menu where (me_code = $1 or me_code in (select me_code from menu_ref where me_file=$2)) and p_id=$3',array($this->me_code,$this->me_file,$p_id)); + + } + /** + * Insert a plugin into the given profile, by default always insert into EXT + * + * @param type $p_id profile.p_id + * @throws Exception if duplicate or error db + */ function insert_plugin() { try @@ -109,11 +159,6 @@ class Extension extends Menu_Ref_sql throw new Exception("Doublon"); $this->me_type = 'PL'; $this->insert(); - /** - * insert into default profile - */ - $this->cn->exec_sql("insert into profile_menu(me_code,me_code_dep,p_type_display,p_id) - values ($1,$2,$3,$4)",array($this->me_code,'EXT','S',1)); $this->cn->commit(); } catch (Exception $exc) @@ -181,5 +226,69 @@ class Extension extends Menu_Ref_sql } } } + /** + * Check that the xml contains all the needed information to change them into + * a extension, the exception code is 0 if the element is optional + * @brief Check XML. + * @param SimpleXMLElement $xml + * @throws Exception + */ + function check_xml(SimpleXMLElement $xml) + { + try { + if ( !isset ($xml->plugin)) throw new Exception(_('Manque plugin'),1); + $nb_plugin=count($xml->plugin); + + for ($i=0;$i<$nb_plugin;$i++) + { + if ( !isset ($xml->plugin[$i]->name)) throw new Exception(_('Manque nom'),1); + if ( !isset ($xml->plugin[$i]->description)) throw new Exception(_('Manque description'),0); + if ( !isset ($xml->plugin[$i]->code)) throw new Exception(_('Manque code'),1); + if ( !isset ($xml->plugin[$i]->author)) throw new Exception(_('Manque auteur'),0); + if ( !isset ($xml->plugin[$i]->root)) throw new Exception(_('Manque répertoire racine'),1); + if ( !isset ($xml->plugin[$i]->file)) throw new Exception(_('Manque fichier à inclure'),1); + } + } catch (Exception $ex) { + throw $ex; + } + } + /** + * Parse a XML file to complete an array of extension objects + * @brief Create extension from XML. + * @param type $p_file filename + * @return array of Extension + */ + static function read_definition($p_file) + { + global $cn; + $dom=new DomDocument('1.0'); + $dom->load($p_file); + $xml=simplexml_import_dom($dom); + $nb_plugin=count($xml->plugin); + $a_extension=array(); + for ($i=0;$i<$nb_plugin;$i++) + { + + $extension=new Extension($cn); + try { + $extension->check_xml($xml); + } catch (Exception $ex) { + echo_warning($e->getMessage()); + if ( $ex->getCode()==1) { + continue; + } + + } + $extension->me_file=trim($xml->plugin[$i]->root).'/'.trim($xml->plugin[$i]->file); + $extension->me_code=trim($xml->plugin[$i]->code); + $extension->me_description=(isset ($xml->plugin[$i]->description))?$xml->plugin[$i]->description:""; + $extension->me_description_etendue=($xml->plugin[$i]->author)?$xml->plugin[$i]->author:""; + $extension->me_type='PL'; + $extension->me_menu=$xml->plugin[$i]->name; + $extension->me_parameter='plugin_code='.trim($xml->plugin[$i]->code); + $a_extension[]=clone $extension; + } + return $a_extension; + } } diff --git a/include/menu.inc.php b/include/menu.inc.php index b0420a8f4..cbc769d5a 100644 --- a/include/menu.inc.php +++ b/include/menu.inc.php @@ -46,6 +46,7 @@ if ( isset($_POST['save_plugin'])) $plugin->me_description=$me_description; $plugin->me_parameter='plugin_code='.$me_code; $plugin->insert_plugin(); + $plugin->insert_profile_module(); } /** * if post update then we update @@ -138,7 +139,7 @@ if ( $iselect->selected != '') { $sql="where me_type='".sql_string($_REQUEST['p_type'])."' "; } -$menu=new Menu_Ref_sql(); +$menu=new Menu_Ref_sql($cn); $ret=$menu->seek($sql.$order); ?>
Recherche diff --git a/sql/upgrade.sql b/sql/upgrade.sql index 7421d8033..f236c2fbe 100644 --- a/sql/upgrade.sql +++ b/sql/upgrade.sql @@ -24,6 +24,11 @@ INSERT INTO menu_ref(me_code, me_menu, me_file, me_type,me_description_etendue insert into profile_menu(me_code,p_id,p_type_display,pm_default,me_code_dep,p_order) values ('ANCKEY',1,'E',0,'ANC',15); insert into profile_menu(me_code,p_id,p_type_display,pm_default,me_code_dep,p_order) values ('ANCKEY',2,'E',0,'ANC',15); +INSERT INTO menu_ref(me_code, me_menu, me_file, me_type,me_description_etendue)VALUES ('CFGPLUGIN', 'Configuration extension', 'cfgplugin.inc.php','ME','Permet d''installer et d''activer facilement des extensions'); + +insert into profile_menu(me_code,p_id,p_type_display,pm_default,me_code_dep,p_order) values ('CFGPLUGIN',1,'E',0,'PARAM',15); +insert into profile_menu(me_code,p_id,p_type_display,pm_default,me_code_dep,p_order) values ('CFGPLUGIN',2,'E',0,'PARAM',15); + create table key_distribution ( kd_id serial primary key, kd_name text,