ILIAS  release_4-3 Revision
 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  return $lng->_lookupEntry($lng->lang_key, $a_mod_prefix."_".$a_pl_id,
501  $a_mod_prefix."_".$a_pl_id."_".$a_lang_var);
502  }
503 
507  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
508  {
509  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
510 
511  return $tpl;
512  }
513 
517  public static final function _getImagePath($a_ctype, $a_cname, $a_slot_id,
518  $a_pname, $a_img)
519  {
520  $d = ilPlugin::_getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname);
521  return $d."/templates/images/".$a_img;
522  }
523 
527  public final function getImagePath($a_img)
528  {
529  return $this->getDirectory()."/templates/images/".$a_img;
530  }
531 
535  public final function getStyleSheetLocation($a_css_file)
536  {
537  return $this->getDirectory()."/templates/".$a_css_file;
538  }
539 
543  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
544  {
545  $a_tpl->addBlockFile($a_var, $a_block,
546  $this->getDirectory()."/templates/".$a_tplname);
547  }
548 
552  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
553  {
554  global $ilDB;
555 
556  // read/set basic data
557  $q = "SELECT * FROM il_plugin".
558  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
559  " AND component_name = ".$ilDB->quote($a_cname, "text").
560  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
561  " AND name = ".$ilDB->quote($a_pname, "text");
562  $set = $ilDB->query($q);
563  if ($rec = $ilDB->fetchAssoc($set))
564  {
565  return $rec;
566  }
567  else // no record? create one
568  {
569  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
570  " VALUES (".$ilDB->quote($a_ctype, "text").",".
571  $ilDB->quote($a_cname, "text").",".
572  $ilDB->quote($a_slot_id, "text").",".
573  $ilDB->quote($a_pname, "text").")";
574  $ilDB->manipulate($q);
575  $q = "SELECT * FROM il_plugin".
576  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
577  " AND component_name = ".$ilDB->quote($a_cname, "text").
578  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
579  " AND name = ".$ilDB->quote($a_pname, "text");
580  $set = $ilDB->query($q);
581  return $ilDB->fetchAssoc($set);
582  }
583  }
584 
588  final private function __init()
589  {
590  global $ilDB, $lng, $ilPluginAdmin;
591 
592  // read/set basic data
594  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
595  $this->setLastUpdateVersion($rec["last_update_version"]);
596  $this->setDBVersion($rec["db_version"]);
597  $this->setActive($rec["active"]);
598 
599  // get id
600  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
601  $this->getComponentName(),
602  $this->getSlotId(),
603  $this->getPluginName()));
604 
605  // get version
606  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
607  $this->getComponentName(),
608  $this->getSlotId(),
609  $this->getPluginName()));
610 
611  // get ilias min version
612  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
613  $this->getComponentName(),
614  $this->getSlotId(),
615  $this->getPluginName()));
616 
617  // get ilias max version
618  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
619  $this->getComponentName(),
620  $this->getSlotId(),
621  $this->getPluginName()));
622 
623  // get slot object
624  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
625  $this->getComponentName(), $this->getSlotId()));
626 
627  // load language module
628 
629  // Fix for authentication plugins
630  $this->loadLanguageModule();
631 
632  // call slot and plugin init methods
633  $this->slotInit();
634  $this->init();
635  }
636 
643  abstract protected function slotInit();
644 
649  protected function init()
650  {
651  }
652 
656  public final function isActive()
657  {
658  global $ilPluginAdmin;
659 
660  return $ilPluginAdmin->isActive($this->getComponentType(),
661  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
662  }
663 
667  public final function needsUpdate()
668  {
669  global $ilPluginAdmin;
670 
671  return $ilPluginAdmin->isActive($this->getComponentType(),
672  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
673  }
674 
678  final function activate()
679  {
680  global $lng, $ilDB;
681 
682  $result = true;
683 
684  // check whether update is necessary
685  if ($this->needsUpdate())
686  {
687  //$result = $this->isUpdatePossible();
688 
689  // do update
690  if ($result === true)
691  {
692  $result = $this->update();
693  }
694  }
695  if ($result === true)
696  {
697  $result = $this->beforeActivation();
698  // activate plugin
699  if ($result === true)
700  {
701  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(1, "integer").",".
702  " plugin_id = ".$ilDB->quote($this->getId(), "text").
703  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
704  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
705  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
706  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
707 
708  $ilDB->manipulate($q);
709  $this->afterActivation();
710  }
711  }
712  return $result;
713  }
714 
718  protected function beforeActivation()
719  {
720  return true; // false would indicate that anything went wrong
721  // activation would not proceed
722  // throw an exception in this case
723  //throw new ilPluginException($lng->txt(""));
724  }
725 
729  protected function afterActivation()
730  {
731  }
732 
736  final function deactivate()
737  {
738  global $ilDB;
739 
740  $result = true;
741 
742  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(0, "integer").
743  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
744  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
745  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
746  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
747 
748  $ilDB->manipulate($q);
749 
750  return $result;
751  }
752 
756  final function update()
757  {
758  global $ilDB, $ilCtrl;
759 
760  $result = true;
761 
762  // DB update
763  if ($result === true)
764  {
765  $result = $this->updateDatabase();
766  }
767 
768  // Load language files
769  $this->updateLanguages();
770 
771  // load control structure
772  include_once("./setup/classes/class.ilCtrlStructureReader.php");
773  $structure_reader = new ilCtrlStructureReader();
774  $structure_reader->readStructure(true, "./".$this->getDirectory(), $this->getPrefix(),
775  $this->getDirectory());
776 // $ilCtrl->storeCommonStructures();
777 
778  // add config gui to the ctrl calls
779  $ilCtrl->insertCtrlCalls("ilobjcomponentsettingsgui", ilPlugin::getConfigureClassName($this->getPluginName()),
780  $this->getPrefix());
781 
782  // set last update version to current version
783  if ($result === true)
784  {
785  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion(), "text").
786  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
787  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
788  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
789  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
790 
791  $ilDB->manipulate($q);
792  }
793 
794  return $result;
795  }
796 
805  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
806  {
807  global $ilDB;
808 
809  include_once("./Services/Component/classes/class.ilPluginSlot.php");
810  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
811 
812  // this check is done due to security reasons
813  $set = $ilDB->queryF("SELECT * FROM il_component WHERE type = %s ".
814  " AND name = %s", array("text", "text"),
815  array($a_ctype, $a_cname));
816  if (!$ilDB->fetchAssoc($set))
817  {
818  return null;
819  }
820 
821  $file = "./Customizing/global/plugins/".$a_ctype."/".
822  $a_cname."/".$slot_name."/".
823  $a_pname."/classes/class.il".$a_pname."Plugin.php";
824 
825  if (is_file($file))
826  {
827  include_once($file);
828  $class = "il".$a_pname."Plugin";
829  $plugin = new $class();
830  return $plugin;
831  }
832 
833  return null;
834  }
835 
836 
840  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
841  {
842  global $ilDB;
843 
844  $q = "SELECT * FROM il_plugin WHERE ".
845  " component_type = ".$ilDB->quote($a_ctype, "text")." AND ".
846  " component_name = ".$ilDB->quote($a_cname, "text")." AND ".
847  " slot_id = ".$ilDB->quote($a_slot_id, "text")." AND ".
848  " name = ".$ilDB->quote($a_pname, "text");
849 
850  $set = $ilDB->query($q);
851 
852  $rec = $ilDB->fetchAssoc($set);
853 
854  return $rec;
855  }
856 
860  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
861  {
862  global $ilDB, $ilPluginAdmin;
863 
864  $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype, "text").
865  " AND component_name = ".$ilDB->quote($a_cname, "text").
866  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
867  " AND active = ".$ilDB->quote(1, "integer");
868 
869  $set = $ilDB->query($q);
870  $plugins = array();
871  while($rec = $ilDB->fetchAssoc($set))
872  {
873  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
874  {
875  $plugins[] = $rec["name"];
876  }
877  }
878 
879  return $plugins;
880  }
881 
885  function lookupNameForId($a_ctype, $a_cname, $a_slot_id, $a_plugin_id)
886  {
887  global $ilDB;
888 
889  $q = "SELECT name FROM il_plugin ".
890  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
891  " AND component_name = ".$ilDB->quote($a_cname, "text").
892  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
893  " AND plugin_id = ".$ilDB->quote($a_plugin_id, "text");
894 
895  $set = $ilDB->query($q);
896  if ($rec = $ilDB->fetchAssoc($set))
897  {
898  return $rec["name"];
899  }
900  }
901 
905  function lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_plugin_name)
906  {
907  global $ilDB;
908 
909  $q = "SELECT plugin_id FROM il_plugin ".
910  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
911  " AND component_name = ".$ilDB->quote($a_cname, "text").
912  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
913  " AND name = ".$ilDB->quote($a_plugin_name, "text");
914 
915  $set = $ilDB->query($q);
916  if ($rec = $ilDB->fetchAssoc($set))
917  {
918  return $rec["plugin_id"];
919  }
920  }
921 }
922 ?>