source: extensions/mypolls/mypolls_install.class.inc.php @ 27153

Last change on this file since 27153 was 3397, checked in by grum, 15 years ago

Add plugin MyPolls - this release is not published in PEM (functionnal but not tested yet...)

  • Property svn:executable set to *
File size: 12.5 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : MyPolls.2
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.dnsalias.com
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  MyPolls_Install : classe to manage plugin install
13
14  --------------------------------------------------------------------------- */
15  @include_once('mypolls_root.class.inc.php');
16  @include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/tables.class.inc.php');
17
18
19  /* mypolls class for install process */
20  class MyPolls_Install extends MyPolls_root
21  {
22    private $tablef;
23    private $exportfile;
24
25    public function MyPolls_Install($prefixeTable, $filelocation)
26    {
27      parent::__construct($prefixeTable, $filelocation);
28      $this->tablef= new manage_tables($this->tables);
29      $this->tablev1x= new manage_tables(
30        array(
31          $this->prefixeTable.'MyPolls_polls',
32          $this->prefixeTable.'MyPolls_polls_by_user'
33        )
34      );
35      $this->exportfile=dirname($this->filelocation).'/'.$this->plugin_name_files.'.sql';
36    }
37
38    /*
39        function for installation process
40        return true if install process is ok, otherwise false
41    */ 
42    public function install()
43    {
44
45      $tables_def=array(
46"CREATE TABLE `".$this->tables['polls']."` (
47  `id` int(11) NOT NULL auto_increment,
48  `date` datetime NOT NULL default '0000-00-00 00:00:00',
49  `allowed_users` varchar(35) NOT NULL default 'guest/generic/normal/',
50  `allowed_groups` varchar(250) NOT NULL default '',
51  `visible` char(1) NOT NULL default 'n',
52  `enabled` char(1) NOT NULL default 'n',
53  `total_votes` int(11) NOT NULL default '0',
54  `last_vote` datetime NOT NULL default '0000-00-00 00:00:00',
55  `visit_without_vote` int(11) NOT NULL default '0',
56  `visit_after_vote` int(11) NOT NULL default '0',
57  `allow_comment` char(1) NOT NULL default 'n',
58  `show_comments` char(1) NOT NULL default 'n',
59  `color_bars` varchar(6) NOT NULL default '',
60  `nb_questions` smallint(6) NOT NULL default '1',
61  `display_type` enum('mono','multi') NOT NULL default 'mono',
62  `default_lang` varchar(25) NOT NULL default '',
63  `counter_questions` int(10) unsigned NOT NULL default '0',
64  `nb_comments` int(11) NOT NULL default '0',
65  `public_results` char(1) NOT NULL default 'y',
66PRIMARY KEY  (`id`)
67) ENGINE=MyISAM DEFAULT CHARSET=latin1",
68
69"CREATE TABLE `".$this->tables['polls_lang']."` (
70  `id_poll` int(11) NOT NULL default '0',
71  `lang` varchar(25) NOT NULL default '',
72  `title` varchar(250) NOT NULL default '',
73  `description` text NOT NULL,
74  `after_vote_text` text NOT NULL,
75  PRIMARY KEY  (`id_poll`,`lang`)
76) ENGINE=MyISAM DEFAULT CHARSET=latin1",
77
78"CREATE TABLE `".$this->tables['polls_questions']."` (
79  `id_poll` int(11) NOT NULL default '0',
80  `question_num` int(11) NOT NULL default '1',
81  `question_type` enum('mono','multi','order') NOT NULL default 'mono',
82  `multi_max_answers` smallint(6) NOT NULL default '2',
83  `counter_answers` int(10) unsigned NOT NULL default '0',
84  `nb_votes` int(11) NOT NULL default '0',
85PRIMARY KEY  (`id_poll`,`question_num`)
86) ENGINE=MyISAM DEFAULT CHARSET=latin1",
87
88"CREATE TABLE `".$this->tables['polls_questions_lang']."` (
89  `id_poll` int(11) NOT NULL default '0',
90  `id_question` int(11) NOT NULL default '0',
91  `lang` varchar(25) NOT NULL default '',
92  `description` text NOT NULL,
93  PRIMARY KEY  (`id_poll`,`id_question`,`lang`)
94) ENGINE=MyISAM DEFAULT CHARSET=latin1",
95
96"CREATE TABLE `".$this->tables['polls_answers']."` (
97  `id_poll` int(11) NOT NULL default '0',
98  `id_question` int(11) NOT NULL default '0',
99  `answer_num` int(11) NOT NULL default '0',
100  `nb_votes` int(11) NOT NULL default '0',
101  `frequency` int(11) NOT NULL default '0',
102  `ans_order` smallint(6) NOT NULL default '0',
103PRIMARY KEY  (`id_poll`,`id_question`,`answer_num`)
104) ENGINE=MyISAM DEFAULT CHARSET=latin1",
105
106"CREATE TABLE `".$this->tables['polls_answers_lang']."` (
107  `id_poll` int(11) NOT NULL default '0',
108  `id_question` int(11) NOT NULL default '0',
109  `id_answer` int(11) NOT NULL default '0',
110  `lang` varchar(25) NOT NULL default '',
111  `answer` varchar(250) NOT NULL default '',
112  `nb_votes` int(11) NOT NULL default '0',
113  PRIMARY KEY  (`id_poll`,`id_question`,`id_answer`,`lang`)
114) ENGINE=MyISAM DEFAULT CHARSET=latin1",
115
116"CREATE TABLE `".$this->tables['polls_votes']."` (
117  `poll_id` INTEGER  NOT NULL DEFAULT '0',
118  `user_id` VARCHAR(15)  NOT NULL DEFAULT '2',
119  `date` DATETIME  NOT NULL DEFAULT '0000-00-00 00:00:00',
120  `user_comment` TEXT  NOT NULL,
121  PRIMARY KEY(`poll_id`, `user_id`)
122) ENGINE = MYISAM DEFAULT CHARSET=latin1"
123      );
124      //$table_def array
125
126      //if present, try to import backup
127      //if backup file
128      $import=$this->tablef->import($this->exportfile);
129      if($import['errors']<0)
130      {
131        $result=$this->tablef->create_tables($tables_def);
132        if($result)
133        {
134          $result=$this->add_enum_to_history();
135        }
136        return($result);
137      }
138      else
139      {
140        return($this->add_enum_to_history());
141      }
142    }
143
144
145    /*
146        function for uninstall process
147    */
148    public function uninstall()
149    {
150      $this->tablef->export($this->exportfile);
151      $this->delete_config();
152      $this->tablef->drop_tables();
153    }
154
155    public function activate()
156    {
157      global $template;
158
159      $upgrade='';
160      if($this->tablev1x->tables_exists())
161      {
162        //tables exists, upgrade from MyPolls v1.x
163        $upgrade='1.x';
164        //step 1: rename tables
165        if($this->tablev1x->rename_tables(
166          array(
167            $this->prefixeTable.'MyPolls_polls' => $this->prefixeTable.$this->plugin_name_files.'_polls_v1x',
168            $this->prefixeTable.'MyPolls_polls_by_user' => $this->prefixeTable.$this->plugin_name_files.'_polls_by_user_v1x'
169          )
170        )!==true)
171        {
172          return('mypolls_cant_rename_old_table');
173        }
174      }
175
176      switch($upgrade)
177      {
178        case '1.x':
179            //create current version tables
180            $this->install();
181            //load v1.x polls into current version tables
182            if($this->migrate_from_v1x())
183            {
184              $this->tablev1x->drop_tables();
185            }
186            else
187            {
188              $this->tablef->drop_tables();
189              $this->tablev1x->rename_tables(
190                array(
191                  $this->prefixeTable.$this->plugin_name_files.'_polls_v1x' => $this->prefixeTable.'MyPolls_polls',
192                  $this->prefixeTable.$this->plugin_name_files.'_polls_by_user_v1x' => $this->prefixeTable.'MyPolls_polls_by_user'
193                )
194              );
195              return('mypolls_cant_migrate_old_table');
196            }
197          break;
198        /*
199          //code for futures versions
200          //from 2.x to 3.x, no tables upgrades needed
201        case '2.x':
202          $this->tablef->update_tables_fields(false);
203          break;
204        */
205      }
206
207      $this->init_config();
208      $this->load_config();
209      $this->save_config();
210      $template->delete_compiled_templates(); 
211      return('');
212    }
213
214    public function deactivate()
215    {
216    }
217
218
219    /* specific function to obtain field 'section' enumeration */
220    private function get_section_enum($add)
221    {
222      $sql = 'SHOW COLUMNS FROM '.HISTORY_TABLE.' LIKE "section"';
223      $result=pwg_query($sql);
224      if($result)
225      {
226        $row = mysql_fetch_array($result);
227        $list=substr($row['Type'], 5, strlen($row['Type'])-6);
228        $returned=explode(',', $list);
229        if((strpos($list, "'$add'")===false)&&($add!='')) 
230        { 
231          array_push($returned, "'$add'"); 
232        }
233        return($returned);
234      }
235    }
236
237    private function add_enum_to_history()
238    {
239      $enums=implode(',', $this->get_section_enum($this->section_name));
240      $sql="ALTER TABLE ".HISTORY_TABLE."
241             CHANGE `section` `section`
242             ENUM (".$enums.") ;";
243      $result=pwg_query($sql);
244      return($result);
245    }
246
247
248    /*
249      do migration from v1.x tables to current version tables
250    */
251    private function migrate_from_v1x()
252    {
253      global $user;
254
255      /*
256        language for migration is user language
257      */
258      $sql="SELECT * FROM ".$this->prefixeTable.$this->plugin_name_files.'_polls_v1x';
259      $result=pwg_query($sql);
260      if($result)
261      {
262        while($row=mysql_fetch_array($result))
263        {
264          // create poll
265          $sql="INSERT INTO ".$this->tables['polls']." VALUES (
266            '0',
267            '".$row['date']."',
268            '".$row['allowed_users']."',
269            '".$row['allowed_groups']."',
270            '".substr($row['visible'],0,1)."',
271            '".substr($row['enabled'],0,1)."',
272            '".$row['total']."',
273            '0000-00-00 00:00:00',
274            '".$row['visit_without_vote']."',
275            '".$row['visit_after_vote']."',
276            'n',
277            'n',
278            '".$this->my_config['mypolls_color_bars']."',
279            '1',
280            'mono',
281            '".$user['language']."',
282            '1',
283            '0',
284            'n'
285          )";
286          if(pwg_query($sql))
287          {
288            $id=mysql_insert_id();
289
290            // create poll's nfo
291            $sql="INSERT INTO ".$this->tables['polls_lang']." VALUES(
292              '".$id."',
293              '".$user['language']."',
294              '".addslashes($row['title'])."',
295              '',
296              '".addslashes($row['after_vote_text'])."'
297            )";
298
299            if(pwg_query($sql))
300            { 
301              //create poll's question's answers (before question to count nb answers)
302              $nbanswers=0;
303             
304              for($i=1;$i<=10;$i++)
305              {
306                if($row['c'.$i.'_text']!="")
307                {
308                  $nbanswers++;
309                  //answer
310                  $sql="INSERT INTO ".$this->tables['polls_answers']." VALUES (
311                    '".$id."',
312                    '1',
313                    '".$i."',
314                    '".$row['c'.$i.'_choose']."',
315                    '0',
316                    '0'
317                  )";
318                  if(!pwg_query($sql))
319                  {
320                    return(false);
321                  }
322
323                  //answer's lang
324                  $sql="INSERT INTO ".$this->tables['polls_answers_lang']." VALUES (
325                    '".$id."',
326                    '1',
327                    '".$i."',
328                    '".$user['language']."',
329                    '".addslashes($row['c'.$i.'_text'])."',
330                    '".addslashes($row['c'.$i.'_choose'])."'
331                  )";
332                  if(!pwg_query($sql))
333                  {
334                    return(false);
335                  }
336                }
337              }
338
339              //create question
340              $sql="INSERT INTO ".$this->tables['polls_questions']." VALUES (
341                '".$id."',
342                '1',
343                'mono',
344                '2',
345                '".$nbanswers."',
346                '".$row['total']."'
347              )";
348              if(pwg_query($sql))
349              {
350                $sql="INSERT INTO ".$this->tables['polls_questions_lang']." VALUES (
351                  '".$id."',
352                  '1',
353                  '".$user['language']."',
354                  '".addslashes($row['description'])."'
355                )";
356                if(!pwg_query($sql))
357                {
358                  return(false);
359                }
360              }
361              else
362              {
363                return(false);
364              }
365
366            }
367            else
368            {
369              return(false);
370            }
371
372            //create user's list votes and comments
373            $sql="SELECT * FROM ".$this->prefixeTable.$this->plugin_name_files."_polls_by_user_v1x
374              WHERE poll_id = '".$row['id']."'";
375            $result2=pwg_query($sql);
376            if($result2)
377            {
378              while($row2=mysql_fetch_assoc($result2))
379              {
380                $sql="INSERT INTO ".$this->tables['polls_votes']." VALUES (
381                  '".$id."',
382                  '".$row2['user_id']."',
383                  '".$row2['date']."',
384                  '".addslashes($row2['user_comment'])."'
385                )";
386                if(!pwg_query($sql))
387                {
388                  return(false);
389                }
390              }
391            }
392          }
393          else
394          {
395            return(false);
396          }
397        }
398      }
399      else
400      {
401        return(false);
402      }
403
404      return(true);
405    }
406
407
408  } //class
409
410?>
Note: See TracBrowser for help on using the repository browser.