Skip to content

Commit

Permalink
Merge pull request #2979 from jeedom/listener-enhancements-toStable
Browse files Browse the repository at this point in the history
make listener enhancements available to stable
  • Loading branch information
Salvialf authored Nov 15, 2024
2 parents f280b6e + 8157798 commit a961c60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
39 changes: 18 additions & 21 deletions core/class/listener.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@ public static function clean() {
if (count($events) > 0) {
$listener->emptyEvent();
foreach ($events as $event) {
if ($event == '#*#') {
$listener->addEvent('#*#');
} else {
$cmd = cmd::byId(str_replace('#', '', $event));
if (is_object($cmd)) {
$listener->addEvent($cmd->getId());
}
if (strpos($event, '*') !== false || strpos($event, '::') !== false || is_object(cmd::byId(trim($event, '#')))) {
$listener->addEvent($event);
}
}
$listener->save();
Expand All @@ -54,11 +49,11 @@ public static function clean() {
$listener->remove();
}
}
$sql = 'SELECT '.DB::buildField(__CLASS__).'
FROM listener GROUP BY class, function, event, option
$sql = 'SELECT ' . DB::buildField(__CLASS__) . '
FROM listener GROUP BY class, function, event, option
HAVING count(*) > 1';
$duplicateds = DB::Prepare($sql, array(), DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__);
if(count($duplicateds) > 0){
if (count($duplicateds) > 0) {
foreach ($duplicateds as $duplicated) {
$value = array(
'class' => $duplicated->getClass(),
Expand All @@ -73,7 +68,7 @@ public static function clean() {
AND `option`=:option
AND `event`=:event';
$listeners = DB::Prepare($sql, $value, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__);
for($i=1;$i<count($listeners);$i++){
for ($i = 1; $i < count($listeners); $i++) {
$listeners[$i]->remove();
}
}
Expand Down Expand Up @@ -206,11 +201,11 @@ public static function searchEvent($_event) {
return DB::Prepare($sql, $value, DB::FETCH_TYPE_ALL, PDO::FETCH_CLASS, __CLASS__);
}

public static function check($_event, $_value, $_datetime = null) {
public static function check($_event, $_value, $_datetime = null, $_object = null) {
$listeners = self::searchEvent($_event);
if (is_array($listeners) && count($listeners) > 0) {
foreach ($listeners as $listener) {
$listener->run(str_replace('#', '', $_event), $_value, $_datetime);
$listener->run(str_replace('#', '', $_event), $_value, $_datetime, $_object);
}
}
}
Expand All @@ -233,13 +228,13 @@ public function getName() {

/* * *********************Méthodes d'instance************************* */

public function run($_event, $_value, $_datetime = null) {
public function run($_event, $_value, $_datetime = null, $_object = null) {
$option = array();
if (count($this->getOption()) > 0) {
$option = $this->getOption();
}
if (isset($option['background']) && $option['background'] == false) {
$this->execute($_event, $_value, $_datetime);
$this->execute($_event, $_value, $_datetime, $_object);
} else {
$cmd = __DIR__ . '/../php/jeeListener.php';
$cmd .= ' listener_id=' . $this->getId() . ' event_id=' . $_event . ' "value=' . escapeshellarg($_value) . '"';
Expand All @@ -250,7 +245,7 @@ public function run($_event, $_value, $_datetime = null) {
}
}

public function execute($_event, $_value, $_datetime = '') {
public function execute($_event, $_value, $_datetime = '', $_object = null) {
try {
$option = array();
if (count($this->getOption()) > 0) {
Expand All @@ -259,6 +254,7 @@ public function execute($_event, $_value, $_datetime = '') {
$option['event_id'] = $_event;
$option['value'] = $_value;
$option['datetime'] = $_datetime;
$option['object'] = $_object;
$option['listener_id'] = $this->getId();
if ($this->getClass() != '') {
$class = $this->getClass();
Expand All @@ -276,11 +272,12 @@ public function execute($_event, $_value, $_datetime = '') {
$function($option);
} else {
log::add('listener', 'error', __('[Erreur] Fonction non trouvée', __FILE__) . ' ' . json_encode(utils::o2a($this)));
$this->remove();
return;
}
}
} catch (Exception $e) {
log::add(init('plugin_id', 'plugin'), 'error', $e->getMessage());
log::add(init('plugin_id', 'plugin'), 'error', log::exception($e));
}
}

Expand All @@ -304,20 +301,20 @@ public function remove() {

public function emptyEvent() {
$this->event = array();
return $this;
}

public function addEvent($_id, $_type = 'cmd') {
public function addEvent($_id) {
$event = $this->getEvent();
if (!is_array($event)) {
$event = array();
}
if ($_type == 'cmd') {
$id = str_replace('#', '', $_id);
}
$id = trim($_id, '#');
if (!in_array('#' . $id . '#', $event)) {
$event[] = '#' . $id . '#';
}
$this->setEvent($event);
return $this;
}

/* * **********************Getteur Setteur*************************** */
Expand Down
1 change: 1 addition & 0 deletions docs/fr_FR/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# 4.4.19

- Prise en charge des évènements personnalisés de type ``#pluginClass::*#`` ou ``#pluginClass::customId#`` [LIEN](https://github.com/jeedom/core/pull/2964)
- Ajout de la sauvegarde du cache avant mise à jour (sera nécessaire pour ne pas perdre le cache lors du passage à Jeedom 4.5) [LIEN](https://github.com/jeedom/core/commit/37351c84c186bd3f1b9da337d0cc68a66c80ef2d)

# 4.4.18
Expand Down

0 comments on commit a961c60

Please sign in to comment.