Exemplo n.º 1
0
 /**
  * get all activities which are active
  * @param xml object $data data to be updated
  * @return array
  */
 public function getListAcivities($data)
 {
     $this->_documentClassName = 'MoveIn4Activity\\Document\\ActivityDocument';
     $results = parent::findAllService($data);
     foreach ($results['results'] as &$row) {
         if (isset($row['owner.id'])) {
             $user = $this->_dm->find('MoveIn4User\\Document\\UserDocument', $row['owner.id']);
             $pic = $user->getProfilePic();
             $row['ownerphoto'] = !empty($pic) ? $pic->getFileStorageId() : '';
             $row['owner'] = $user->getFirstName();
         }
         if (isset($row['fromPerson.id'])) {
             $person = $this->_dm->find('MoveIn4Person\\Document\\PersonDocument', $row['fromPerson.id']);
             $row['frompersonphoto'] = $person->getPhoto();
         }
         $row = $this->cleanResult($row);
     }
     return $results;
 }
 /**
  * Creates an new ActivityIncomingEmailDocument for each new email imported
  * @param  array $data data to be manipulated
  * @return array
  */
 public function importIncomingEmail($data)
 {
     // for each email, create an activity
     $result = array();
     $router = $this->serviceManager->get('platform-router');
     $matchedPersons = array();
     foreach ($data->rows as $email) {
         //initilize the array of new activities
         $activityId = array();
         $dateRecieved = new \DateTime((string) $email->email_date);
         $body = $router->getFileResource((int) $email->email_body['fileRef']);
         $bodyPlain = $router->getFileResource((int) $email->email_body_plain['fileRef']);
         //get email data
         $emailData = array('emailImportId' => (int) $email->email_id, 'subject' => (string) $email->email_subject, 'messageHtml' => iconv(mb_detect_encoding((string) $body['file'], mb_detect_order(), true), "UTF-8//IGNORE", (string) $body['file']), 'message' => iconv(mb_detect_encoding((string) $bodyPlain['file'], mb_detect_order(), true), "UTF-8//IGNORE", (string) $bodyPlain['file']), 'fromEmailString' => (string) $email->email_from, 'dateRecieved' => $dateRecieved->format('d/m/Y h:i:A'), 'emailConfig' => (string) $data->emailConfigId);
         //get the attachments
         $attachmentArray = array();
         foreach ($email->email_attachment as $att) {
             $attachmentArray['file' . (string) $att->id] = array('id' => (string) $att->id, 'fileName' => (string) $att->filename, 'file' => $att->file);
         }
         //check to see if the person exists in the system
         //get the email address
         $emailAddress = explode('<', (string) $email->email_from);
         $emailAddress = rtrim($emailAddress[count($emailAddress) - 1], '>');
         if (!isset($matchedPersons[$emailAddress])) {
             //query for persons
             $query = $this->_dm->createQueryBuilder('MoveIn4Person\\Document\\PersonDocument')->field('active')->equals(true)->field('deleted')->equals(false)->field('email.email')->equals($emailAddress);
             $personResult = $query->getQuery()->execute();
             foreach ($personResult as $person) {
                 $matchedPersons[$emailAddress][] = $person->getId();
             }
         }
         //if matching person is found
         if (isset($matchedPersons[$emailAddress])) {
             foreach ($matchedPersons[$emailAddress] as $person) {
                 $message = new Message(array('env' => (string) $data->auth->env, 'inst' => (string) $data->auth->inst, 'userId' => (string) $data->auth->userId));
                 $emailData['fromPerson'] = $person;
                 $message->addAction('null', 'null', $emailData);
                 $action = $message->getNextAction();
                 $activityId[] = parent::edit($action[0]->data);
             }
             //if no matching person found
         } else {
             $message = new Message(array('env' => (string) $data->auth->env, 'inst' => (string) $data->auth->inst, 'userId' => (string) $data->auth->userId));
             $message->addAction('null', 'null', $emailData);
             $action = $message->getNextAction();
             $activityId[] = parent::edit($action[0]->data);
         }
         $result[] = $activityId;
         $auth = array('env' => (string) $data->auth->env, 'inst' => (string) $data->auth->inst, 'userId' => (string) $data->auth->userId);
         if (count($attachmentArray)) {
             //foreach attachment save in file storage and add to activity
             foreach ($attachmentArray as $attachment) {
                 $message = new Message($auth);
                 //save file
                 $mergeFileData = array('id' => null, 'file_attr' => array('fileRef' => (string) $attachment['file']["fileRef"]), 'options' => array('filename' => $attachment['fileName'], 'pathPrepend' => array($auth['env'], $auth['inst'])));
                 $fileStorage = $message->addAction('filestorage', 'saveFile', $mergeFileData, 'FileStorage');
                 //update activity
                 foreach ($activityId as $aId) {
                     $updateData['id'] = $aId['id'];
                     $updateData['attachment'] = array('fileName' => $attachment['fileName'], 'fileStorageId_attr' => array('xpath' => '//response/action[@id="' . $fileStorage . '"]/data/docId'));
                     $editId = $message->addAction('movein', 'editIncommingEmail', $updateData, 'MoveIn4Activity\\Model\\ActivityIncomingEmail');
                 }
                 $router->queue($message->asXml());
             }
         }
     }
     return array('rows' => $result);
 }
 /**
  * Factory method to create Model instance
  * @see \Zend\ServiceManager\FactoryInterface::createService()
  * @param ServiceLocatorInterface $serviceLocator Service locator
  * @return void
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $activity = new ActivityBaseService();
     $activity->setDocumentManager($serviceLocator->get('doctrine.documentmanager.odm_default'));
     return $activity;
 }