ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilBuddySystemRelationStateFactory.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Contact/BuddySystem/classes/states/class.ilAbstractBuddySystemRelationState.php';
5
11{
15 protected static $instance;
16
20 protected static $valid_states;
21
25 protected static $state_option_array;
26
30 protected $lng;
31
35 protected function __construct()
36 {
37 global $DIC;
38
39 $this->lng = $DIC['lng'];
40 }
41
45 public static function getInstance()
46 {
47 if(null === self::$instance)
48 {
49 self::$instance = new self;
50 }
51
52 return self::$instance;
53 }
54
59 public function getValidStates()
60 {
61 if(null !== self::$valid_states)
62 {
64 }
65
66 $states = array();
67 $iter = new DirectoryIterator(dirname(__FILE__));
68 foreach($iter as $file)
69 {
73 if($file->isDir())
74 {
75 continue;
76 }
77
78 require_once $file->getFilename();
79 $class = str_replace(array('class.', '.php'), '', $file->getBasename());
80 $reflection = new ReflectionClass($class);
81 if(
82 !$reflection->isAbstract() &&
83 $reflection->isSubclassOf('ilBuddySystemRelationState')
84 )
85 {
86 $states[] = new $class();
87 }
88 }
89
90 return (self::$valid_states = $states);
91 }
92
97 public function getInitialState()
98 {
99 foreach($this->getValidStates() as $state)
100 {
101 if($state->isInitial())
102 {
103 return $state;
104 }
105 }
106
107 throw new ilBuddySystemException("Could not find an initial state class");
108 }
109
114 public function getStatesAsOptionArray($with_initial_state = false)
115 {
116 if(null !== self::$state_option_array[$with_initial_state])
117 {
118 return self::$state_option_array[$with_initial_state];
119 }
120
121 $options = array();
122
123 foreach($this->getValidStates() as $state)
124 {
125 if($with_initial_state || !$state->isInitial())
126 {
127 $options[get_class($state)] = $this->lng->txt('buddy_bs_state_' . strtolower($state->getName()));
128 }
129 }
130
131 return (self::$state_option_array[$with_initial_state] = $options);
132 }
133
140 public function getRendererByOwnerAndRelation($owner_id, ilBuddySystemRelation $relation)
141 {
142 $state_class = get_class($relation->getState());
143 $renderer_class = $state_class . 'ButtonRenderer';
144 $renderer_path = "Services/Contact/BuddySystem/classes/states/renderer/class.{$renderer_class}.php";
145
146 if(!file_exists($renderer_path))
147 {
148 throw new ilBuddySystemException(sprintf("Could not find a renderer file for state: %s", $state_class));
149 }
150
151 require_once $renderer_path;
152 if(!class_exists($renderer_class))
153 {
154 throw new ilBuddySystemException(sprintf("Could not find a renderer class for state: %s in file: %s", $state_class, $renderer_path));
155 }
156
157 return new $renderer_class($owner_id, $relation);
158 }
159}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
getRendererByOwnerAndRelation($owner_id, ilBuddySystemRelation $relation)
Class ilBuddySystemRelation.
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
if(!is_array($argv)) $options
global $DIC