ILIAS  Release_4_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 {
21  final function __construct()
22  {
23  $this->__init();
24  }
25 
34  abstract function getComponentType();
35 
44  abstract function getComponentName();
45 
54  abstract function getSlot();
55 
64  abstract function getSlotId();
65 
75  abstract function getPluginName();
76 
82  private final function setId($a_id)
83  {
84  $this->id = $a_id;
85  }
86 
92  final function getId()
93  {
94  return $this->id;
95  }
96 
102  private final function setLastUpdateVersion($a_lastupdateversion)
103  {
104  $this->lastupdateversion = $a_lastupdateversion;
105  }
106 
112  final function getLastUpdateVersion()
113  {
114  return $this->lastupdateversion;
115  }
116 
122  private final function setVersion($a_version)
123  {
124  $this->version = $a_version;
125  }
126 
132  final function getVersion()
133  {
134  return $this->version;
135  }
136 
142  private final function setIliasMinVersion($a_iliasminversion)
143  {
144  $this->iliasminversion = $a_iliasminversion;
145  }
146 
152  final function getIliasMinVersion()
153  {
154  return $this->iliasminversion;
155  }
156 
162  private final function setIliasMaxVersion($a_iliasmaxversion)
163  {
164  $this->iliasmaxversion = $a_iliasmaxversion;
165  }
166 
172  final function getIliasMaxVersion()
173  {
174  return $this->iliasmaxversion;
175  }
176 
182  private final function setActive($a_active)
183  {
184  $this->active = $a_active;
185  }
186 
192  final function getActive()
193  {
194  return $this->active;
195  }
196 
202  protected final function setSlotObject($a_slot)
203  {
204  $this->slot = $a_slot;
205  }
206 
212  protected final function getSlotObject()
213  {
214  return $this->slot;
215  }
216 
222  final function setDBVersion($a_dbversion)
223  {
224  $this->dbversion = $a_dbversion;
225  }
226 
232  final function getDBVersion()
233  {
234  return $this->dbversion;
235  }
236 
242  final function writeDBVersion($a_dbversion)
243  {
244  global $ilDB;
245 
246  $this->setDBVersion($a_dbversion);
247 
248  $q = "UPDATE il_plugin SET db_version = ".$ilDB->quote((int) $this->getDBVersion(), "integer").
249  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
250  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
251  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
252  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
253 
254  $ilDB->manipulate($q);
255  }
256 
257 
263  protected final function getDirectory()
264  {
265  return $this->getSlotObject()->getPluginsDirectory()."/".$this->getPluginName();
266  }
267 
271  static public final function _getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname)
272  {
273  return ilPluginSlot::_getPluginsDirectory($a_ctype, $a_cname, $a_slot_id)."/".$a_pname;
274 
275  }
276 
277 
283  protected final function getClassesDirectory()
284  {
285  return $this->getDirectory()."/classes";
286  }
287 
291  public final function includeClass($a_class_file_name)
292  {
293  include_once($this->getClassesDirectory()."/".$a_class_file_name);
294  }
295 
301  protected final function getLanguageDirectory()
302  {
303  return $this->getDirectory()."/lang";
304  }
305 
309  static final function getAvailableLangFiles($a_lang_directory)
310  {
311  $langs = array();
312 
313  if (!@is_dir($a_lang_directory))
314  {
315  return array();
316  }
317 
318  $dir = opendir($a_lang_directory);
319  while($file = readdir($dir))
320  {
321  if ($file != "." and
322  $file != "..")
323  {
324  // directories
325  if (@is_file($a_lang_directory."/".$file))
326  {
327  if (substr($file, 0, 6) == "ilias_" &&
328  substr($file, strlen($file) - 5) == ".lang")
329  {
330  $langs[] = array("key" => substr($file, 6, 2), "file" => $file,
331  "path" => $a_lang_directory."/".$file);
332  }
333  }
334  }
335  }
336 
337  return $langs;
338  }
339 
343  final function getPrefix()
344  {
345  return $this->getSlotObject()->getPrefix()."_".$this->getId();
346  }
347 
353  static public final function getDBUpdateScriptName($a_ctype, $a_cname, $a_slot_name, $a_pname)
354  {
355  return "Customizing/global/plugins/".$a_ctype."/".$a_cname."/".
356  $a_slot_name."/".$a_pname."/sql/dbupdate.php";
357  }
358 
362  final function getTablePrefix()
363  {
364  return $this->getPrefix();
365  }
366 
370  final public function updateLanguages()
371  {
372  global $ilCtrl;
373 
374  include_once("./Services/Language/classes/class.ilObjLanguage.php");
375 
376  $langs = $this->getAvailableLangFiles($this->getLanguageDirectory());
377 
378  $prefix = $this->getPrefix();
379 
380  foreach($langs as $lang)
381  {
382  $txt = file($this->getLanguageDirectory()."/".$lang["file"]);
383  $lang_array = array();
384 
385  // get language data
386  if (is_array($txt))
387  {
388  foreach ($txt as $row)
389  {
390  if ($row[0] != "#" && strpos($row, "#:#") > 0)
391  {
392  $a = explode("#:#",trim($row));
393  $lang_array[$prefix."_".trim($a[0])] = trim($a[1]);
394  ilObjLanguage::replaceLangEntry($prefix, $prefix."_".trim($a[0]),
395  $lang["key"], trim($a[1]));
396  }
397  }
398  }
399 
400  ilObjLanguage::replaceLangModule($lang["key"], $prefix,
401  $lang_array);
402  }
403  }
404 
408  function updateDatabase()
409  {
410  global $ilDB, $lng;
411 
412  include_once("./Services/Component/classes/class.ilPluginDBUpdate.php");
413  $dbupdate = new ilPluginDBUpdate($this->getComponentType(),
414  $this->getComponentName(), $this->getSlotId(),
415  $this->getPluginName(), $ilDB, true, $this->getTablePrefix());
416 
417  //$dbupdate->getDBVersionStatus();
418  //$dbupdate->getCurrentVersion();
419 
420  $result = $dbupdate->applyUpdate();
421 
422  if ($dbupdate->updateMsg == "no_changes")
423  {
424  $message = $lng->txt("no_changes").". ".$lng->txt("database_is_uptodate");
425  }
426  else
427  {
428  foreach ($dbupdate->updateMsg as $row)
429  {
430  $message .= $lng->txt($row["msg"]).": ".$row["nr"]."<br/>";
431  }
432  }
433 
434  $this->message.= $message;
435 
436  return $result;
437  }
438 
442  public final function loadLanguageModule()
443  {
444  global $lng;
445 
446  $lng->loadLanguageModule($this->getPrefix());
447  }
448 
452  public final function txt($a_var)
453  {
454  global $lng;
455  return $lng->txt($this->getPrefix()."_".$a_var);
456  }
457 
461  static function lookupTxt($a_mod_prefix, $a_pl_id, $a_lang_var)
462  {
463  global $lng;
464  return $lng->_lookupEntry($lng->lang_key, $a_mod_prefix."_".$a_pl_id,
465  $a_mod_prefix."_".$a_pl_id."_".$a_lang_var);
466  }
467 
471  public final function getTemplate($a_template, $a_par1 = true, $a_par2 = true)
472  {
473  $tpl = new ilTemplate($this->getDirectory()."/templates/".$a_template, $a_par1, $a_par2);
474 
475  return $tpl;
476  }
477 
481  public static final function _getImagePath($a_ctype, $a_cname, $a_slot_id,
482  $a_pname, $a_img)
483  {
484  $d = ilPlugin::_getDirectory($a_ctype, $a_cname, $a_slot_id, $a_pname);
485  return $d."/templates/images/".$a_img;
486  }
487 
491  public final function getImagePath($a_img)
492  {
493  return $this->getDirectory()."/templates/images/".$a_img;
494  }
495 
499  public final function getStyleSheetLocation($a_css_file)
500  {
501  return $this->getDirectory()."/templates/".$a_css_file;
502  }
503 
507  public final function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
508  {
509  $a_tpl->addBlockFile($a_var, $a_block,
510  $this->getDirectory()."/templates/".$a_tplname);
511  }
512 
516  static final public function getPluginRecord($a_ctype, $a_cname, $a_slot_id, $a_pname)
517  {
518  global $ilDB;
519 
520  // read/set basic data
521  $q = "SELECT * FROM il_plugin".
522  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
523  " AND component_name = ".$ilDB->quote($a_cname, "text").
524  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
525  " AND name = ".$ilDB->quote($a_pname, "text");
526  $set = $ilDB->query($q);
527  if ($rec = $ilDB->fetchAssoc($set))
528  {
529  return $rec;
530  }
531  else // no record? create one
532  {
533  $q = "INSERT INTO il_plugin (component_type, component_name, slot_id, name)".
534  " VALUES (".$ilDB->quote($a_ctype, "text").",".
535  $ilDB->quote($a_cname, "text").",".
536  $ilDB->quote($a_slot_id, "text").",".
537  $ilDB->quote($a_pname, "text").")";
538  $ilDB->manipulate($q);
539  $q = "SELECT * FROM il_plugin".
540  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
541  " AND component_name = ".$ilDB->quote($a_cname, "text").
542  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
543  " AND name = ".$ilDB->quote($a_pname, "text");
544  $set = $ilDB->query($q);
545  return $ilDB->fetchAssoc($set);
546  }
547  }
548 
552  final private function __init()
553  {
554  global $ilDB, $lng, $ilPluginAdmin;
555 
556  // read/set basic data
558  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
559  $this->setLastUpdateVersion($rec["last_update_version"]);
560  $this->setDBVersion($rec["db_version"]);
561  $this->setActive($rec["active"]);
562 
563  // get id
564  $this->setId($ilPluginAdmin->getId($this->getComponentType(),
565  $this->getComponentName(),
566  $this->getSlotId(),
567  $this->getPluginName()));
568 
569  // get version
570  $this->setVersion($ilPluginAdmin->getVersion($this->getComponentType(),
571  $this->getComponentName(),
572  $this->getSlotId(),
573  $this->getPluginName()));
574 
575  // get ilias min version
576  $this->setIliasMinVersion($ilPluginAdmin->getIliasMinVersion($this->getComponentType(),
577  $this->getComponentName(),
578  $this->getSlotId(),
579  $this->getPluginName()));
580 
581  // get ilias max version
582  $this->setIliasMaxVersion($ilPluginAdmin->getIliasMaxVersion($this->getComponentType(),
583  $this->getComponentName(),
584  $this->getSlotId(),
585  $this->getPluginName()));
586 
587  // get slot object
588  $this->setSlotObject(new ilPluginSlot($this->getComponentType(),
589  $this->getComponentName(), $this->getSlotId()));
590 
591  // load language module
592 
593  // Fix for authentication plugins
594  if(is_object($lng))
595  {
596  $lng->loadLanguageModule($this->getPrefix());
597  }
598 
599  // call slot and plugin init methods
600  $this->slotInit();
601  $this->init();
602  }
603 
610  abstract protected function slotInit();
611 
616  protected function init()
617  {
618  }
619 
623  public final function isActive()
624  {
625  global $ilPluginAdmin;
626 
627  return $ilPluginAdmin->isActive($this->getComponentType(),
628  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
629  }
630 
634  public final function needsUpdate()
635  {
636  global $ilPluginAdmin;
637 
638  return $ilPluginAdmin->isActive($this->getComponentType(),
639  $this->getComponentName(), $this->getSlotId(), $this->getPluginName());
640  }
641 
645  final function activate()
646  {
647  global $lng, $ilDB;
648 
649  $result = true;
650 
651  // check whether update is necessary
652  if ($this->needsUpdate())
653  {
654  //$result = $this->isUpdatePossible();
655 
656  // do update
657  if ($result === true)
658  {
659  $result = $this->update();
660  }
661  }
662  if ($result === true)
663  {
664  $result = $this->beforeActivation();
665  // activate plugin
666  if ($result === true)
667  {
668  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(1, "integer").",".
669  " plugin_id = ".$ilDB->quote($this->getId(), "text").
670  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
671  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
672  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
673  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
674 
675  $ilDB->manipulate($q);
676  $this->afterActivation();
677  }
678  }
679  return $result;
680  }
681 
685  protected function beforeActivation()
686  {
687  return true; // false would indicate that anything went wrong
688  // activation would not proceed
689  // throw an exception in this case
690  //throw new ilPluginException($lng->txt(""));
691  }
692 
696  protected function afterActivation()
697  {
698  }
699 
703  final function deactivate()
704  {
705  global $ilDB;
706 
707  $result = true;
708 
709  $q = "UPDATE il_plugin SET active = ".$ilDB->quote(0, "integer").
710  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
711  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
712  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
713  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
714 
715  $ilDB->manipulate($q);
716 
717  return $result;
718  }
719 
723  final function update()
724  {
725  global $ilDB, $ilCtrl;
726 
727  $result = true;
728 
729  // DB update
730  if ($result === true)
731  {
732  $result = $this->updateDatabase();
733  }
734 
735  // Load language files
736  $this->updateLanguages();
737 
738  // load control structure
739  include_once("./setup/classes/class.ilCtrlStructureReader.php");
740  $structure_reader = new ilCtrlStructureReader();
741  $structure_reader->readStructure(true, "./".$this->getDirectory(), $this->getPrefix(),
742  $this->getDirectory());
743  $ilCtrl->storeCommonStructures();
744 
745  // set last update version to current version
746  if ($result === true)
747  {
748  $q = "UPDATE il_plugin SET last_update_version = ".$ilDB->quote($this->getVersion(), "text").
749  " WHERE component_type = ".$ilDB->quote($this->getComponentType(), "text").
750  " AND component_name = ".$ilDB->quote($this->getComponentName(), "text").
751  " AND slot_id = ".$ilDB->quote($this->getSlotId(), "text").
752  " AND name = ".$ilDB->quote($this->getPluginName(), "text");
753 
754  $ilDB->manipulate($q);
755  }
756 
757  return $result;
758  }
759 
768  final static function getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
769  {
770  global $ilDB;
771 
772  include_once("./Services/Component/classes/class.ilPluginSlot.php");
773  $slot_name = ilPluginSlot::lookupSlotName($a_ctype, $a_cname, $a_slot_id);
774 
775  // this check is done due to security reasons
776  $set = $ilDB->queryF("SELECT * FROM il_component WHERE type = %s ".
777  " AND name = %s", array("text", "text"),
778  array($a_ctype, $a_cname));
779  if (!$ilDB->fetchAssoc($set))
780  {
781  return null;
782  }
783 
784  $file = "./Customizing/global/plugins/".$a_ctype."/".
785  $a_cname."/".$slot_name."/".
786  $a_pname."/classes/class.il".$a_pname."Plugin.php";
787 
788  if (is_file($file))
789  {
790  include_once($file);
791  $class = "il".$a_pname."Plugin";
792  $plugin = new $class();
793  return $plugin;
794  }
795 
796  return null;
797  }
798 
799 
803  final static function lookupStoredData($a_ctype, $a_cname, $a_slot_id, $a_pname)
804  {
805  global $ilDB;
806 
807  $q = "SELECT * FROM il_plugin WHERE ".
808  " component_type = ".$ilDB->quote($a_ctype, "text")." AND ".
809  " component_name = ".$ilDB->quote($a_cname, "text")." AND ".
810  " slot_id = ".$ilDB->quote($a_slot_id, "text")." AND ".
811  " name = ".$ilDB->quote($a_pname, "text");
812 
813  $set = $ilDB->query($q);
814 
815  $rec = $ilDB->fetchAssoc($set);
816 
817  return $rec;
818  }
819 
823  static final function getActivePluginsForSlot($a_ctype, $a_cname, $a_slot_id)
824  {
825  global $ilDB, $ilPluginAdmin;
826 
827  $q = "SELECT * FROM il_plugin WHERE component_type = ".$ilDB->quote($a_ctype, "text").
828  " AND component_name = ".$ilDB->quote($a_cname, "text").
829  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
830  " AND active = ".$ilDB->quote(1, "integer");
831 
832  $set = $ilDB->query($q);
833  $plugins = array();
834  while($rec = $ilDB->fetchAssoc($set))
835  {
836  if ($ilPluginAdmin->isActive($a_ctype, $a_cname, $a_slot_id, $rec["name"]))
837  {
838  $plugins[] = $rec["name"];
839  }
840  }
841 
842  return $plugins;
843  }
844 
848  function lookupNameForId($a_ctype, $a_cname, $a_slot_id, $a_plugin_id)
849  {
850  global $ilDB;
851 
852  $q = "SELECT name FROM il_plugin ".
853  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
854  " AND component_name = ".$ilDB->quote($a_cname, "text").
855  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
856  " AND plugin_id = ".$ilDB->quote($a_plugin_id, "text");
857 
858  $set = $ilDB->query($q);
859  if ($rec = $ilDB->fetchAssoc($set))
860  {
861  return $rec["name"];
862  }
863  }
864 
868  function lookupIdForName($a_ctype, $a_cname, $a_slot_id, $a_plugin_name)
869  {
870  global $ilDB;
871 
872  $q = "SELECT plugin_id FROM il_plugin ".
873  " WHERE component_type = ".$ilDB->quote($a_ctype, "text").
874  " AND component_name = ".$ilDB->quote($a_cname, "text").
875  " AND slot_id = ".$ilDB->quote($a_slot_id, "text").
876  " AND name = ".$ilDB->quote($a_plugin_name, "text");
877 
878  $set = $ilDB->query($q);
879  if ($rec = $ilDB->fetchAssoc($set))
880  {
881  return $rec["plugin_id"];
882  }
883  }
884 
885 }
886 ?>