> ------------------------------------------------------------------------------ See main.inc.php for release information MyPolls_Install : classe to manage plugin install --------------------------------------------------------------------------- */ @include_once('mypolls_root.class.inc.php'); @include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/tables.class.inc.php'); /* mypolls class for install process */ class MyPolls_Install extends MyPolls_root { private $tablef; private $exportfile; public function MyPolls_Install($prefixeTable, $filelocation) { parent::__construct($prefixeTable, $filelocation); $this->tablef= new manage_tables($this->tables); $this->tablev1x= new manage_tables( array( $this->prefixeTable.'MyPolls_polls', $this->prefixeTable.'MyPolls_polls_by_user' ) ); $this->exportfile=dirname($this->filelocation).'/'.$this->plugin_name_files.'.sql'; } /* function for installation process return true if install process is ok, otherwise false */ public function install() { $tables_def=array( "CREATE TABLE `".$this->tables['polls']."` ( `id` int(11) NOT NULL auto_increment, `date` datetime NOT NULL default '0000-00-00 00:00:00', `allowed_users` varchar(35) NOT NULL default 'guest/generic/normal/', `allowed_groups` varchar(250) NOT NULL default '', `visible` char(1) NOT NULL default 'n', `enabled` char(1) NOT NULL default 'n', `total_votes` int(11) NOT NULL default '0', `last_vote` datetime NOT NULL default '0000-00-00 00:00:00', `visit_without_vote` int(11) NOT NULL default '0', `visit_after_vote` int(11) NOT NULL default '0', `allow_comment` char(1) NOT NULL default 'n', `show_comments` char(1) NOT NULL default 'n', `color_bars` varchar(6) NOT NULL default '', `nb_questions` smallint(6) NOT NULL default '1', `display_type` enum('mono','multi') NOT NULL default 'mono', `default_lang` varchar(25) NOT NULL default '', `counter_questions` int(10) unsigned NOT NULL default '0', `nb_comments` int(11) NOT NULL default '0', `public_results` char(1) NOT NULL default 'y', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1", "CREATE TABLE `".$this->tables['polls_lang']."` ( `id_poll` int(11) NOT NULL default '0', `lang` varchar(25) NOT NULL default '', `title` varchar(250) NOT NULL default '', `description` text NOT NULL, `after_vote_text` text NOT NULL, PRIMARY KEY (`id_poll`,`lang`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1", "CREATE TABLE `".$this->tables['polls_questions']."` ( `id_poll` int(11) NOT NULL default '0', `question_num` int(11) NOT NULL default '1', `question_type` enum('mono','multi','order') NOT NULL default 'mono', `multi_max_answers` smallint(6) NOT NULL default '2', `counter_answers` int(10) unsigned NOT NULL default '0', `nb_votes` int(11) NOT NULL default '0', PRIMARY KEY (`id_poll`,`question_num`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1", "CREATE TABLE `".$this->tables['polls_questions_lang']."` ( `id_poll` int(11) NOT NULL default '0', `id_question` int(11) NOT NULL default '0', `lang` varchar(25) NOT NULL default '', `description` text NOT NULL, PRIMARY KEY (`id_poll`,`id_question`,`lang`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1", "CREATE TABLE `".$this->tables['polls_answers']."` ( `id_poll` int(11) NOT NULL default '0', `id_question` int(11) NOT NULL default '0', `answer_num` int(11) NOT NULL default '0', `nb_votes` int(11) NOT NULL default '0', `frequency` int(11) NOT NULL default '0', `ans_order` smallint(6) NOT NULL default '0', PRIMARY KEY (`id_poll`,`id_question`,`answer_num`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1", "CREATE TABLE `".$this->tables['polls_answers_lang']."` ( `id_poll` int(11) NOT NULL default '0', `id_question` int(11) NOT NULL default '0', `id_answer` int(11) NOT NULL default '0', `lang` varchar(25) NOT NULL default '', `answer` varchar(250) NOT NULL default '', `nb_votes` int(11) NOT NULL default '0', PRIMARY KEY (`id_poll`,`id_question`,`id_answer`,`lang`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1", "CREATE TABLE `".$this->tables['polls_votes']."` ( `poll_id` INTEGER NOT NULL DEFAULT '0', `user_id` VARCHAR(15) NOT NULL DEFAULT '2', `date` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', `user_comment` TEXT NOT NULL, PRIMARY KEY(`poll_id`, `user_id`) ) ENGINE = MYISAM DEFAULT CHARSET=latin1" ); //$table_def array //if present, try to import backup //if backup file $import=$this->tablef->import($this->exportfile); if($import['errors']<0) { $result=$this->tablef->create_tables($tables_def); if($result) { $result=$this->add_enum_to_history(); } return($result); } else { return($this->add_enum_to_history()); } } /* function for uninstall process */ public function uninstall() { $this->tablef->export($this->exportfile); $this->delete_config(); $this->tablef->drop_tables(); } public function activate() { global $template; $upgrade=''; if($this->tablev1x->tables_exists()) { //tables exists, upgrade from MyPolls v1.x $upgrade='1.x'; //step 1: rename tables if($this->tablev1x->rename_tables( array( $this->prefixeTable.'MyPolls_polls' => $this->prefixeTable.$this->plugin_name_files.'_polls_v1x', $this->prefixeTable.'MyPolls_polls_by_user' => $this->prefixeTable.$this->plugin_name_files.'_polls_by_user_v1x' ) )!==true) { return('mypolls_cant_rename_old_table'); } } switch($upgrade) { case '1.x': //create current version tables $this->install(); //load v1.x polls into current version tables if($this->migrate_from_v1x()) { $this->tablev1x->drop_tables(); } else { $this->tablef->drop_tables(); $this->tablev1x->rename_tables( array( $this->prefixeTable.$this->plugin_name_files.'_polls_v1x' => $this->prefixeTable.'MyPolls_polls', $this->prefixeTable.$this->plugin_name_files.'_polls_by_user_v1x' => $this->prefixeTable.'MyPolls_polls_by_user' ) ); return('mypolls_cant_migrate_old_table'); } break; /* //code for futures versions //from 2.x to 3.x, no tables upgrades needed case '2.x': $this->tablef->update_tables_fields(false); break; */ } $this->init_config(); $this->load_config(); $this->save_config(); $template->delete_compiled_templates(); return(''); } public function deactivate() { } /* specific function to obtain field 'section' enumeration */ private function get_section_enum($add) { $sql = 'SHOW COLUMNS FROM '.HISTORY_TABLE.' LIKE "section"'; $result=pwg_query($sql); if($result) { $row = mysql_fetch_array($result); $list=substr($row['Type'], 5, strlen($row['Type'])-6); $returned=explode(',', $list); if((strpos($list, "'$add'")===false)&&($add!='')) { array_push($returned, "'$add'"); } return($returned); } } private function add_enum_to_history() { $enums=implode(',', $this->get_section_enum($this->section_name)); $sql="ALTER TABLE ".HISTORY_TABLE." CHANGE `section` `section` ENUM (".$enums.") ;"; $result=pwg_query($sql); return($result); } /* do migration from v1.x tables to current version tables */ private function migrate_from_v1x() { global $user; /* language for migration is user language */ $sql="SELECT * FROM ".$this->prefixeTable.$this->plugin_name_files.'_polls_v1x'; $result=pwg_query($sql); if($result) { while($row=mysql_fetch_array($result)) { // create poll $sql="INSERT INTO ".$this->tables['polls']." VALUES ( '0', '".$row['date']."', '".$row['allowed_users']."', '".$row['allowed_groups']."', '".substr($row['visible'],0,1)."', '".substr($row['enabled'],0,1)."', '".$row['total']."', '0000-00-00 00:00:00', '".$row['visit_without_vote']."', '".$row['visit_after_vote']."', 'n', 'n', '".$this->my_config['mypolls_color_bars']."', '1', 'mono', '".$user['language']."', '1', '0', 'n' )"; if(pwg_query($sql)) { $id=mysql_insert_id(); // create poll's nfo $sql="INSERT INTO ".$this->tables['polls_lang']." VALUES( '".$id."', '".$user['language']."', '".addslashes($row['title'])."', '', '".addslashes($row['after_vote_text'])."' )"; if(pwg_query($sql)) { //create poll's question's answers (before question to count nb answers) $nbanswers=0; for($i=1;$i<=10;$i++) { if($row['c'.$i.'_text']!="") { $nbanswers++; //answer $sql="INSERT INTO ".$this->tables['polls_answers']." VALUES ( '".$id."', '1', '".$i."', '".$row['c'.$i.'_choose']."', '0', '0' )"; if(!pwg_query($sql)) { return(false); } //answer's lang $sql="INSERT INTO ".$this->tables['polls_answers_lang']." VALUES ( '".$id."', '1', '".$i."', '".$user['language']."', '".addslashes($row['c'.$i.'_text'])."', '".addslashes($row['c'.$i.'_choose'])."' )"; if(!pwg_query($sql)) { return(false); } } } //create question $sql="INSERT INTO ".$this->tables['polls_questions']." VALUES ( '".$id."', '1', 'mono', '2', '".$nbanswers."', '".$row['total']."' )"; if(pwg_query($sql)) { $sql="INSERT INTO ".$this->tables['polls_questions_lang']." VALUES ( '".$id."', '1', '".$user['language']."', '".addslashes($row['description'])."' )"; if(!pwg_query($sql)) { return(false); } } else { return(false); } } else { return(false); } //create user's list votes and comments $sql="SELECT * FROM ".$this->prefixeTable.$this->plugin_name_files."_polls_by_user_v1x WHERE poll_id = '".$row['id']."'"; $result2=pwg_query($sql); if($result2) { while($row2=mysql_fetch_assoc($result2)) { $sql="INSERT INTO ".$this->tables['polls_votes']." VALUES ( '".$id."', '".$row2['user_id']."', '".$row2['date']."', '".addslashes($row2['user_comment'])."' )"; if(!pwg_query($sql)) { return(false); } } } } else { return(false); } } } else { return(false); } return(true); } } //class ?>