예제 #1
0
 public function save(Entity $entity)
 {
     $result = parent::save($entity);
     $accountIdChanged = $entity->has('accountId') && $entity->get('accountId') != $entity->getFetched('accountId');
     $titleChanged = $entity->has('title') && $entity->get('title') != $entity->getFetched('title');
     if ($accountIdChanged) {
         $accountId = $entity->get('accountId');
         if (empty($accountId)) {
             $this->unrelate($entity, 'accounts', $entity->getFetched('accountId'));
             return $result;
         }
     }
     if ($titleChanged) {
         if (empty($accountId)) {
             $accountId = $entity->getFetched('accountId');
             if (empty($accountId)) {
                 return $result;
             }
         }
     }
     if ($accountIdChanged || $titleChanged) {
         $pdo = $this->getEntityManager()->getPDO();
         $sql = "\n\t\t\t\tSELECT id, role FROM account_contact\n\t\t\t\tWHERE \n\t\t\t\t\taccount_id = " . $pdo->quote($accountId) . " AND\n\t\t\t\t\tcontact_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\tdeleted = 0\t\t\t\t\t\n\t\t\t";
         $sth = $pdo->prepare($sql);
         if ($row = $sth->fetch()) {
             if ($titleChanged && $entity->get('title') != $row['role']) {
                 $this->updateRelation($entity, 'accounts', $accountId, array('role' => $entity->get('title')));
             }
         } else {
             $this->relate($entity, 'accounts', $accountId, array('role' => $entity->get('title')));
         }
     }
     return $result;
 }
예제 #2
0
 protected function beforeSave(Entity $entity, array $options)
 {
     parent::beforeSave($entity, $options);
     if ($entity->isNew()) {
         $userName = $entity->get('userName');
         if (empty($userName)) {
             throw new Error();
         }
         $user = $this->where(array('userName' => $userName))->findOne();
         if ($user) {
             throw new Error();
         }
     } else {
         if ($entity->isFieldChanged('userName')) {
             $userName = $entity->get('userName');
             if (empty($userName)) {
                 throw new Error();
             }
             $user = $this->where(array('userName' => $userName, 'id!=' => $entity->id))->findOne();
             if ($user) {
                 throw new Error();
             }
         }
     }
 }
예제 #3
0
파일: Lead.php 프로젝트: naushrambo/espocrm
 public function afterSave(Entity $entity, array $options)
 {
     parent::afterSave($entity, $options);
     if ($entity->has('targetListId')) {
         $this->relate($entity, 'targetLists', $entity->get('targetListId'));
     }
 }
예제 #4
0
 protected function beforeRemove(Entity $entity)
 {
     parent::beforeRemove($entity);
     $attachments = $entity->get('attachments');
     foreach ($attachments as $attachment) {
         $this->getEntityManager()->removeEntity($attachment);
     }
 }
예제 #5
0
파일: Lead.php 프로젝트: lucasmattos/crm
 public function handleSelectParams(&$params)
 {
     parent::handleSelectParams($params);
     if (empty($params['customJoin'])) {
         $params['customJoin'] = '';
     }
     $params['customJoin'] .= " \n\t\t\tLEFT JOIN currency ON currency.id = lead.opportunity_amount_currency\n\t\t";
 }
예제 #6
0
 public function afterRemove(Entity $entity, $options)
 {
     parent::afterRemove($entity, $options);
     $pdo = $this->getEntityManager()->getPDO();
     $query = $this->getEntityManager()->getQuery();
     $pathsTableName = $query->toDb($entity->getEntityType() . 'Path');
     $sql = "DELETE FROM `" . $pathsTableName . "` WHERE descendor_id = " . $pdo->quote($entity->id) . "";
     $pdo->query($sql);
 }
예제 #7
0
 public function get($id = null)
 {
     $entity = parent::get($id);
     if (empty($entity) && !empty($id)) {
         $entity = $this->get();
         $entity->id = $id;
     }
     return $entity;
 }
