ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPlugin.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
25 include_once("./Services/Component/classes/class.ilComponent.php");
26 
35 abstract class ilPlugin
36 {
40  final function __construct()
41  {
42  $this->__init();
43  }
44 
53  abstract function getComponentType();
54 
63  abstract function getComponentName();
64 
73  abstract function getSlot();
74 
83  abstract function getSlotId();
84 
94  abstract function getPluginName();
95 
101  private final function setId($a_id)
102  {
103  $this->id = $a_id;
104  }
105 
111  final function getId()
112  {
113  return $this->id;
114  }
115 
121  private final function setLastUpdateVersion($a_lastupdateversion)
122  {
123  $this->lastupdateversion = $a_lastupdateversion;
124  }
125 
131  final function getLastUpdateVersion()
132  {
133  return $this->lastupdateversion;
134  }
135 
141  private final function setVersion($a_version)
142  {
143  $this->version = $a_version;
144  }
145 
151  final function getVersion()
152  {
153  return $this->version;
154  }
155 
161  private final function setIliasMinVersion($a_iliasminversion)
162  {
163  $this->iliasminversion = $a_iliasminversion;
164  }
165 
171  final function getIliasMinVersion()
172  {
173  return $this->iliasminversion;
174  }
175 
181  private final function setIliasMaxVersion($a_iliasmaxversion)
182  {
183  $this->iliasmaxversion = $a_iliasmaxversion;
184  }
185 
191  final function getIliasMaxVersion()
192  {
193  return $this->iliasmaxversion;
194  }
195 
201  private final function setActive($a_active)
202  {
203  $this->active = $a_active;
204  }
205 
211  final function getActive()
212  {
213  return $this->active;
214  }
215 
221  protected final function setSlotObject($a_slot)
222  {
223  $this->slot = $a_slot;
224  }
225 
231  protected final function getSlotObject()
232  {
233  return $this->slot;
234  }
235 
241  final function setDBVersion($a_dbversion)
242  {
243  $this->dbversion = $a_dbversion;
244  }
245 
251  final function getDBVersion()
252  {
253  return $this->dbversion;
254  }
255 
261  final function writeDBVersion($a_dbversion)
262  {
263  global $ilDB;
264 
265  $this->setDBVersion($a_dbversion);
266 
267  $q = "UPDATE il_plugin SET db_version = ".$ilDB->quote($this->getDBVersion()).
268  " WHERE component_type = ".$ilDB->quote($this->getComponentType()).
269  " AND component_name = ".$ilDB->quote($this->getComponentName()).
270  " AND slot_id = ".$ilDB->quote($this->getSlotId()).
271  " AND name = ".$ilDB->quote($this->getPluginName());
272 
273  $ilDB->query($q);
274  }
275 
276 
282  protected final function getDirectory()
283  {
284  return $this->getSlotObject()->getPluginsDirectory()."/".$this->getPluginName();
285  }
286 
292  protected final function getClassesDirectory()
293  {
294  return $this->getDirectory()."/classes";
295  }
296 
300  public final function includeClass($a_class_file_name)
301  {
302  include_once($this->getClassesDirectory()."/".$a_class_file_name);
303  }
304 
310  protected final function getLanguageDirectory()
311  {
312  return $this->getDirectory()."/lang";
313  }
314 
318  static final function getAvailableLangFiles($a_lang_directory)
319  {
320  $langs = array();
321 
322  if (!@is_dir($a_lang_directory))
323  {
324  return array();
325  }
326 
327  $dir = opendir($a_lang_directory);
328  while($file = readdir($dir))
329  {
330  if ($file != "." and
331  $file != "..")
332  {
333  // directories
334  if (@is_file($a_lang_directory."/".$file))
335  {
336  if (substr($file, 0, 6) == "ilias_" &&
337  substr($file, strlen($file) - 5) == ".lang")
338  {
339  $langs[] = array("key" => substr($file, 6, 2), "file" => $file,
340  "path" => $a_lang_directory."/".$file);
341  }
342  }
343  }
344  }
345 
346  return $langs;
347  }
348 
352  final function getPrefix()
353  {
354  return $this->getSlotObject()->getPrefix()."_".$this->getId();
355  }
356 
362  static public final function getDBUpdateScriptName($a_ctype, $a_cname, $a_slot_name, $a_pname)
363  {
364  return "Customizing/global/plugins/".$a_ctype."/".$a_cname."/".
365  $a_slot_name."/".$a_pname."/sql/dbupdate.php";
366  }
367 
371  final function getTablePrefix()
372  {
373  return "il_".$this->getPrefix();
374  }
375 
379  final public function updateLanguages()
380  {
381  global $ilCtrl;
382 
383  include_once("./Services/Language/classes/class.ilObjLanguage.php");
384 
385  $langs = $this->getAvailableLangFiles($this->getLanguageDirectory());
386 
387  $prefix = $this->getPrefix();
388 
389  foreach($langs as $lang)
390  {
391  $txt = file($this->getLanguageDirectory()."/".$lang["file"]);
392  $lang_array = array();
393 
394  // get language data
395  if (is_array($txt))
396  {
397  foreach ($txt as $row)
398  {
399  if ($row[0] != "#" && strpos($row, "#:#") > 0)
400  {
401  $a = explode("#:#",trim($row));
402  $lang_array[$prefix."_".trim($a[0])] = trim($a[1]);
403  }
404  }
405  }
406 
407  ilObjLanguage::replaceLangModule($lang["key"], $prefix,
408  $lang_array);
409  }
410  }
411 
415  function updateDatabase()
416  {
417  global $ilDB, $lng;
418 
419  include_once("./Services/Component/classes/class.ilPluginDBUpdate.php");
420  $dbupdate = new ilPluginDBUpdate($this->getComponentType(),
421  $this->getComponentName(), $this->getSlotId(),
422  $this->getPluginName(), $ilDB, true, $this->getTablePrefix());
423 
424  //$dbupdate->getDBVersionStatus();
425  //$dbupdate->getCurrentVersion();
426 
427  $result = $dbupdate->applyUpdate();
428 
429  if ($dbupdate->updateMsg == "no_changes")
430  {
431  $message = $lng->txt("no_changes").". ".$lng->txt("database_is_uptodate");
432  }
433  else
434  {
435  foreach ($dbupdate->updateMsg as $row)
436  {
437  $message .= $lng->txt($row["msg"]).": ".$row["nr"]."<br/>";
438  }
439  }
440 
441  $this->message.= $message;
442 
443  return $result;
444  }
445 
449  public final function loadLanguageModule()
450  {
451  global $lng;
452 
453  $lng->loadLanguageModule($this->getPrefix());
454  }
455 
459  public final function txt($a_var)
460  {
461  global $lng;
462  return $lng->txt($this->getPrefix()."_".$a_var);
463  }
464 
468  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
469  {
470  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
471 
472  return $tpl;
473  }
474 
478  public final function getStyleSheetLocation($a_css_file)
479  {
480  return $this->getDirectory()."/templates/".$a_css_file;
481  }
482 
486  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
487  {
488  $a_tpl->addBlockFile($a_var, $a_block,
489  $this->getDirectory()."/templates/".$a_tplname);
490  }
491 
495  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
496  {
497  global $ilDB;
498 
499  // read/set basic data
500  $q = "SELECT * FROM il_plugin".
501  " WHERE component_type = ".$ilDB->quote($a_ctype).
502  " AND component_name = ".$ilDB->quote($a_cname).
503  " AND slot_id = ".$ilDB->quote($a_slot_id).
504  " AND name = ".$ilDB->quote($a_pname);
505  $set = $ilDB->query($q);
506  if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
507  {
508  return $rec;
509  }
510  else // no record? create one
511  {
512  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
513  " VALUES (".$ilDB->quote($a_ctype).",".
514  $ilDB->quote($a_cname).",".
515  $ilDB->quote($a_slot_id).",".
516  $ilDB->quote($a_pname).")";
517  $ilDB->query($q);
518  $q = "SELECT * FROM il_plugin".
519  " WHERE component_type = ".$ilDB->quote($a_ctype).
520  " AND component_name = ".$ilDB->quote($a_cname).
521  " AND slot_id = ".$ilDB->quote($a_slot_id).
522  " AND name = ".$ilDB->quote($a_pname);
523  $set = $ilDB->query($q);
524  return $set->fetchRow(DB_FETCHMODE_ASSOC);
525  }
526  }
527 
531  final private function __init()
532  {
533  global $ilDB, $lng, $ilPluginAdmin;
534 
535  // read/set basic data
537  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
538  $this->setLastUpdateVersion($rec["last_update_version"]);
539  $this->setDBVersion($rec["db_version"]);
540  $this->setActive($rec["active"]);
541 
542  // get id
543  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
544  $this->getComponentName(),
545  $this->getSlotId(),
546  $this->getPluginName()));
547 
548  // get version
549  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
550  $this->getComponentName(),
551  $this->getSlotId(),
552  $this->getPluginName()));
553 
554  // get ilias min version
555  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
556  $this->getComponentName(),
557  $this->getSlotId(),
558  $this->getPluginName()));
559 
560  // get ilias max version
561  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
562  $this->getComponentName(),
563  $this->getSlotId(),
564  $this->getPluginName()));
565 
566  // get slot object
567  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
568  $this->getComponentName(), $this->getSlotId()));
569 
570  // load language module
571  $lng->loadLanguageModule($this->getPrefix());
572 
573  // call slot and plugin init methods
574  $this->slotInit();
575  $this->init();
576  }
577 
584  abstract protected function slotInit();
585 
590  protected function init()
591  {
592  }
593 
597  public final function isActive()
598  {
599  global $ilPluginAdmin;
600 
601  return $ilPluginAdmin->isActive($this->getComponentType(),
602  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
603  }
604 
608  public final function needsUpdate()
609  {
610  global $ilPluginAdmin;
611 
612  return $ilPluginAdmin->isActive($this->getComponentType(),
613  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
614  }
615 
619  final function activate()
620  {
621  global $lng, $ilDB;
622 
623  $result = true;
624 
625  // check whether update is necessary
626  if ($this->needsUpdate())
627  {
628  $result = $this->isUpdatePossible();
629 
630  // do update
631  if ($result === true)
632  {
633  $result = $this->update();
634  }
635  }
636 
637  // activate plugin
638  if ($result === true)
639  {
640  $q = "UPDATE il_plugin SET active = 1".
641  " WHERE component_type = ".$ilDB->quote($this->getComponentType()).
642  " AND component_name = ".$ilDB->quote($this->getComponentName()).
643  " AND slot_id = ".$ilDB->quote($this->getSlotId()).
644  " AND name = ".$ilDB->quote($this->getPluginName());
645 
646  $ilDB->query($q);
647  }
648  }
649 
653  final function deactivate()
654  {
655  global $ilDB;
656 
657  $result = true;
658 
659  $q = "UPDATE il_plugin SET active = 0".
660  " WHERE component_type = ".$ilDB->quote($this->getComponentType()).
661  " AND component_name = ".$ilDB->quote($this->getComponentName()).
662  " AND slot_id = ".$ilDB->quote($this->getSlotId()).
663  " AND name = ".$ilDB->quote($this->getPluginName());
664 
665  $ilDB->query($q);
666 
667  return $result;
668  }
669 
673  final function update()
674  {
675  global $ilDB, $ilCtrl;
676 
677  $result = true;
678 
679  // DB update
680  if ($result === true)
681  {
682  $result = $this->updateDatabase();
683  }
684 
685  // Load language files
686  $this->updateLanguages();
687 
688  // load control structure
689  chdir("./setup");
690  include_once("./classes/class.ilCtrlStructureReader.php");
691  $structure_reader = new ilCtrlStructureReader();
692  $structure_reader->readStructure(true, "../".$this->getDirectory(), $this->getPrefix());
693  chdir("..");
694  $ilCtrl->storeCommonStructures();
695 
696  // set last update version to current version
697  if ($result === true)
698  {
699  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion()).
700  " WHERE component_type = ".$ilDB->quote($this->getComponentType()).
701  " AND component_name = ".$ilDB->quote($this->getComponentName()).
702  " AND slot_id = ".$ilDB->quote($this->getSlotId()).
703  " AND name = ".$ilDB->quote($this->getPluginName());
704 
705  $ilDB->query($q);
706  }
707 
708  return $result;
709  }
710 
719  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
720  {
721  global $ilDB;
722 
723  include_once("./Services/Component/classes/class.ilPluginSlot.php");
724  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
725 
726  // this check is done due to security reasons
727  $set = $ilDB->query("SELECT * FROM il_component WHERE type = ".
728  $ilDB->quote($a_ctype)." AND name = ".$ilDB->quote($a_cname));
729  if ($set->numRows() == 0)
730  {
731  return null;
732  }
733 
734  $file = "./Customizing/global/plugins/".$a_ctype."/".
735  $a_cname."/".$slot_name."/".
736  $a_pname."/classes/class.il".$a_pname."Plugin.php";
737 
738  if (is_file($file))
739  {
740  include_once($file);
741  $class = "il".$a_pname."Plugin";
742  $plugin = new $class();
743  return $plugin;
744  }
745 
746  return null;
747  }
748 
749 
753  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
754  {
755  global $ilDB;
756 
757  $q = "SELECT * FROM il_plugin WHERE ".
758  " component_type = ".$ilDB->quote($a_ctype)." AND ".
759  " component_name = ".$ilDB->quote($a_cname)." AND ".
760  " slot_id = ".$ilDB->quote($a_slot_id)." AND ".
761  " name = ".$ilDB->quote($a_pname);
762 
763  $set = $ilDB->query($q);
764 
765  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
766 
767  return $rec;
768  }
769 
773  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
774  {
775  global $ilDB, $ilPluginAdmin;
776 
777  $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype).
778  " AND component_name = ".$ilDB->quote($a_cname).
779  " AND slot_id = ".$ilDB->quote($a_slot_id).
780  " AND active = 1";
781 
782  $set = $ilDB->query($q);
783  $plugins = array();
784  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
785  {
786  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
787  {
788  $plugins[] = $rec["name"];
789  }
790  }
791 
792  return $plugins;
793  }
794 }
795 ?>