public function executeSend() { $param = $this->getAllRequestParameter(); /* VALIDATING */ if (empty($param['subject'])) { $this->jsonwrapper->show_json_error('#send-01-empty-subject', 'Empty Subject'); } if (empty($param['recipient'])) { $this->jsonwrapper->show_json_error('#send-01-empty-recipient', 'Empty Recipient'); } if (empty($param['message'])) { $this->jsonwrapper->show_json_error('#send-01-empty-message', 'Empty Message'); } if (empty($param['send_at']['type'])) { $this->jsonwrapper->show_json_error('#send-01-empty-sendat', 'Empty send_at'); } if ($param['send_at']['type'] == 'schedule') { if (empty($param['send_at']['date'])) { $this->jsonwrapper->show_json_error('#send-01-empty-senddate', 'Empty send date'); } } if (empty($param['sender_id'])) { $this->jsonwrapper->show_json_error('#send-01-empty-senderid', 'Empty sender id'); } $senderIdCriteria = new Criteria(); $senderIdCriteria->add(SmsSenderIdPeer::INITIAL, $param['sender_id'], Criteria::EQUAL); $senderId = SmsSenderIdPeer::doSelectOne($senderIdCriteria); /* Getting Employee Data */ $this->userInit(); $userId = $this->user->getAttribute('user_id', null, 'bo'); $cmsTools = new CmsTools(); $smsContact = new smsContact(); $smsContactEmployee = new smsContactEmployee(); $departmentAvailable = $cmsTools->departmentAvailable($this->department->getId()); /* Sorting Recipient */ $recipients = array('student' => array(), 'class' => array(), 'pbgroup' => array(), 'pbcontact' => array(), 'number' => array(), 'employee' => array()); foreach ($param['recipient'] as $r) { if (!empty($r['value'])) { $recipients[$r['type']][] = $r['value']; } } /* ------------------ */ /* MAPPING RECIPIENTS */ /* ------------------ */ /* Getting Students */ $students = $recipients['student'] ? $smsContact->getStudentsAvailable($recipients['student'], $departmentAvailable) : false; /* Getting Students Class */ $class = $recipients['class'] ? $smsContact->getStudentClassAvailable($recipients['class'], $departmentAvailable) : false; /* Getting Phonebook Contact */ $pbcontact = $recipients['pbcontact'] ? $smsContact->getPbContactAvailable($recipients['pbcontact'], $userId, $this->userType) : false; /* Getting Phonebook Group Contact */ $pbgroup = $recipients['pbgroup'] ? $smsContact->getPbGroupContactAvailable($recipients['pbgroup'], $userId, $this->userType) : false; /* Getting Employees */ $employees = $recipients['employee'] ? $smsContactEmployee->getEmployeesAvailable($recipients['employee'], $departmentAvailable) : false; /* Getting Number */ $number = $recipients['number']; /* ------------------ */ /* ------------------ */ /* ------------------ */ /* ------------------ */ /* Save SMS Log */ /* ------------------ */ $log = new SmsLog(); $log->setCreatorUserId($this->user->getAttribute('user_id', null, 'bo')); $log->setCreatorType($this->userType); $log->setSubject($param['subject']); $log->setRecipient($this->jsonwrapper->json_encode($param['recipient'])); $log->setMessage($param['message']); $log->setCreated(date('Y-m-d H:i:s')); $log->setSenderId($senderId->getId()); if ($param['send_at']['type'] == 'now') { $sendAt = date('Y-m-d H:i:s'); } else { $sendAt = date('Y-m-d H:i:s', strtotime($param['send_at']['date'] . ', ' . $param['send_at']['hour'] . ':' . $param['send_at']['minute'])); } $log->setSendAt($sendAt); $log->save(); if (!$log) { $this->jsonwrapper->show_json_error('#send-02-save', 'Failed save log'); } /* ------------------ */ /* ------------------ */ /* ------------------ */ /* ------------------ */ /* Save Log Recipient */ /* ------------------ */ /* Saving Students */ if ($students) { foreach ($students as $s) { $recNumber = $s->getStudentDetail()->getCellphone(); $rec = new SmsLogRecipient(); $rec->setLogId($log->getId()); $rec->setRecipientId($s->getId()); $rec->setRecipientType('student'); $rec->setRecipientNumber($recNumber); $rec->setStatus('pending'); $rec->setLog(0); $rec->save(); /* Saving to gammu Database */ $save = $this->sms->send($rec->getId(), 1, 2, 3, $recNumber, $log->getMessage(), $log->getCreated(), $log->getSendAt()); } } if ($employees) { foreach ($employees as $s) { $detail = $smsContactEmployee->getEmployeeDetail($s->getId()); $recNumber = $detail ? $detail->getPhone() : ''; $rec = new SmsLogRecipient(); $rec->setLogId($log->getId()); $rec->setRecipientId($s->getId()); $rec->setRecipientType('employee'); $rec->setRecipientNumber($recNumber); $rec->setStatus('pending'); $rec->setLog(0); $rec->save(); /* Saving to gammu Database */ $save = $this->sms->send($rec->getId(), 1, 2, 3, $recNumber, $log->getMessage(), $log->getCreated(), $log->getSendAt()); } } /* Saving Student Class */ if ($class) { foreach ($class as $c) { $recNumber = $c->getStudentDetail()->getCellphone(); $rec = new SmsLogRecipient(); $rec->setLogId($log->getId()); $rec->setRecipientId($c->getId()); $rec->setRecipientGroupId($c->getClassGroupId()); $rec->setRecipientType('student_class'); $rec->setRecipientNumber($recNumber); $rec->setStatus('pending'); $rec->setLog(0); $rec->save(); /* Saving to gammu Database */ $save = $this->sms->send($rec->getId(), 1, 2, 3, $recNumber, $log->getMessage(), $log->getCreated(), $log->getSendAt()); } } /* Saving pbContact */ if ($pbcontact) { foreach ($pbcontact as $p) { $recNumber = $p->getPhoneNumber(); $rec = new SmsLogRecipient(); $rec->setLogId($log->getId()); $rec->setRecipientId($p->getId()); $rec->setRecipientType('pbcontact'); $rec->setRecipientNumber($recNumber); $rec->setStatus('pending'); $rec->setLog(0); $rec->save(); /* Saving to gammu Database */ $save = $this->sms->send($rec->getId(), 1, 2, 3, $recNumber, $log->getMessage(), $log->getCreated(), $log->getSendAt()); } } /* Saving pbGroup */ if ($pbgroup) { foreach ($pbgroup as $p) { $recNumber = $p->getPhoneNumber(); $rec = new SmsLogRecipient(); $rec->setLogId($log->getId()); $rec->setRecipientId($p->getId()); $rec->setRecipientGroupId($p->getGroupId()); $rec->setRecipientType('pbgroup'); $rec->setRecipientNumber($recNumber); $rec->setStatus('pending'); $rec->setLog(0); $rec->save(); /* Saving to gammu Database */ $save = $this->sms->send($rec->getId(), 1, 2, 3, $recNumber, $log->getMessage(), $log->getCreated(), $log->getSendAt()); } } /* Saving number */ if ($number) { foreach ($number as $n) { $recNumber = $n; $rec = new SmsLogRecipient(); $rec->setLogId($log->getId()); $rec->setRecipientId($recNumber); $rec->setRecipientType('number'); $rec->setRecipientNumber($recNumber); $rec->setStatus('pending'); $rec->setLog(0); $rec->save(); /* Saving to gammu Database */ $save = $this->sms->send($rec->getId(), 1, 2, 3, $recNumber, $log->getMessage(), $log->getCreated(), $log->getSendAt()); } } $rc = new Criteria(); $rc->add(SmsLogRecipientPeer::LOG_ID, $log->getId(), Criteria::EQUAL); $recipientCount = SmsLogRecipientPeer::doCount($rc); $smsLong = ceil(strlen($param['message']) / $this->sms->smsLong()); $smsCount = $smsLong * $recipientCount; $log->setSmsLong($smsLong); $log->setSmsCount($smsCount); $log->save(); $this->jsonwrapper->print_json(array('success' => 1, 'data' => $log->toArray())); }