ILIAS  Release_4_4_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 
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 
443  include_once("./Services/Component/classes/class.ilPluginDBUpdate.php");
444  $dbupdate = new ilPluginDBUpdate($this->getComponentType(),
445  $this->getComponentName(), $this->getSlotId(),
446  $this->getPluginName(), $ilDB, true, $this->getTablePrefix());
447 
448  //$dbupdate->getDBVersionStatus();
449  //$dbupdate->getCurrentVersion();
450 
451  $result = $dbupdate->applyUpdate();
452 
453  if ($dbupdate->updateMsg == "no_changes")
454  {
455  $message = $lng->txt("no_changes").". ".$lng->txt("database_is_uptodate");
456  }
457  else
458  {
459  foreach ($dbupdate->updateMsg as $row)
460  {
461  $message .= $lng->txt($row["msg"]).": ".$row["nr"]."<br/>";
462  }
463  }
464 
465  $this->message.= $message;
466 
467  return $result;
468  }
469 
473  public final function loadLanguageModule()
474  {
475  global $lng;
476 
477  if (!$this->lang_initialised && is_object($lng))
478  {
479  $lng->loadLanguageModule($this->getPrefix());
480  $this->lang_initialised = true;
481  }
482  }
483 
487  public final function txt($a_var)
488  {
489  global $lng;
490  $this->loadLanguageModule();
491  return $lng->txt($this->getPrefix()."_".$a_var, $this->getPrefix());
492  }
493 
497  static function lookupTxt($a_mod_prefix, $a_pl_id, $a_lang_var)
498  {
499  global $lng;
500 
501  // this enables default language fallback
502  $prefix = $a_mod_prefix."_".$a_pl_id;
503  return $lng->txt($prefix."_".$a_lang_var, $prefix);
504 
505  /*
506  return $lng->_lookupEntry($lng->lang_key, $a_mod_prefix."_".$a_pl_id,
507  $a_mod_prefix."_".$a_pl_id."_".$a_lang_var);
508  */
509  }
510 
514  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
515  {
516  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
517 
518  return $tpl;
519  }
520 
524  public static final function _getImagePath($a_ctype, $a_cname, $a_slot_id,
525  $a_pname, $a_img)
526  {
527  $d = ilPlugin::_getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname);
528  return $d."/templates/images/".$a_img;
529  }
530 
534  public final function getImagePath($a_img)
535  {
536  return $this->getDirectory()."/templates/images/".$a_img;
537  }
538 
542  public final function getStyleSheetLocation($a_css_file)
543  {
544  return $this->getDirectory()."/templates/".$a_css_file;
545  }
546 
550  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
551  {
552  $a_tpl->addBlockFile($a_var, $a_block,
553  $this->getDirectory()."/templates/".$a_tplname);
554  }
555 
559  static final public function createPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
560  {
561  global $ilDB;
562 
563  // check record existence record
564  $q = "SELECT * FROM il_plugin".
565  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
566  " AND component_name = ".$ilDB->quote($a_cname, "text").
567  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
568  " AND name = ".$ilDB->quote($a_pname, "text");
569  $set = $ilDB->query($q);
570  if (!$rec = $ilDB->fetchAssoc($set))
571  {
572  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
573  " VALUES (".$ilDB->quote($a_ctype, "text").",".
574  $ilDB->quote($a_cname, "text").",".
575  $ilDB->quote($a_slot_id, "text").",".
576  $ilDB->quote($a_pname, "text").")";
577  $ilDB->manipulate($q);
578  }
579  }
580 
581 
585  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
586  {
587  global $ilDB;
588 
589  // read/set basic data
590  $q = "SELECT * FROM il_plugin".
591  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
592  " AND component_name = ".$ilDB->quote($a_cname, "text").
593  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
594  " AND name = ".$ilDB->quote($a_pname, "text");
595  $set = $ilDB->query($q);
596  if ($rec = $ilDB->fetchAssoc($set))
597  {
598  return $rec;
599  }
600  else // no record? create one
601  {
602  // silently create these records is not a good idea, since
603  // the function can be called with "wrong parameters"
604  // raise exceptions instead
605  include_once("./Services/Component/exceptions/class.ilPluginException.php");
606  throw (new ilPluginException("No plugin record found for '".$a_ctype."', '".$a_cname."', '".$a_slot_id."', '".$a_pname."'."));
607 
608  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
609  " VALUES (".$ilDB->quote($a_ctype, "text").",".
610  $ilDB->quote($a_cname, "text").",".
611  $ilDB->quote($a_slot_id, "text").",".
612  $ilDB->quote($a_pname, "text").")";
613  $ilDB->manipulate($q);
614  $q = "SELECT * FROM il_plugin".
615  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
616  " AND component_name = ".$ilDB->quote($a_cname, "text").
617  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
618  " AND name = ".$ilDB->quote($a_pname, "text");
619  $set = $ilDB->query($q);
620  return $ilDB->fetchAssoc($set);
621  }
622  }
623 
627  final private function __init()
628  {
629  global $ilDB, $lng, $ilPluginAdmin;
630 
631  // read/set basic data
633  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
634  $this->setLastUpdateVersion($rec["last_update_version"]);
635  $this->setDBVersion($rec["db_version"]);
636  $this->setActive($rec["active"]);
637 
638  // get id
639  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
640  $this->getComponentName(),
641  $this->getSlotId(),
642  $this->getPluginName()));
643 
644  // get version
645  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
646  $this->getComponentName(),
647  $this->getSlotId(),
648  $this->getPluginName()));
649 
650  // get ilias min version
651  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
652  $this->getComponentName(),
653  $this->getSlotId(),
654  $this->getPluginName()));
655 
656  // get ilias max version
657  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
658  $this->getComponentName(),
659  $this->getSlotId(),
660  $this->getPluginName()));
661 
662  // get slot object
663  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
664  $this->getComponentName(), $this->getSlotId()));
665 
666  // load language module
667 
668  // Fix for authentication plugins
669  $this->loadLanguageModule();
670 
671  // call slot and plugin init methods
672  $this->slotInit();
673  $this->init();
674  }
675 
682  abstract protected function slotInit();
683 
688  protected function init()
689  {
690  }
691 
695  public final function isActive()
696  {
697  global $ilPluginAdmin;
698 
699  return $ilPluginAdmin->isActive($this->getComponentType(),
700  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
701  }
702 
706  public final function needsUpdate()
707  {
708  global $ilPluginAdmin;
709 
710  return $ilPluginAdmin->isActive($this->getComponentType(),
711  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
712  }
713 
717  final function activate()
718  {
719  global $lng, $ilDB;
720 
721  $result = true;
722 
723  // check whether update is necessary
724  if ($this->needsUpdate())
725  {
726  //$result = $this->isUpdatePossible();
727 
728  // do update
729  if ($result === true)
730  {
731  $result = $this->update();
732  }
733  }
734  if ($result === true)
735  {
736  $result = $this->beforeActivation();
737  // activate plugin
738  if ($result === true)
739  {
740  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(1, "integer").",".
741  " plugin_id = ".$ilDB->quote($this->getId(), "text").
742  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
743  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
744  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
745  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
746 
747  $ilDB->manipulate($q);
748  $this->afterActivation();
749  }
750  }
751  return $result;
752  }
753 
757  protected function beforeActivation()
758  {
759  return true; // false would indicate that anything went wrong
760  // activation would not proceed
761  // throw an exception in this case
762  //throw new ilPluginException($lng->txt(""));
763  }
764 
768  protected function afterActivation()
769  {
770  }
771 
775  final function deactivate()
776  {
777  global $ilDB;
778 
779  $result = true;
780 
781  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(0, "integer").
782  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
783  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
784  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
785  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
786 
787  $ilDB->manipulate($q);
788 
789  return $result;
790  }
791 
795  final function update()
796  {
797  global $ilDB, $ilCtrl;
798 
799  $result = true;
800 
801  // DB update
802  if ($result === true)
803  {
804  $result = $this->updateDatabase();
805  }
806 
807  // Load language files
808  $this->updateLanguages();
809 
810  // load control structure
811  include_once("./setup/classes/class.ilCtrlStructureReader.php");
812  $structure_reader = new ilCtrlStructureReader();
813  $structure_reader->readStructure(true, "./".$this->getDirectory(), $this->getPrefix(),
814  $this->getDirectory());
815 // $ilCtrl->storeCommonStructures();
816 
817  // add config gui to the ctrl calls
818  $ilCtrl->insertCtrlCalls("ilobjcomponentsettingsgui", ilPlugin::getConfigureClassName($this->getPluginName()),
819  $this->getPrefix());
820 
821  // set last update version to current version
822  if ($result === true)
823  {
824  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion(), "text").
825  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
826  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
827  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
828  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
829 
830  $ilDB->manipulate($q);
831  }
832 
833  return $result;
834  }
835 
844  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
845  {
846  global $ilDB;
847 
848  include_once("./Services/Component/classes/class.ilPluginSlot.php");
849  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
850 
851  // this check is done due to security reasons
852  $set = $ilDB->queryF("SELECT * FROM il_component WHERE type = %s ".
853  " AND name = %s", array("text", "text"),
854  array($a_ctype, $a_cname));
855  if (!$ilDB->fetchAssoc($set))
856  {
857  return null;
858  }
859 
860  $file = "./Customizing/global/plugins/".$a_ctype."/".
861  $a_cname."/".$slot_name."/".
862  $a_pname."/classes/class.il".$a_pname."Plugin.php";
863 
864  if (is_file($file))
865  {
866  include_once($file);
867  $class = "il".$a_pname."Plugin";
868  $plugin = new $class();
869  return $plugin;
870  }
871 
872  return null;
873  }
874 
875 
879  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
880  {
881  global $ilDB;
882 
883  $q = "SELECT * FROM il_plugin WHERE ".
884  " component_type = ".$ilDB->quote($a_ctype, "text")." AND ".
885  " component_name = ".$ilDB->quote($a_cname, "text")." AND ".
886  " slot_id = ".$ilDB->quote($a_slot_id, "text")." AND ".
887  " name = ".$ilDB->quote($a_pname, "text");
888 
889  $set = $ilDB->query($q);
890 
891  $rec = $ilDB->fetchAssoc($set);
892 
893  return $rec;
894  }
895 
899  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
900  {
901  global $ilDB, $ilPluginAdmin;
902 
903  $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype, "text").
904  " AND component_name = ".$ilDB->quote($a_cname, "text").
905  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
906  " AND active = ".$ilDB->quote(1, "integer");
907 
908  $set = $ilDB->query($q);
909  $plugins = array();
910  while($rec = $ilDB->fetchAssoc($set))
911  {
912  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
913  {
914  $plugins[] = $rec["name"];
915  }
916  }
917 
918  return $plugins;
919  }
920 
924  function lookupNameForId($a_ctype, $a_cname, $a_slot_id, $a_plugin_id)
925  {
926  global $ilDB;
927 
928  $q = "SELECT name FROM il_plugin ".
929  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
930  " AND component_name = ".$ilDB->quote($a_cname, "text").
931  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
932  " AND plugin_id = ".$ilDB->quote($a_plugin_id, "text");
933 
934  $set = $ilDB->query($q);
935  if ($rec = $ilDB->fetchAssoc($set))
936  {
937  return $rec["name"];
938  }
939  }
940 
944  function lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_plugin_name)
945  {
946  global $ilDB;
947 
948  $q = "SELECT plugin_id FROM il_plugin ".
949  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
950  " AND component_name = ".$ilDB->quote($a_cname, "text").
951  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
952  " AND name = ".$ilDB->quote($a_plugin_name, "text");
953 
954  $set = $ilDB->query($q);
955  if ($rec = $ilDB->fetchAssoc($set))
956  {
957  return $rec["plugin_id"];
958  }
959  }
960 }
961 ?>