ILIAS  Release_4_2_x_branch Revision 61807
 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 {
21  final function __construct()
22  {
23  $this->__init();
24  $this->initAutoLoad();
25  }
26 
35  abstract function getComponentType();
36 
45  abstract function getComponentName();
46 
55  abstract function getSlot();
56 
65  abstract function getSlotId();
66 
76  abstract function getPluginName();
77 
83  private final function setId($a_id)
84  {
85  $this->id = $a_id;
86  }
87 
93  final function getId()
94  {
95  return $this->id;
96  }
97 
103  private final function setLastUpdateVersion($a_lastupdateversion)
104  {
105  $this->lastupdateversion = $a_lastupdateversion;
106  }
107 
113  final function getLastUpdateVersion()
114  {
115  return $this->lastupdateversion;
116  }
117 
123  private final function setVersion($a_version)
124  {
125  $this->version = $a_version;
126  }
127 
133  final function getVersion()
134  {
135  return $this->version;
136  }
137 
143  private final function setIliasMinVersion($a_iliasminversion)
144  {
145  $this->iliasminversion = $a_iliasminversion;
146  }
147 
153  final function getIliasMinVersion()
154  {
155  return $this->iliasminversion;
156  }
157 
163  private final function setIliasMaxVersion($a_iliasmaxversion)
164  {
165  $this->iliasmaxversion = $a_iliasmaxversion;
166  }
167 
173  final function getIliasMaxVersion()
174  {
175  return $this->iliasmaxversion;
176  }
177 
183  private final function setActive($a_active)
184  {
185  $this->active = $a_active;
186  }
187 
193  final function getActive()
194  {
195  return $this->active;
196  }
197 
203  protected final function setSlotObject($a_slot)
204  {
205  $this->slot = $a_slot;
206  }
207 
213  protected final function getSlotObject()
214  {
215  return $this->slot;
216  }
217 
223  final function setDBVersion($a_dbversion)
224  {
225  $this->dbversion = $a_dbversion;
226  }
227 
233  final function getDBVersion()
234  {
235  return $this->dbversion;
236  }
237 
243  final function writeDBVersion($a_dbversion)
244  {
245  global $ilDB;
246 
247  $this->setDBVersion($a_dbversion);
248 
249  $q = "UPDATE il_plugin SET db_version = ".$ilDB->quote((int) $this->getDBVersion(), "integer").
250  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
251  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
252  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
253  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
254 
255  $ilDB->manipulate($q);
256  }
257 
258 
264  public final function getDirectory()
265  {
266  return $this->getSlotObject()->getPluginsDirectory()."/".$this->getPluginName();
267  }
268 
272  static public final function _getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname)
273  {
274  return ilPluginSlot::_getPluginsDirectory($a_ctype, $a_cname, $a_slot_id)."/".$a_pname;
275 
276  }
277 
278 
284  protected final function getClassesDirectory()
285  {
286  return $this->getDirectory()."/classes";
287  }
288 
292  public final function includeClass($a_class_file_name)
293  {
294  include_once($this->getClassesDirectory()."/".$a_class_file_name);
295  }
296 
302  protected final function getLanguageDirectory()
303  {
304  return $this->getDirectory()."/lang";
305  }
306 
310  static final function getAvailableLangFiles($a_lang_directory)
311  {
312  $langs = array();
313 
314  if (!@is_dir($a_lang_directory))
315  {
316  return array();
317  }
318 
319  $dir = opendir($a_lang_directory);
320  while($file = readdir($dir))
321  {
322  if ($file != "." and
323  $file != "..")
324  {
325  // directories
326  if (@is_file($a_lang_directory."/".$file))
327  {
328  if (substr($file, 0, 6) == "ilias_" &&
329  substr($file, strlen($file) - 5) == ".lang")
330  {
331  $langs[] = array("key" => substr($file, 6, 2), "file" => $file,
332  "path" => $a_lang_directory."/".$file);
333  }
334  }
335  }
336  }
337 
338  return $langs;
339  }
340 
348  static final function hasConfigureClass($a_slot_dir, $a_name)
349  {
350  if (is_file($a_slot_dir."/".
351  $a_name."/classes/class.il".$a_name."ConfigGUI.php"))
352  {
353  return true;
354  }
355  return false;
356  }
357 
364  static final function getConfigureClassName($a_name)
365  {
366  return "il".$a_name."ConfigGUI";
367  }
368 
372  final function getPrefix()
373  {
374  return $this->getSlotObject()->getPrefix()."_".$this->getId();
375  }
376 
382  static public final function getDBUpdateScriptName($a_ctype, $a_cname, $a_slot_name, $a_pname)
383  {
384  return "Customizing/global/plugins/".$a_ctype."/".$a_cname."/".
385  $a_slot_name."/".$a_pname."/sql/dbupdate.php";
386  }
387 
391  final function getTablePrefix()
392  {
393  return $this->getPrefix();
394  }
395 
399  final public function updateLanguages()
400  {
401  global $ilCtrl;
402 
403  include_once("./Services/Language/classes/class.ilObjLanguage.php");
404 
405  $langs = $this->getAvailableLangFiles($this->getLanguageDirectory());
406 
407  $prefix = $this->getPrefix();
408 
409  foreach($langs as $lang)
410  {
411  $txt = file($this->getLanguageDirectory()."/".$lang["file"]);
412  $lang_array = array();
413 
414  // get language data
415  if (is_array($txt))
416  {
417  foreach ($txt as $row)
418  {
419  if ($row[0] != "#" && strpos($row, "#:#") > 0)
420  {
421  $a = explode("#:#",trim($row));
422  $lang_array[$prefix."_".trim($a[0])] = trim($a[1]);
423  ilObjLanguage::replaceLangEntry($prefix, $prefix."_".trim($a[0]),
424  $lang["key"], trim($a[1]));
425 //echo "<br>-$prefix-".$prefix."_".trim($a[0])."-".$lang["key"]."-";
426  }
427  }
428  }
429 
430  ilObjLanguage::replaceLangModule($lang["key"], $prefix,
431  $lang_array);
432  }
433  }
434 
438  function updateDatabase()
439  {
440  global $ilDB, $lng;
441 
442  include_once("./Services/Component/classes/class.ilPluginDBUpdate.php");
443  $dbupdate = new ilPluginDBUpdate($this->getComponentType(),
444  $this->getComponentName(), $this->getSlotId(),
445  $this->getPluginName(), $ilDB, true, $this->getTablePrefix());
446 
447  //$dbupdate->getDBVersionStatus();
448  //$dbupdate->getCurrentVersion();
449 
450  $result = $dbupdate->applyUpdate();
451 
452  if ($dbupdate->updateMsg == "no_changes")
453  {
454  $message = $lng->txt("no_changes").". ".$lng->txt("database_is_uptodate");
455  }
456  else
457  {
458  foreach ($dbupdate->updateMsg as $row)
459  {
460  $message .= $lng->txt($row["msg"]).": ".$row["nr"]."<br/>";
461  }
462  }
463 
464  $this->message.= $message;
465 
466  return $result;
467  }
468 
472  public final function loadLanguageModule()
473  {
474  global $lng;
475 
476  $lng->loadLanguageModule($this->getPrefix());
477  }
478 
482  public final function txt($a_var)
483  {
484  global $lng;
485  return $lng->txt($this->getPrefix()."_".$a_var, $this->getPrefix());
486  }
487 
491  static function lookupTxt($a_mod_prefix, $a_pl_id, $a_lang_var)
492  {
493  global $lng;
494  return $lng->_lookupEntry($lng->lang_key, $a_mod_prefix."_".$a_pl_id,
495  $a_mod_prefix."_".$a_pl_id."_".$a_lang_var);
496  }
497 
501  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
502  {
503  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
504 
505  return $tpl;
506  }
507 
511  public static final function _getImagePath($a_ctype, $a_cname, $a_slot_id,
512  $a_pname, $a_img)
513  {
514  $d = ilPlugin::_getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname);
515  return $d."/templates/images/".$a_img;
516  }
517 
521  public final function getImagePath($a_img)
522  {
523  return $this->getDirectory()."/templates/images/".$a_img;
524  }
525 
529  public final function getStyleSheetLocation($a_css_file)
530  {
531  return $this->getDirectory()."/templates/".$a_css_file;
532  }
533 
537  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
538  {
539  $a_tpl->addBlockFile($a_var, $a_block,
540  $this->getDirectory()."/templates/".$a_tplname);
541  }
542 
546  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
547  {
548  global $ilDB;
549 
550  // read/set basic data
551  $q = "SELECT * FROM il_plugin".
552  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
553  " AND component_name = ".$ilDB->quote($a_cname, "text").
554  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
555  " AND name = ".$ilDB->quote($a_pname, "text");
556  $set = $ilDB->query($q);
557  if ($rec = $ilDB->fetchAssoc($set))
558  {
559  return $rec;
560  }
561  else // no record? create one
562  {
563  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
564  " VALUES (".$ilDB->quote($a_ctype, "text").",".
565  $ilDB->quote($a_cname, "text").",".
566  $ilDB->quote($a_slot_id, "text").",".
567  $ilDB->quote($a_pname, "text").")";
568  $ilDB->manipulate($q);
569  $q = "SELECT * FROM il_plugin".
570  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
571  " AND component_name = ".$ilDB->quote($a_cname, "text").
572  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
573  " AND name = ".$ilDB->quote($a_pname, "text");
574  $set = $ilDB->query($q);
575  return $ilDB->fetchAssoc($set);
576  }
577  }
578 
582  final private function __init()
583  {
584  global $ilDB, $lng, $ilPluginAdmin;
585 
586  // read/set basic data
588  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
589  $this->setLastUpdateVersion($rec["last_update_version"]);
590  $this->setDBVersion($rec["db_version"]);
591  $this->setActive($rec["active"]);
592 
593  // get id
594  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
595  $this->getComponentName(),
596  $this->getSlotId(),
597  $this->getPluginName()));
598 
599  // get version
600  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
601  $this->getComponentName(),
602  $this->getSlotId(),
603  $this->getPluginName()));
604 
605  // get ilias min version
606  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
607  $this->getComponentName(),
608  $this->getSlotId(),
609  $this->getPluginName()));
610 
611  // get ilias max version
612  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
613  $this->getComponentName(),
614  $this->getSlotId(),
615  $this->getPluginName()));
616 
617  // get slot object
618  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
619  $this->getComponentName(), $this->getSlotId()));
620 
621  // load language module
622 
623  // Fix for authentication plugins
624  if(is_object($lng))
625  {
626  $lng->loadLanguageModule($this->getPrefix());
627  }
628 
629  // call slot and plugin init methods
630  $this->slotInit();
631  $this->init();
632  }
633 
640  abstract protected function slotInit();
641 
646  protected function init()
647  {
648  }
649 
653  public final function isActive()
654  {
655  global $ilPluginAdmin;
656 
657  return $ilPluginAdmin->isActive($this->getComponentType(),
658  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
659  }
660 
664  public final function needsUpdate()
665  {
666  global $ilPluginAdmin;
667 
668  return $ilPluginAdmin->isActive($this->getComponentType(),
669  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
670  }
671 
675  final function activate()
676  {
677  global $lng, $ilDB;
678 
679  $result = true;
680 
681  // check whether update is necessary
682  if ($this->needsUpdate())
683  {
684  //$result = $this->isUpdatePossible();
685 
686  // do update
687  if ($result === true)
688  {
689  $result = $this->update();
690  }
691  }
692  if ($result === true)
693  {
694  $result = $this->beforeActivation();
695  // activate plugin
696  if ($result === true)
697  {
698  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(1, "integer").",".
699  " plugin_id = ".$ilDB->quote($this->getId(), "text").
700  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
701  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
702  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
703  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
704 
705  $ilDB->manipulate($q);
706  $this->afterActivation();
707  }
708  }
709  return $result;
710  }
711 
715  protected function beforeActivation()
716  {
717  return true; // false would indicate that anything went wrong
718  // activation would not proceed
719  // throw an exception in this case
720  //throw new ilPluginException($lng->txt(""));
721  }
722 
726  protected function afterActivation()
727  {
728  }
729 
733  final function deactivate()
734  {
735  global $ilDB;
736 
737  $result = true;
738 
739  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(0, "integer").
740  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
741  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
742  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
743  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
744 
745  $ilDB->manipulate($q);
746 
747  return $result;
748  }
749 
753  final function update()
754  {
755  global $ilDB, $ilCtrl;
756 
757  $result = true;
758 
759  // DB update
760  if ($result === true)
761  {
762  $result = $this->updateDatabase();
763  }
764 
765  // Load language files
766  $this->updateLanguages();
767 
768  // load control structure
769  include_once("./setup/classes/class.ilCtrlStructureReader.php");
770  $structure_reader = new ilCtrlStructureReader();
771  $structure_reader->readStructure(true, "./".$this->getDirectory(), $this->getPrefix(),
772  $this->getDirectory());
773 // $ilCtrl->storeCommonStructures();
774 
775  // add config gui to the ctrl calls
776  $ilCtrl->insertCtrlCalls("ilobjcomponentsettingsgui", ilPlugin::getConfigureClassName($this->getPluginName()),
777  $this->getPrefix());
778 
779  // set last update version to current version
780  if ($result === true)
781  {
782  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion(), "text").
783  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
784  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
785  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
786  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
787 
788  $ilDB->manipulate($q);
789  }
790 
791  return $result;
792  }
793 
802  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
803  {
804  global $ilDB;
805 
806  include_once("./Services/Component/classes/class.ilPluginSlot.php");
807  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
808 
809  // this check is done due to security reasons
810  $set = $ilDB->queryF("SELECT * FROM il_component WHERE type = %s ".
811  " AND name = %s", array("text", "text"),
812  array($a_ctype, $a_cname));
813  if (!$ilDB->fetchAssoc($set))
814  {
815  return null;
816  }
817 
818  $file = "./Customizing/global/plugins/".$a_ctype."/".
819  $a_cname."/".$slot_name."/".
820  $a_pname."/classes/class.il".$a_pname."Plugin.php";
821 
822  if (is_file($file))
823  {
824  include_once($file);
825  $class = "il".$a_pname."Plugin";
826  $plugin = new $class();
827  return $plugin;
828  }
829 
830  return null;
831  }
832 
833 
837  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
838  {
839  global $ilDB;
840 
841  $q = "SELECT * FROM il_plugin WHERE ".
842  " component_type = ".$ilDB->quote($a_ctype, "text")." AND ".
843  " component_name = ".$ilDB->quote($a_cname, "text")." AND ".
844  " slot_id = ".$ilDB->quote($a_slot_id, "text")." AND ".
845  " name = ".$ilDB->quote($a_pname, "text");
846 
847  $set = $ilDB->query($q);
848 
849  $rec = $ilDB->fetchAssoc($set);
850 
851  return $rec;
852  }
853 
857  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
858  {
859  global $ilDB, $ilPluginAdmin;
860 
861  $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype, "text").
862  " AND component_name = ".$ilDB->quote($a_cname, "text").
863  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
864  " AND active = ".$ilDB->quote(1, "integer");
865 
866  $set = $ilDB->query($q);
867  $plugins = array();
868  while($rec = $ilDB->fetchAssoc($set))
869  {
870  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
871  {
872  $plugins[] = $rec["name"];
873  }
874  }
875 
876  return $plugins;
877  }
878 
882  function lookupNameForId($a_ctype, $a_cname, $a_slot_id, $a_plugin_id)
883  {
884  global $ilDB;
885 
886  $q = "SELECT name FROM il_plugin ".
887  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
888  " AND component_name = ".$ilDB->quote($a_cname, "text").
889  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
890  " AND plugin_id = ".$ilDB->quote($a_plugin_id, "text");
891 
892  $set = $ilDB->query($q);
893  if ($rec = $ilDB->fetchAssoc($set))
894  {
895  return $rec["name"];
896  }
897  }
898 
902  function lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_plugin_name)
903  {
904  global $ilDB;
905 
906  $q = "SELECT plugin_id FROM il_plugin ".
907  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
908  " AND component_name = ".$ilDB->quote($a_cname, "text").
909  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
910  " AND name = ".$ilDB->quote($a_plugin_name, "text");
911 
912  $set = $ilDB->query($q);
913  if ($rec = $ilDB->fetchAssoc($set))
914  {
915  return $rec["plugin_id"];
916  }
917  }
918 
923  protected function initAutoLoad()
924  {
925  spl_autoload_register(
926  array($this,'autoLoad')
927  );
928  }
929 
935  private final function autoLoad($a_classname)
936  {
937  $class_file = $this->getClassesDirectory().'/class.'.$a_classname.'.php';
938  @include_once($class_file);
939  }
940 
941 }
942 ?>