Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a6601e7860
Fetching contributors…

Cannot retrieve contributors at this time

file 201 lines (171 sloc) 4.852 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/**
* Stores local listeners
* @type array
*/
private $listeners = null;

/**
* Stores local listeners queue
* @type array
*/
private $listenerQueue = null;

/**
* Saves a listener in the database
*
* @param mixed $listener listener-config
* @param int $refId reference id for binding a local listener to a tupel
*/
private static function saveListener($listener, $refId = null)
{
$listener = <?php echo $listenerName; ?>::getListenerConfig($listener, get_called_class(), $refId);
// save listener
$l = <?php echo $listenerName; ?>Query::create()->findOneById($listener['id']);
if (is_null($l)) {
$l = new <?php echo $listenerName; ?>();
$l->fromArray($listener, BasePeer::TYPE_FIELDNAME);
if (array_key_exists('params', $listener)) {
$l->setParams($listener['params']);
}
$l->save();
}

return $l;
}

/**
* Adds a global listener to the collection
*
* @param mixed $listener listener-config
*/
public static function addGlobalListener($listener)
{
<?php echo $listenerName; ?>::addListenerToRuntime(self::saveListener($listener));
}

/**
* Adds a local listener to the collection
*
* @param mixed $listener listener-config
*/
public function addListener($listener)
{
if ($this->_deleted) {
return false;
}
if (is_array($this->getPrimaryKey())) {
throw new PropelException('Cannot add local listener on an object with a composite primary key');
}
// pre fill $this->listeners. So new listeners won't be saved here once the object isn't saved itself
if (is_null($this->listeners)) {
$this->getListeners();
}

// enqueue listener, if this is new
if ($this->_new) {
if (is_null($this->listenerQueue)) {
$this->listenerQueue = array();
}
$this->listenerQueue[] = $listener;
}
// anyway save it
else {
$this->listeners[] = self::saveListener($listener, $this->getPrimaryKey());
}
}

/**
* Removes a global listener from the collection
*
* @param mixed $listener listener-config
*/
public static function removeGlobalListener($listener)
{
$listener = <?php echo $listenerName; ?>::getListenerConfig($listener, get_called_class());
$l = <?php echo $listenerName; ?>Query::create()->findOneById($listener['id']);
if ($l) {
<?php echo $listenerName; ?>::removeListenerFromRuntime($l);
$l->delete();
}
}

/**
* Removes a local listener from the collection
*
* @param mixed $listener listener-config
*/
public function removeListener($listener) {
$listener = <?php echo $listenerName; ?>::getListenerConfig($listener, get_class($this), $this->getPrimaryKey());
$l = <?php echo $listenerName; ?>Query::create()->findOneById($listener['id']);
if ($l) {
$key = array_search($l, $this->getListeners());
if ($key) {
unset($this->listeners[$key]);
}
$l->delete();
}
}

/**
* Notifies listeners about a certain occured event
*
* @param String $event the event at which the listener fires
*/
private function notifyListener($event) {
$target = '<?php echo $className; ?>';
$allListeners = <?php echo $listenerName; ?>::getGlobalListeners($target);
$listeners = array();

// grab static listeners that will receive notification based on the occured event
if (array_key_exists($event, $allListeners)) {
$listeners = $allListeners[$event];
}

// grab static listeners that will receive notifications on all events
if (isset($allListeners[<?php echo $listenerName; ?>::ALL])) {
$listeners = array_merge($listeners, $allListeners[<?php echo $listenerName; ?>::ALL]);
}
// grab local listeners
$listeners = array_merge($listeners, $this->getListeners());
if (count($listeners)) {
$e = array(
'event' => $event,
'target' => $this,
);
foreach ($listeners as $l) {
$e['params'] = $l->getParams() ? $l->getParams() : null;

if ($l->getOn()) {
$cls = $l->getOn();
$c = new $cls();
$method = $l->getCallback();
if (!method_exists($c, $method)) {
$method = sprintf('on%s%s',
strtoupper(substr($e['event'], 0, 1)),
substr($e['event'], 1));
if (!method_exists($c, $method)) {
$method = 'handleEvent';
}
}
if (method_exists($c, $method)) {
$c->$method($e);
}
} else {
$cb = $l->getCallback();
$cb($e);
}
}
}
}

/**
* Returns the local listeners
*
* @return array
*/
private function getListeners() {
if (is_null($this->listeners)) {
$listeners = <?php echo $listenerName; ?>Query::create()
->filterByTarget('<?php echo $listenerName; ?>')
->where('ref_id IS NOT NULL')
->find();
$this->listeners = is_array($listeners) ? $listeners : array();
}

return $this->listeners;
}

private function saveEnqueuedListeners() {
if (is_array($this->listenerQueue)) {
$this->getListeners();
foreach ($this->listenerQueue as $l) {
$this->listeners[] = self::saveListener($l, $this->getPrimaryKey());
}
$this->listenerQueue = null;
}
}
Something went wrong with that request. Please try again.