public function send(array $message) { $result = []; $to = (array) array_value($message, 'to'); foreach ($to as $item) { $result[] = ['_id' => Utility::guid(false), 'email' => $item['email'], 'status' => 'sent']; } return $result; }
public function send(array $message) { // build recipients $to = []; $bcc = []; $toIncoming = (array) array_value($message, 'to'); foreach ($toIncoming as $item) { $type = array_value($item, 'type'); if ($type == 'bcc') { $bcc[$item['email']] = $item['name']; } else { $to[$item['email']] = $item['name']; } } if (count($to) === 0) { return []; } $fromEmail = array_value($message, 'from_email'); $fromName = array_value($message, 'from_name'); $swiftMessage = Swift_Message::newInstance()->setFrom([$fromEmail => $fromName])->setTo($to)->setBcc($bcc)->setSubject($message['subject'])->setBody($message['html'], 'text/html'); if (isset($message['text'])) { $swiftMessage->addPart($message['text'], 'text/plain'); } if (isset($message['headers']) && is_array($message['headers'])) { $headers = $swiftMessage->getHeaders(); foreach ($message['headers'] as $k => $v) { $headers->addTextHeader($k, $v); } } $sent = $this->swift->send($swiftMessage); $result = []; foreach ($message['to'] as $item) { $result[] = ['_id' => Utility::guid(false), 'email' => $item['email'], 'status' => $sent ? 'sent' : 'rejected']; } return $result; }
public function addVolunteer($req, $res) { $org = $this->getOrgForAdmin($req, $res); if (!is_object($org)) { return $org; } $success = false; if ($req->request('emails')) { $emails = explode("\n", $req->request('emails')); $n = 0; foreach ($emails as $email) { if ($org->inviteVolunteer($email)) { ++$n; } else { $this->app['errors']->push(['error' => 'could_not_add_volunteer_email', 'params' => ['email' => $email]]); } } $req->setParams(['numAdded' => $n]); $success = count($emails) == $n; if (!$success) { return $this->addVolunteerForm($req, $res); } } elseif ($req->files('import')) { $file = $req->files('import'); $n = 0; // check the upload is valid if (!is_array($file) || !U::array_value($file, 'error') === 0 || !U::array_value($file, 'size') > 0 || !U::array_value($file, 'tmp_name')) { $this->app['errors']->push(['error' => 'There was a problem with the upload.']); } else { $expFilename = explode('.', U::array_value($file, 'name')); $ext = strtolower(end($expFilename)); $new = U::guid() . '.' . $ext; $temp = INFUSE_BASE_DIR . '/temp/uploads/' . $new; // check extension if ($ext != 'csv') { $this->app['errors']->push(['error' => 'The file type is invalid. Only .csv files are allowed.']); } else { // move uploaded file to temp dir if (!move_uploaded_file($file['tmp_name'], $temp)) { $this->app['errors']->push(['error' => 'There was an error processing your upload.']); } else { // bugfix for csvs created on macs ini_set('auto_detect_line_endings', true); $columnMapping = []; $handle = fopen($temp, 'r'); if ($handle !== false) { $isFirst = true; while (($line = fgetcsv($handle, 1000, ',')) !== false) { if ($isFirst) { // determine column mapping foreach ($line as $field) { $columnMapping[] = $field; } $isFirst = false; continue; } // map csv columns to fields $fields = []; foreach ($columnMapping as $k => $field) { $fields[$field] = $line[$k]; } if (!isset($fields['email'])) { continue; } $volunteer = $org->inviteVolunteer($fields['email']); if ($volunteer) { unset($fields['email']); // add any meta-data to volunteer if (count($fields) > 0) { $volunteer->set('metadata', json_encode($fields)); } ++$n; } } fclose($handle); } // delete the temp file @unlink($temp); $success = true; $req->setParams(['numAdded' => $n]); } } } if (!$success) { return $this->addVolunteerImportForm($req, $res); } } if ($success) { return $this->volunteersBrowse($req, $res); } }
protected function preCreateHook(&$data) { $org = new Organization(U::array_value($data, 'organization')); // check creator permission $requester = $this->app['user']; $role = $org->getRoleOfUser($requester); if ($role < Volunteer::ROLE_VOLUNTEER && !$requester->isAdmin()) { $this->app['errors']->push(['error' => ERROR_NO_PERMISSION]); return false; } // volunteers cannot approve own hours if ($role < Volunteer::ROLE_ADMIN && !$requester->isAdmin()) { $data['approved'] = false; } // validate number of hours $hours = $data['hours'] = floor($data['hours']); if ($hours <= 0 || $hours >= 13) { $this->app['errors']->push(['error' => 'invalid_num_volunteer_hours']); return false; } // convert day timestamp to beginning of day $data['timestamp'] = self::timestampToStartOfDay($data['timestamp']); // the timestamp on hours cannot be more than 1 day in the future if ($data['timestamp'] - 86400 > time()) { $this->app['errors']->push(['error' => 'invalid_hours_timestamp']); return false; } // approval link if (!U::array_value($data, 'approved')) { $data['approval_link'] = U::guid(false); } if (isset($data['tags'])) { self::$createTags = $data['tags']; if (!is_array(self::$createTags)) { self::$createTags = explode(' ', self::$createTags); } } return true; }
public function preCreateHook(&$data) { $organization = new Organization(U::array_value($data, 'organization')); // In order to create volunteer models must be one of: // i) admin // ii) org admin // ii) current user creating a volunteer model for themselves $uid = U::array_value($data, 'uid'); $currentRole = $organization->getRoleOfUser($this->app['user']); $isAdmin = $this->app['user']->isAdmin() || $currentRole == self::ROLE_ADMIN; if (!$isAdmin && $uid != $this->app['user']->id()) { $this->app['errors']->push(['error' => ERROR_NO_PERMISSION]); return false; } // volunteers cannot be promoted beyond the role of the current user $maxLevel = $isAdmin ? self::ROLE_ADMIN : max(self::ROLE_AWAITING_APPROVAL, $currentRole); $role = U::array_value($data, 'role'); if ($role > $maxLevel) { $this->app['errors']->push(['error' => ERROR_NO_PERMISSION]); return false; } // approval link if ($role == self::ROLE_AWAITING_APPROVAL) { $data['approval_link'] = U::guid(false); } return true; }