예제 #8
0
 protected function beforeSave(Entity $entity, array $options)
 {
     parent::beforeSave($entity, $options);
     if ($entity->isFieldChanged('status')) {
         if ($entity->get('status') == 'Completed') {
             $entity->set('dateCompleted', date('Y-m-d H:i:s'));
         } else {
             $entity->set('dateCompleted', null);
         }
     }
     if ($entity->has('dateStartDate')) {
         $dateStartDate = $entity->get('dateStartDate');
         if (!empty($dateStartDate)) {
             $dateStart = $dateStartDate . ' 00:00:00';
             $dateStart = $this->convertDateTimeToDefaultTimezone($dateStart);
             $entity->set('dateStart', $dateStart);
         } else {
             $entity->set('dateStartDate', null);
         }
     }
     if ($entity->has('dateEndDate')) {
         $dateEndDate = $entity->get('dateEndDate');
         if (!empty($dateEndDate)) {
             $dateEnd = $dateEndDate . ' 00:00:00';
             $dateEnd = $this->convertDateTimeToDefaultTimezone($dateEnd);
             $entity->set('dateEnd', $dateEnd);
         } else {
             $entity->set('dateEndDate', null);
         }
     }
     $parentId = $entity->get('parentId');
     $parentType = $entity->get('parentType');
     if (!empty($parentId) || !empty($parentType)) {
         $parent = $this->getEntityManager()->getEntity($parentType, $parentId);
         if (!empty($parent)) {
             $accountId = null;
             if ($parent->getEntityType() == 'Account') {
                 $accountId = $parent->id;
             } else {
                 if ($parent->get('accountId')) {
                     $accountId = $parent->get('accountId');
                 } else {
                     if ($parent->getEntityType() == 'Lead') {
                         if ($parent->get('status') == 'Converted') {
                             if ($parent->get('createdAccountId')) {
                                 $accountId = $parent->get('createdAccountId');
                             }
                         }
                     }
                 }
             }
             if (!empty($accountId)) {
                 $entity->set('accountId', $accountId);
             }
         }
     }
 }
예제 #9
0
 public function afterSave(Entity $entity, array $options)
 {
     $result = parent::afterSave($entity, $options);
     $this->handleAfterSaveAccounts($entity, $options);
     if ($entity->has('targetListId') && $entity->isNew()) {
         $this->relate($entity, 'targetLists', $entity->get('targetListId'));
     }
     return $result;
 }
예제 #10
0
 protected function afterSave(Entity $entity, array $options)
 {
     parent::afterSave($entity, $options);
     if ($entity->has('isDefault')) {
         if ($entity->get('isDefault')) {
             $this->getConfig()->set('defaultPortalId', $entity->id);
         } else {
             $this->getConfig()->set('defaultPortalId', null);
         }
         $this->getConfig()->save();
     }
 }
예제 #11
0
 protected function afterRemove(Entity $entity, array $options = array())
 {
     if ($entity->get('fileId')) {
         $attachment = $this->getEntityManager()->getEntity('Attachment', $entity->get('fileId'));
         if ($attachment) {
             $this->getEntityManager()->removeEntity($attachment);
         }
     }
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "DELETE FROM import_entity WHERE import_id = :importId";
     $sth = $pdo->prepare($sql);
     $sth->bindValue(':importId', $entity->id);
     $sth->execute();
     parent::afterRemove($entity, $options);
 }
예제 #12
0
파일: User.php 프로젝트: disearth/espocrm
 protected function beforeSave(Entity $entity, array $options)
 {
     parent::beforeSave($entity, $options);
     if ($entity->isNew()) {
         $userName = $entity->get('userName');
         if (empty($userName)) {
             throw new Error();
         }
         $user = $this->where(array('userName' => $userName))->findOne();
         if ($user) {
             throw new Error();
         }
     } else {
         if ($entity->isFieldChanged('userName')) {
             $userName = $entity->get('userName');
             if (empty($userName)) {
                 throw new Error();
             }
             $user = $this->where(array('userName' => $userName, 'id!=' => $entity->id))->findOne();
             if ($user) {
                 throw new Error();
             }
         }
     }
     if ($entity->has('isAdmin') && $entity->get('isAdmin')) {
         $entity->set('isPortalUser', false);
         $entity->set('portalRolesIds', []);
         $entity->set('portalRolesNames', (object) []);
         $entity->set('portalsIds', []);
         $entity->set('portalsNames', (object) []);
     }
     if ($entity->has('isPortalUser') && $entity->get('isPortalUser')) {
         $entity->set('isAdmin', false);
         $entity->set('rolesIds', []);
         $entity->set('rolesNames', (object) []);
         $entity->set('teamsIds', []);
         $entity->set('teamsNames', (object) []);
         $entity->set('defaultTeamId', null);
         $entity->set('defaultTeamName', null);
     }
 }
