ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPlugin.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
5 include_once("./Services/Component/classes/class.ilComponent.php");
6 include_once("./Services/Component/exceptions/class.ilPluginException.php");
7 
16 abstract class ilPlugin
17 {
18  protected $lang_initialised = false;
19 
23  final function __construct()
24  {
25  $this->__init();
26  }
27 
36  abstract function getComponentType();
37 
46  abstract function getComponentName();
47 
56  abstract function getSlot();
57 
66  abstract function getSlotId();
67 
77  abstract function getPluginName();
78 
84  private final function setId($a_id)
85  {
86  $this->id = $a_id;
87  }
88 
94  final function getId()
95  {
96  return $this->id;
97  }
98 
104  private final function setLastUpdateVersion($a_lastupdateversion)
105  {
106  $this->lastupdateversion = $a_lastupdateversion;
107  }
108 
114  final function getLastUpdateVersion()
115  {
116  return $this->lastupdateversion;
117  }
118 
124  private final function setVersion($a_version)
125  {
126  $this->version = $a_version;
127  }
128 
134  final function getVersion()
135  {
136  return $this->version;
137  }
138 
144  private final function setIliasMinVersion($a_iliasminversion)
145  {
146  $this->iliasminversion = $a_iliasminversion;
147  }
148 
154  final function getIliasMinVersion()
155  {
156  return $this->iliasminversion;
157  }
158 
164  private final function setIliasMaxVersion($a_iliasmaxversion)
165  {
166  $this->iliasmaxversion = $a_iliasmaxversion;
167  }
168 
174  final function getIliasMaxVersion()
175  {
176  return $this->iliasmaxversion;
177  }
178 
184  private final function setActive($a_active)
185  {
186  $this->active = $a_active;
187  }
188 
194  final function getActive()
195  {
196  return $this->active;
197  }
198 
204  protected final function setSlotObject($a_slot)
205  {
206  $this->slot = $a_slot;
207  }
208 
214  protected final function getSlotObject()
215  {
216  return $this->slot;
217  }
218 
224  final function setDBVersion($a_dbversion)
225  {
226  $this->dbversion = $a_dbversion;
227  }
228 
234  final function getDBVersion()
235  {
236  return $this->dbversion;
237  }
238 
244  final function writeDBVersion($a_dbversion)
245  {
246  global $ilDB;
247 
248  $this->setDBVersion($a_dbversion);
249 
250  $q = "UPDATE il_plugin SET db_version = ".$ilDB->quote((int) $this->getDBVersion(), "integer").
251  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
252  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
253  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
254  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
255 
256  $ilDB->manipulate($q);
257  }
258 
259 
265  public final function getDirectory()
266  {
267  return $this->getSlotObject()->getPluginsDirectory()."/".$this->getPluginName();
268  }
269 
273  static public final function _getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname)
274  {
275  include_once "Services/Component/classes/class.ilPluginSlot.php";
276  return ilPluginSlot::_getPluginsDirectory($a_ctype, $a_cname, $a_slot_id)."/".$a_pname;
277  }
278 
279 
285  protected final function getClassesDirectory()
286  {
287  return $this->getDirectory()."/classes";
288  }
289 
293  public final function includeClass($a_class_file_name)
294  {
295  include_once($this->getClassesDirectory()."/".$a_class_file_name);
296  }
297 
303  protected final function getLanguageDirectory()
304  {
305  return $this->getDirectory()."/lang";
306  }
307 
311  static final function getAvailableLangFiles($a_lang_directory)
312  {
313  $langs = array();
314 
315  if (!@is_dir($a_lang_directory))
316  {
317  return array();
318  }
319 
320  $dir = opendir($a_lang_directory);
321  while($file = readdir($dir))
322  {
323  if ($file != "." and
324  $file != "..")
325  {
326  // directories
327  if (@is_file($a_lang_directory."/".$file))
328  {
329  if (substr($file, 0, 6) == "ilias_" &&
330  substr($file, strlen($file) - 5) == ".lang")
331  {
332  $langs[] = array("key" => substr($file, 6, 2), "file" => $file,
333  "path" => $a_lang_directory."/".$file);
334  }
335  }
336  }
337  }
338 
339  return $langs;
340  }
341 
349  static final function hasConfigureClass($a_slot_dir, $a_name)
350  {
351  if (is_file($a_slot_dir."/".
352  $a_name."/classes/class.il".$a_name."ConfigGUI.php"))
353  {
354  return true;
355  }
356  return false;
357  }
358 
365  static final function getConfigureClassName($a_name)
366  {
367  return "il".$a_name."ConfigGUI";
368  }
369 
373  final function getPrefix()
374  {
375  return $this->getSlotObject()->getPrefix()."_".$this->getId();
376  }
377 
383  static public final function getDBUpdateScriptName($a_ctype, $a_cname, $a_slot_name, $a_pname)
384  {
385  return "Customizing/global/plugins/".$a_ctype."/".$a_cname."/".
386  $a_slot_name."/".$a_pname."/sql/dbupdate.php";
387  }
388 
392  final function getTablePrefix()
393  {
394  return $this->getPrefix();
395  }
396 
400  final public function updateLanguages()
401  {
402  global $ilCtrl;
403  ilGlobalCache::flushAll();
404  include_once("./Services/Language/classes/class.ilObjLanguage.php");
405 
406  $langs = $this->getAvailableLangFiles($this->getLanguageDirectory());
407 
408  $prefix = $this->getPrefix();
409 
410  foreach($langs as $lang)
411  {
412  $txt = file($this->getLanguageDirectory()."/".$lang["file"]);
413  $lang_array = array();
414 
415  // get language data
416  if (is_array($txt))
417  {
418  foreach ($txt as $row)
419  {
420  if ($row[0] != "#" && strpos($row, "#:#") > 0)
421  {
422  $a = explode("#:#",trim($row));
423  $lang_array[$prefix."_".trim($a[0])] = trim($a[1]);
424  ilObjLanguage::replaceLangEntry($prefix, $prefix."_".trim($a[0]),
425  $lang["key"], trim($a[1]));
426  //echo "<br>-$prefix-".$prefix."_".trim($a[0])."-".$lang["key"]."-";
427  }
428  }
429  }
430 
431  ilObjLanguage::replaceLangModule($lang["key"], $prefix,
432  $lang_array);
433  }
434  }
435 
439  function updateDatabase()
440  {
441  global $ilDB, $lng;
442 
444 
445  include_once("./Services/Component/classes/class.ilPluginDBUpdate.php");
446  $dbupdate = new ilPluginDBUpdate($this->getComponentType(),
447  $this->getComponentName(), $this->getSlotId(),
448  $this->getPluginName(), $ilDB, true, $this->getTablePrefix());
449 
450  //$dbupdate->getDBVersionStatus();
451  //$dbupdate->getCurrentVersion();
452 
453  $result = $dbupdate->applyUpdate();
454  $message = '';
455  if ($dbupdate->updateMsg == "no_changes")
456  {
457  $message = $lng->txt("no_changes").". ".$lng->txt("database_is_uptodate");
458  }
459  else
460  {
461  foreach ($dbupdate->updateMsg as $row)
462  {
463  $message .= $lng->txt($row["msg"]).": ".$row["nr"]."<br/>";
464  }
465  }
466 
467  $this->message.= $message;
468 
469  return $result;
470  }
471 
475  public final function loadLanguageModule()
476  {
477  global $lng;
478 
479  if (!$this->lang_initialised && is_object($lng))
480  {
481  $lng->loadLanguageModule($this->getPrefix());
482  $this->lang_initialised = true;
483  }
484  }
485 
489  public final function txt($a_var)
490  {
491  global $lng;
492  $this->loadLanguageModule();
493  return $lng->txt($this->getPrefix()."_".$a_var, $this->getPrefix());
494  }
495 
499  static function lookupTxt($a_mod_prefix, $a_pl_id, $a_lang_var)
500  {
501  global $lng;
502 
503  // this enables default language fallback
504  $prefix = $a_mod_prefix."_".$a_pl_id;
505  return $lng->txt($prefix."_".$a_lang_var, $prefix);
506 
507  /*
508  return ilLanguage::_lookupEntry($lng->lang_key, $a_mod_prefix."_".$a_pl_id,
509  $a_mod_prefix."_".$a_pl_id."_".$a_lang_var);
510  */
511  }
512 
516  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
517  {
518  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
519 
520  return $tpl;
521  }
522 
526  public static final function _getImagePath($a_ctype, $a_cname, $a_slot_id,
527  $a_pname, $a_img)
528  {
529  $d2 = ilComponent::lookupId($a_ctype, $a_cname)."_".$a_slot_id."_".
530  ilPlugin::lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_pname);
531 
532  $img = ilUtil::getImagePath($d2."/".$a_img);
533  if (is_int(strpos($img, "Customizing")))
534  {
535  return $img;
536  }
537 
538  $d = ilPlugin::_getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname);
539  return $d."/templates/images/".$a_img;
540  }
541 
545  public final function getImagePath($a_img)
546  {
547  return self::_getImagePath($this->getComponentType(), $this->getComponentName(), $this->getSlotId(),
548  $this->getPluginName(), $a_img);
549  }
550 
554  public final function getStyleSheetLocation($a_css_file)
555  {
556  $d2 = ilComponent::lookupId($this->getComponentType(), $this->getComponentName())."_".$this->getSlotId()."_".
558 
559  $css = ilUtil::getStyleSheetLocation("output", $a_css_file, $d2);
560  if (is_int(strpos($css, "Customizing")))
561  {
562  return $css;
563  }
564 
565  return $this->getDirectory()."/templates/".$a_css_file;
566  }
567 
571  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
572  {
573  $a_tpl->addBlockFile($a_var, $a_block,
574  $this->getDirectory()."/templates/".$a_tplname);
575  }
576 
580  static final public function createPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
581  {
582  global $ilDB;
583 
585 
586  // check record existence record
587  $q = "SELECT * FROM il_plugin".
588  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
589  " AND component_name = ".$ilDB->quote($a_cname, "text").
590  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
591  " AND name = ".$ilDB->quote($a_pname, "text");
592  $set = $ilDB->query($q);
593  if (!$rec = $ilDB->fetchAssoc($set))
594  {
595  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
596  " VALUES (".$ilDB->quote($a_ctype, "text").",".
597  $ilDB->quote($a_cname, "text").",".
598  $ilDB->quote($a_slot_id, "text").",".
599  $ilDB->quote($a_pname, "text").")";
600  $ilDB->manipulate($q);
601  }
602  }
603 
604 
608  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
609  {
610  $cached_component = ilCachedComponentData::getInstance();
611  $rec = $cached_component->lookupPluginByName($a_pname);
612 
613  if ($rec['component_type'] == $a_ctype AND $rec['component_name'] == $a_cname AND $rec['slot_id'] == $a_slot_id) {
614  return $rec;
615  } else {
616  include_once("./Services/Component/exceptions/class.ilPluginException.php");
617  throw (new ilPluginException("No plugin record found for '" . $a_ctype . "', '" . $a_cname . "', '" . $a_slot_id . "', '" . $a_pname
618  . "'."));
619 
620  }
621  //
622  // global $ilDB;
623  //
624  // // read/set basic data
625  // $q = "SELECT * FROM il_plugin".
626  // " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
627  // " AND component_name = ".$ilDB->quote($a_cname, "text").
628  // " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
629  // " AND name = ".$ilDB->quote($a_pname, "text");
630  // $set = $ilDB->query($q);
631  // if ($rec = $ilDB->fetchAssoc($set))
632  // {
633  // return $rec;
634  // }
635  // else // no record? create one
636  // {
637  // // silently create these records is not a good idea, since
638  // // the function can be called with "wrong parameters"
639  // // raise exceptions instead
640  // include_once("./Services/Component/exceptions/class.ilPluginException.php");
641  // throw (new ilPluginException("No plugin record found for '".$a_ctype."', '".$a_cname."', '".$a_slot_id."', '".$a_pname."'."));
642  //
643  // $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
644  // " VALUES (".$ilDB->quote($a_ctype, "text").",".
645  // $ilDB->quote($a_cname, "text").",".
646  // $ilDB->quote($a_slot_id, "text").",".
647  // $ilDB->quote($a_pname, "text").")";
648  // $ilDB->manipulate($q);
649  // $q = "SELECT * FROM il_plugin".
650  // " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
651  // " AND component_name = ".$ilDB->quote($a_cname, "text").
652  // " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
653  // " AND name = ".$ilDB->quote($a_pname, "text");
654  // $set = $ilDB->query($q);
655  // return $ilDB->fetchAssoc($set);
656  // }
657  }
658 
662  final private function __init()
663  {
664  global $ilDB, $lng, $ilPluginAdmin;
665 
666  // read/set basic data
668  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
669  $this->setLastUpdateVersion($rec["last_update_version"]);
670  $this->setDBVersion($rec["db_version"]);
671  $this->setActive($rec["active"]);
672 
673  // get id
674  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
675  $this->getComponentName(),
676  $this->getSlotId(),
677  $this->getPluginName()));
678 
679  // get version
680  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
681  $this->getComponentName(),
682  $this->getSlotId(),
683  $this->getPluginName()));
684 
685  // get ilias min version
686  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
687  $this->getComponentName(),
688  $this->getSlotId(),
689  $this->getPluginName()));
690 
691  // get ilias max version
692  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
693  $this->getComponentName(),
694  $this->getSlotId(),
695  $this->getPluginName()));
696 
697  // get slot object
698  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
699  $this->getComponentName(), $this->getSlotId()));
700 
701  // load language module
702 
703  // Fix for authentication plugins
704  $this->loadLanguageModule();
705 
706  // call slot and plugin init methods
707  $this->slotInit();
708  $this->init();
709  }
710 
717  abstract protected function slotInit();
718 
723  protected function init()
724  {
725  }
726 
730  public final function isActive()
731  {
732  global $ilPluginAdmin;
733 
734  return $ilPluginAdmin->isActive($this->getComponentType(),
735  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
736  }
737 
741  public final function needsUpdate()
742  {
743  global $ilPluginAdmin;
744 
745  return $ilPluginAdmin->isActive($this->getComponentType(),
746  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
747  }
748 
752  final function activate()
753  {
754  global $lng, $ilDB;
755 
757 
758  $result = true;
759 
760  // check whether update is necessary
761  if ($this->needsUpdate())
762  {
763  //$result = $this->isUpdatePossible();
764 
765  // do update
766  if ($result === true)
767  {
768  $result = $this->update();
769  }
770  }
771  if ($result === true)
772  {
773  $result = $this->beforeActivation();
774  // activate plugin
775  if ($result === true)
776  {
777  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(1, "integer").",".
778  " plugin_id = ".$ilDB->quote($this->getId(), "text").
779  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
780  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
781  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
782  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
783 
784  $ilDB->manipulate($q);
785  $this->afterActivation();
786  }
787  }
789  return $result;
790  }
791 
795  protected function beforeActivation()
796  {
797  return true; // false would indicate that anything went wrong
798  // activation would not proceed
799  // throw an exception in this case
800  //throw new ilPluginException($lng->txt(""));
801  }
802 
806  protected function afterActivation()
807  {
808  }
809 
813  final function deactivate()
814  {
815  global $ilDB;
816 
818 
819  $result = true;
820 
821  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(0, "integer").
822  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
823  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
824  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
825  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
826 
827  $ilDB->manipulate($q);
828  $this->afterDeactivation();
829 
830  return $result;
831  }
832 
833 
837  protected function afterDeactivation()
838  {
839  }
840 
844  final function update()
845  {
846  global $ilDB, $ilCtrl;
847 
849 
850  $result = $this->beforeUpdate();
851  if ($result === false) {
852  return false;
853  }
854 
855  // DB update
856  if ($result === true)
857  {
858  $result = $this->updateDatabase();
859  }
860 
861  // Load language files
862  $this->updateLanguages();
863 
864  // load control structure
865  include_once("./setup/classes/class.ilCtrlStructureReader.php");
866  $structure_reader = new ilCtrlStructureReader();
867  $structure_reader->readStructure(true, "./".$this->getDirectory(), $this->getPrefix(),
868  $this->getDirectory());
869  // $ilCtrl->storeCommonStructures();
870 
871  // add config gui to the ctrl calls
872  $ilCtrl->insertCtrlCalls("ilobjcomponentsettingsgui", ilPlugin::getConfigureClassName($this->getPluginName()),
873  $this->getPrefix());
874 
875  // set last update version to current version
876  if ($result === true)
877  {
878  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion(), "text").
879  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
880  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
881  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
882  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
883 
884  $ilDB->manipulate($q);
885  $this->afterUpdate();
886  }
887 
888  return $result;
889  }
890 
894  protected function beforeUpdate()
895  {
896  return true; // false would indicate that anything went wrong
897  // update would not proceed
898  // throw an exception in this case
899  //throw new ilPluginException($lng->txt(""));
900  }
901 
905  protected function afterUpdate()
906  {
907  }
908 
917  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
918  {
919  global $ilDB;
920 
921  include_once("./Services/Component/classes/class.ilPluginSlot.php");
922  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
923 
924  $cached_component = ilCachedComponentData::getInstance();
925  $rec = $cached_component->lookCompId($a_ctype, $a_cname);
926  if (! $rec) {
927  return NULL;
928  }
929 
930  // this check is done due to security reasons
931  // $set = $ilDB->queryF("SELECT * FROM il_component WHERE type = %s ".
932  // " AND name = %s", array("text", "text"),
933  // array($a_ctype, $a_cname));
934  // if (!$ilDB->fetchAssoc($set))
935  // {
936  // return null;
937  // }
938 
939  $file = "./Customizing/global/plugins/".$a_ctype."/".
940  $a_cname."/".$slot_name."/".
941  $a_pname."/classes/class.il".$a_pname."Plugin.php";
942 
943  if (is_file($file))
944  {
945  include_once($file);
946  $class = "il".$a_pname."Plugin";
947  $plugin = new $class();
948  return $plugin;
949  }
950 
951  return null;
952  }
953 
954 
958  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
959  {
960  global $ilDB;
961 
962  $q = "SELECT * FROM il_plugin WHERE ".
963  " component_type = ".$ilDB->quote($a_ctype, "text")." AND ".
964  " component_name = ".$ilDB->quote($a_cname, "text")." AND ".
965  " slot_id = ".$ilDB->quote($a_slot_id, "text")." AND ".
966  " name = ".$ilDB->quote($a_pname, "text");
967 
968  $set = $ilDB->query($q);
969 
970  $rec = $ilDB->fetchAssoc($set);
971 
972  return $rec;
973  }
974 
978  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
979  {
980  global $ilDB, $ilPluginAdmin;
981 
982  $plugins = array();
983 
984  // $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype, "text").
985  // " AND component_name = ".$ilDB->quote($a_cname, "text").
986  // " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
987  // " AND active = ".$ilDB->quote(1, "integer");
988  //
989  // $set = $ilDB->query($q);
990  $cached_component = ilCachedComponentData::getInstance();
991  // while($rec = $ilDB->fetchAssoc($set))
992  $lookupActivePluginsBySlotId = $cached_component->lookupActivePluginsBySlotId($a_slot_id);
993  foreach($lookupActivePluginsBySlotId as $rec)
994  {
995  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
996  {
997  $plugins[] = $rec["name"];
998  }
999  }
1000 
1001  return $plugins;
1002  }
1003 
1007  function lookupNameForId($a_ctype, $a_cname, $a_slot_id, $a_plugin_id)
1008  {
1009  global $ilDB;
1010 
1011  $q = "SELECT name FROM il_plugin ".
1012  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
1013  " AND component_name = ".$ilDB->quote($a_cname, "text").
1014  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
1015  " AND plugin_id = ".$ilDB->quote($a_plugin_id, "text");
1016 
1017  $set = $ilDB->query($q);
1018  if ($rec = $ilDB->fetchAssoc($set))
1019  {
1020  return $rec["name"];
1021  }
1022  }
1023 
1027  function lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_plugin_name)
1028  {
1029  global $ilDB;
1030 
1031  $q = "SELECT plugin_id FROM il_plugin ".
1032  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
1033  " AND component_name = ".$ilDB->quote($a_cname, "text").
1034  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
1035  " AND name = ".$ilDB->quote($a_plugin_name, "text");
1036 
1037  $set = $ilDB->query($q);
1038  if ($rec = $ilDB->fetchAssoc($set))
1039  {
1040  return $rec["plugin_id"];
1041  }
1042  }
1043 }
1044 ?>