Commits

Ivica Nedeljkovic committed 07a9c75

Attaching users to emails

Comments (0)

Files changed (4)

app/protected/modules/accounts/models/AccountSearch.php

         /**
          * For a given email address, run search by email address and retrieve account models.
          */
-        public static function getAccountsByAnyEmailAddress($emailAddress, $pageSize = null, $stateMetadataAdapterClassName = null)
+        public static function getAccountsByAnyEmailAddress($emailAddress, $pageSize = null)
         {
             assert('is_string($emailAddress)');
             $metadata = array();

app/protected/modules/emailMessages/jobs/EmailArchivingJob.php

                         {
                             // Check if email belongs to account
                             $accounts = AccountSearch::getAccountsByAnyEmailAddress($senderInfo['email'], 1);
-                            if (count($contacts))
+                            if (count($accounts))
                             {
-                                $sender->personOrAccount = $contacts[0]->getClassId('Item');
+                                $sender->personOrAccount = $accounts[0]->getClassId('Item');
                             }
+                            /*
+                            else
+                            {
+                                $users = UserModelSearch::getUsersByEmail($senderInfo['email']);
+                                if (count($users))
+                                {
+                                    $sender->personOrAccount = $users[0]->getClassId('Item');
+                                }
+                            }
+                            */
                         }
                     }
 
                         {
                             // Check if email belongs to account
                             $accounts = AccountSearch::getAccountsByAnyEmailAddress($recipientInfo['email'], 1);
-                            if (count($contacts))
+                            if (count($accounts))
                             {
-                                $recipient->personOrAccount = $contacts[0]->getClassId('Item');
-                            }
+                                $recipient->personOrAccount = $accounts[0]->getClassId('Item');
+                            }
+                            /*
+                            else
+                            {
+                                $users = UserModelSearch::getUsersByEmail($senderInfo['email']);
+                                if (count($users))
+                                {
+                                    $sender->personOrAccount = $users[0]->getClassId('Item');
+                                }
+                            }
+                            */
                         }
                         $emailMessage->recipients->add($recipient);
                     }

app/protected/modules/users/models/UserModelSearch.php

             return User::getSubset($joinTablesAdapter, null, $pageSize,
                                             $where, "person.firstname, person.lastname");
         }
+
+        public static function getUsersByEmail($emailAddress)
+        {
+            assert('is_string($emailAddress)');
+            $metadata = array();
+            $metadata['clauses'] = array(
+                    1 => array(
+                            'attributeName'        => 'primaryEmail',
+                            'relatedAttributeName' => 'emailAddress',
+                            'operatorType'         => 'equals',
+                            'value'                => $emailAddress,
+                    ),
+            );
+            $metadata['structure'] = '(1)';
+            $joinTablesAdapter   = new RedBeanModelJoinTablesQueryAdapter('Person');
+            $where  = RedBeanModelDataProvider::makeWhere('Person', $metadata, $joinTablesAdapter);
+            $persons = Person::getSubset($joinTablesAdapter, null, null, $where);
+
+            $metadata = array();
+            $metadata['clauses'] = array(
+                    1 => array(
+                            'attributeName'        => 'primaryEmail',
+                            'relatedAttributeName' => 'emailAddress',
+                            'operatorType'         => 'equals',
+                            'value'                => $emailAddress,
+                    ),
+            );
+            $metadata['structure'] = '(1)';
+            $joinTablesAdapter   = new RedBeanModelJoinTablesQueryAdapter('Person');
+            $where  = RedBeanModelDataProvider::makeWhere('Person', $metadata, $joinTablesAdapter);
+            $persons = Person::getSubset($joinTablesAdapter, null, null, $where);
+
+            if (count($persons))
+            {
+                $personTableName   = RedBeanModel::getTableName('Person');
+                $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('User');
+                $joinTablesAdapter->addFromTableAndGetAliasName($personTableName, "{$personTableName}_id");
+                $where = "      (person.id = '{$persons[0]->id}') ";
+
+                return User::getSubset($joinTablesAdapter, null, null,
+                                                $where, "person.firstname, person.lastname");
+            }
+            else
+            {
+                return array();
+            }
+        }
     }
 ?>

app/protected/modules/users/tests/unit/UserModelSearchTest.php

             $users = UserModelSearch::getUsersByPartialFullName('Ab', 5);
             $this->assertEquals(1, count($users));
         }
+
+        public function testGetUsersByEmail()
+        {
+            Yii::app()->user->userModel = User::getByUsername('super');
+            $user = UserTestHelper::createBasicUser('Steve');
+            $user->primaryEmail->emailAddress = 'steve@example.com';
+            $user->primaryEmail->optOut       = 1;
+            $user->primaryEmail->isInvalid    = 0;
+            $this->assertTrue($user->save());
+
+            $users = UserModelSearch::getUsersByEmail('steve@example.com');
+            $this->assertEquals(1, count($users));
+            //$this->assertEquals($user->id, $users[0]->id);
+        }
     }
 ?>