예제 #13
0
 protected function beforeSave(Entity $entity)
 {
     parent::beforeSave($entity);
     $parentId = $entity->get('parentId');
     $parentType = $entity->get('parentType');
     if (!empty($parentId) || !empty($parentType)) {
         $parent = $this->getEntityManager()->getEntity($parentType, $parentId);
         if (!empty($parent)) {
             if ($parent->getEntityName() == 'Account') {
                 $accountId = $parent->id;
             } else {
                 if ($parent->has('accountId')) {
                     $accountId = $parent->get('accountId');
                 }
             }
             if (!empty($accountId)) {
                 $entity->set('accountId', $accountId);
             }
         }
     }
 }
예제 #14
0
 public function afterSave(Entity $entity, array $options)
 {
     $result = parent::afterSave($entity, $options);
     $this->handleAfterSaveContacts($entity, $options);
     return $result;
 }
예제 #15
0
 protected function afterSave(Entity $entity, array $options)
 {
     parent::afterSave($entity, $options);
     if (!$entity->isNew()) {
         if ($entity->get('parentType') && $entity->get('parentId') && $entity->isFieldChanged('parentId')) {
             $replyList = $this->findRelated($entity, 'replies');
             foreach ($replyList as $reply) {
                 if ($reply->id === $entity->id) {
                     continue;
                 }
                 if (!$reply->get('parentId')) {
                     $reply->set(array('parentId' => $entity->get('parentId'), 'parentType' => $entity->get('parentType')));
                     $this->getEntityManager()->saveEntity($reply);
                 }
             }
         }
     }
 }
예제 #16
0
 protected function afterRemove(Entity $entity)
 {
     parent::afterRemove($entity);
     $this->getFileManager()->removeFile('data/upload/' . $entity->id);
 }
예제 #17
0
 protected function getNewEntity()
 {
     $entity = parent::getNewEntity();
     $entity->set('name', uniqid());
     return $entity;
 }
예제 #18
0
 protected function afterSave(Entity $entity, array $options)
 {
     parent::afterSave($entity, $options);
     if ($entity->isNew() || $entity->isFieldChanged('assignedUserId') || $entity->isFieldChanged('dateStart') || $entity->has('reminders')) {
         $pdo = $this->getEntityManager()->getPDO();
         $reminderTypeList = $this->getMetadata()->get('entityDefs.Reminder.fields.type.options');
         if (!$entity->has('reminders')) {
             $reminders = $this->getEntityReminders($entity);
         } else {
             $reminders = $entity->get('reminders');
         }
         if (!$entity->isNew()) {
             $sql = "\n                    DELETE FROM `reminder`\n                    WHERE\n                        entity_id = " . $pdo->quote($entity->id) . " AND\n                        entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n                        deleted = 0\n                ";
             $pdo->query($sql);
         }
         if (empty($reminders) || !is_array($reminders)) {
             return;
         }
         $entityType = $entity->getEntityName();
         $dateStart = $entity->get('dateStart');
         $assignedUserId = $entity->get('assignedUserId');
         if (!$dateStart || !$assignedUserId) {
             $e = $this->get($entity->id);
             if ($e) {
                 $dateStart = $e->get('dateStart');
                 $assignedUserId = $e->get('assignedUserId');
             }
         }
         if (!$dateStart || !$assignedUserId) {
             return;
         }
         $dateStartObj = new \DateTime($dateStart);
         if (!$dateStartObj) {
             return;
         }
         foreach ($reminders as $item) {
             $remindAt = clone $dateStartObj;
             $seconds = intval($item->seconds);
             $type = $item->type;
             if (!in_array($type, $reminderTypeList)) {
                 continue;
             }
             $remindAt->sub(new \DateInterval('PT' . $seconds . 'S'));
             $id = uniqid();
             $sql = "\n                    INSERT\n                    INTO `reminder`\n                    (id, entity_id, entity_type, `type`, user_id, remind_at, start_at, `seconds`)\n                    VALUES (\n                        " . $pdo->quote($id) . ",\n                        " . $pdo->quote($entity->id) . ",\n                        " . $pdo->quote($entityType) . ",\n                        " . $pdo->quote($type) . ",\n                        " . $pdo->quote($assignedUserId) . ",\n                        " . $pdo->quote($remindAt->format('Y-m-d H:i:s')) . ",\n                        " . $pdo->quote($dateStart) . ",\n                        " . $pdo->quote($seconds) . "\n                    )\n                ";
             $pdo->query($sql);
         }
     }
 }
예제 #19
0
 protected function afterRemove(Entity $entity, array $options = array())
 {
     parent::afterRemove($entity, $options);
     $this->getFileManager()->removeFile('data/upload/' . $entity->id);
 }