diff --git a/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php b/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php index 4880ce24..08f59f56 100644 --- a/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php +++ b/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php @@ -4,6 +4,7 @@ require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/thread.php' require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/database.php'; require_once dirname(__FILE__) . '/thread_processor_mock.php'; require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/settings.php'; +require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/event_dispatcher.php'; require_once dirname(__FILE__) . '/../../../../../webim/libs/common/locale.php'; require_once dirname(__FILE__) . '/../config.php'; @@ -434,6 +435,11 @@ class ThreadTest extends PHPUnit_Framework_TestCase { unset($another_thread); + // Reload thread because values in database was changed + // in Thread::reopen method called above, but values in $thread object + // are still the same + $thread = Thread::load($thread->id); + // Try to reopen thread with Thread::STATE_QUEUE state $thread->nextAgent = 2; $thread->state = Thread::STATE_QUEUE; diff --git a/src/messenger/webim/libs/classes/thread.php b/src/messenger/webim/libs/classes/thread.php index 3e304d2e..4074b996 100644 --- a/src/messenger/webim/libs/classes/thread.php +++ b/src/messenger/webim/libs/classes/thread.php @@ -162,7 +162,7 @@ Class Thread { * Do not use this property manually! * @var array */ - protected $updatedFields = array(); + protected $changedFields = array(); /** * Forbid create instance from outside of the class @@ -412,8 +412,11 @@ Class Thread { /** * Implementation of the magic __set method * - * Check if variable with name $name exists in the Thread::$propertyMap array before setting. - * If it does not exist triggers an error with E_USER_NOTICE level and value will NOT set. + * Check if variable with name $name exists in the Thread::$propertyMap + * array before setting. If it does not exist triggers an error + * with E_USER_NOTICE level and value will NOT set. If previous value is + * equal to new value the property will NOT be update and NOT update in + * database when Thread::save method call. * * @param string $name Property name * @param mixed $value Property value @@ -427,10 +430,16 @@ Class Thread { } $field_name = $this->propertyMap[$name]; + + if (array_key_exists($field_name, $this->threadInfo) + && ($this->threadInfo[$field_name] === $value)) { + return; + } + $this->threadInfo[$field_name] = $value; - if (! in_array($field_name, $this->updatedFields)) { - $this->updatedFields[] = $field_name; + if (! in_array($name, $this->changedFields)) { + $this->changedFields[] = $name; } } @@ -550,16 +559,33 @@ Class Thread { $this->modified = time(); } + // Do not save thread if nothing changed + if (empty($this->changedFields)) { + return; + } + $values = array(); $set_clause = array(); - foreach ($this->updatedFields as $field_name) { - $set_clause[] = "{$field_name} = ?"; - $values[] = $this->threadInfo[$field_name]; + foreach ($this->changedFields as $field_name) { + $field_db_name = $this->propertyMap[$field_name]; + $set_clause[] = "{$field_db_name} = ?"; + $values[] = $this->threadInfo[$field_db_name]; } $query = "update {chatthread} t set " . implode(', ', $set_clause) . " where threadid = ?"; $values[] = $this->id; $db->query($query, $values); + + // Trigger thread changed event + $args = array( + 'thread' => $this, + 'changed_fields' => $this->changedFields + ); + $dispatcher = EventDispatcher::getInstance(); + $dispatcher->triggerEvent('threadChanged', $args); + + // Clear updated fields + $this->changedFields = array(); } /**