From 0faf0f4fbd636cab9dfebbc3918b839f480ee763 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 5 Sep 2018 10:17:54 +0200 Subject: [PATCH] Minor - message.user_receiver_id can have a NULL value used --- app/Migrations/Schema/V200/Version20.php | 8 +++++--- main/inc/lib/message.lib.php | 10 ++++++---- src/CoreBundle/Entity/Message.php | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Migrations/Schema/V200/Version20.php b/app/Migrations/Schema/V200/Version20.php index 7086fdce08..bfad6c6a81 100644 --- a/app/Migrations/Schema/V200/Version20.php +++ b/app/Migrations/Schema/V200/Version20.php @@ -169,10 +169,12 @@ class Version20 extends AbstractMigrationChamilo $this->addSql('UPDATE session_category SET date_end = NULL WHERE date_end = "0000-00-00"'); $this->addSql('DELETE FROM message WHERE user_sender_id IS NULL OR user_sender_id = 0'); - $this->addSql('DELETE FROM message WHERE user_receiver_id IS NULL OR user_sender_id = 0'); + + $this->addSql('ALTER TABLE message CHANGE user_receiver_id user_receiver_id INT DEFAULT NULL'); + $this->addSql('UPDATE message SET user_receiver_id = NULL WHERE user_receiver_id = 0'); $this->addSql('DELETE FROM message WHERE user_sender_id NOT IN (SELECT id FROM user)'); - $this->addSql('DELETE FROM message WHERE user_receiver_id NOT IN (SELECT id FROM user)'); + $this->addSql('DELETE FROM message WHERE user_receiver_id IS NOT NULL AND user_receiver_id NOT IN (SELECT id FROM user)'); $this->addSql('ALTER TABLE message ADD CONSTRAINT FK_B6BD307FF6C43E79 FOREIGN KEY (user_sender_id) REFERENCES user (id)'); $this->addSql('ALTER TABLE message ADD CONSTRAINT FK_B6BD307F64482423 FOREIGN KEY (user_receiver_id) REFERENCES user (id)'); @@ -502,7 +504,7 @@ class Version20 extends AbstractMigrationChamilo // Portfolio if (!$schema->hasTable('portfolio')) { - $this->addSql('CREATE TABLE portfolio_category (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, title LONGTEXT NOT NULL, description LONGTEXT DEFAULT NULL, is_visible TINYINT(1) DEFAULT "1" NOT NULL, INDEX user (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;'); + $this->addSql('CREATE TABLE portfolio_category (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, title LONGTEXT DEFAULT NULL, description LONGTEXT DEFAULT NULL, is_visible TINYINT(1) DEFAULT "1" NOT NULL, INDEX user (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;'); $this->addSql('CREATE TABLE portfolio (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, c_id INT DEFAULT NULL, session_id INT DEFAULT NULL, category_id INT DEFAULT NULL, title LONGTEXT NOT NULL, content LONGTEXT NOT NULL, creation_date DATETIME NOT NULL, update_date DATETIME NOT NULL, is_visible TINYINT(1) DEFAULT "1" NOT NULL, INDEX user (user_id), INDEX course (c_id), INDEX session (session_id), INDEX category (category_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;'); $this->addSql('ALTER TABLE portfolio_category ADD CONSTRAINT FK_7AC64359A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'); $this->addSql('ALTER TABLE portfolio ADD CONSTRAINT FK_A9ED1062A76ED395 FOREIGN KEY (user_id) REFERENCES user (id);'); diff --git a/main/inc/lib/message.lib.php b/main/inc/lib/message.lib.php index 1b5ca5bb55..a99b84473e 100755 --- a/main/inc/lib/message.lib.php +++ b/main/inc/lib/message.lib.php @@ -464,7 +464,7 @@ class MessageManager return false; } elseif ($totalFileSize > intval(api_get_setting('message_max_upload_filesize'))) { $warning = sprintf( - get_lang("FilesSizeExceedsX"), + get_lang('FilesSizeExceedsX'), format_file_size(api_get_setting('message_max_upload_filesize')) ); @@ -481,15 +481,14 @@ class MessageManager //@todo it's possible to edit a message? yes, only for groups if (!empty($editMessageId)) { $query = " UPDATE $table SET - update_date = '".$now."', - content = '".Database::escape_string($content)."' + update_date = '".$now."', + content = '".Database::escape_string($content)."' WHERE id = '$editMessageId' "; Database::query($query); $messageId = $editMessageId; } else { $params = [ 'user_sender_id' => $user_sender_id, - 'user_receiver_id' => $receiver_user_id, 'msg_status' => MESSAGE_STATUS_UNREAD, 'send_date' => $now, 'title' => $subject, @@ -498,6 +497,9 @@ class MessageManager 'parent_id' => $parent_id, 'update_date' => $now, ]; + if (!empty($receiver_user_id)) { + $params['user_receiver_id'] = $receiver_user_id; + } $messageId = Database::insert($table, $params); } diff --git a/src/CoreBundle/Entity/Message.php b/src/CoreBundle/Entity/Message.php index 8965e0bda1..c5fbea068e 100644 --- a/src/CoreBundle/Entity/Message.php +++ b/src/CoreBundle/Entity/Message.php @@ -44,7 +44,7 @@ class Message * @var User * * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="receivedMessages") - * @ORM\JoinColumn(name="user_receiver_id", referencedColumnName="id", nullable=false) + * @ORM\JoinColumn(name="user_receiver_id", referencedColumnName="id", nullable=true) */ protected $userReceiver;