Exemple #1
0
 public function executeRegister($request)
 {
     $userParams = $request->getParameter('api_user');
     $this->user_form = new ApiUserForm();
     $this->created = false;
     if ($request->isMethod('post')) {
         //bind request params to form
         $captcha = array('recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'), 'recaptcha_response_field' => $request->getParameter('recaptcha_response_field'));
         $userParams = array_merge($userParams, array('captcha' => $captcha));
         $this->user_form->bind($userParams);
         //look for user with duplicate email
         $q = LsDoctrineQuery::create()->from('ApiUser u')->where('u.email = ?', $userParams['email']);
         if ($q->count()) {
             $validator = new sfValidatorString(array(), array('invalid' => 'There is already an API user with that email address.'));
             $this->user_form->getErrorSchema()->addError(new sfValidatorError($validator, 'invalid'), 'email');
             $request->setError('email', 'There is already a user with that email');
         }
         if ($this->user_form->isValid() && !$request->hasErrors()) {
             //create inactive api user
             $user = new ApiUser();
             $user->name_first = $userParams['name_first'];
             $user->name_last = $userParams['name_last'];
             $user->email = $userParams['email'];
             $user->reason = $userParams['reason'];
             $user->api_key = $user->generateKey();
             $user->is_active = 1;
             $user->save();
             //add admin notification email to queue
             $email = new ScheduledEmail();
             $email->from_name = sfConfig::get('app_mail_sender_name');
             $email->from_email = sfConfig::get('app_mail_sender_address');
             $email->to_name = sfConfig::get('app_mail_sender_name');
             $email->to_email = sfConfig::get('app_mail_sender_address');
             $email->subject = sprintf("%s (%s) has requested an API key", $user->getFullName(), $user->email);
             $email->body_text = $this->getPartial('keyrequestnotify', array('user' => $user));
             $email->save();
             $this->created = true;
             //send approval email
             $mailBody = $this->getPartial('keycreatenotify', array('user' => $user));
             $mailer = new Swift(new Swift_Connection_NativeMail());
             $message = new Swift_Message('Your LittleSis API key', $mailBody, 'text/plain');
             $from = new Swift_Address(sfConfig::get('app_mail_sender_address'), sfConfig::get('app_mail_sender_name'));
             $recipients = new Swift_RecipientList();
             $recipients->addTo($user->email, $user->name_first . ' ' . $user->name_last);
             $recipients->addBcc(sfConfig::get('app_mail_sender_address'));
             $mailer->send($message, $recipients, $from);
             $mailer->disconnect();
         }
     }
 }
Exemple #2
0
 public function executeNotes($request)
 {
     $this->checkUser();
     $this->profile = $this->getUser()->getProfile();
     $this->note_form = new NoteForm();
     $this->unread_notes = $this->profile->unread_notes;
     $this->profile->unread_notes = 0;
     $this->profile->save();
     //get network options
     $networkIds = array_unique(array(sfGuardUserTable::getHomeNetworkId(), LsListTable::US_NETWORK_ID));
     $networkIds = array_unique(array_merge($networkIds, $request->getParameter('network_ids', array())));
     if (count($networkIds) > 1) {
         $this->networks = LsDoctrineQuery::create()->from('LsList l')->whereIn('l.id', $networkIds)->fetchArray();
     }
     if ($request->isMethod('post')) {
         $params = $request->getParameter('note');
         $this->note_form->bind($params);
         if ($this->note_form->isValid()) {
             $db = Doctrine_Manager::connection();
             try {
                 $db->beginTransaction();
                 //associate note with specified networks, or else the user's home network
                 $networkIds = $request->getParameter('network_ids', array(sfGuardUserTable::getHomeNetworkId()));
                 $note = new Note();
                 $note->user_id = $this->getUser()->getGuardUser()->id;
                 $note->encodeBody($params['body']);
                 $note->is_private = isset($params['is_private']) ? true : false;
                 $note->network_ids = NoteTable::serialize($networkIds);
                 $note->save();
                 //if there are alerted users, add notification emails to email queue
                 foreach (NoteTable::unserialize($note->alerted_user_names) as $public_name) {
                     if ($profile = Doctrine::getTable('sfGuardUserProfile')->findOneByPublicName($public_name)) {
                         $public_name = $this->getUser()->getGuardUser()->getProfile()->public_name;
                         $profile->unread_notes++;
                         $profile->save();
                         $email = new ScheduledEmail();
                         $email->from_name = sfConfig::get('app_mail_notes_sender_name');
                         $email->from_email = sfConfig::get('app_mail_notes_sender_address');
                         $email->to_name = $profile->getName();
                         $email->to_email = $profile->email;
                         $email->subject = $public_name . ' has written you a note on LittleSis';
                         $email->body_text = $this->getPartial('notenotify', array('note_author' => $public_name, 'note_author_id' => $note->user_id, 'note_body' => NoteTable::prepareBodyForEmail($note->body), 'note_is_private' => $note->is_private));
                         $email->body_html = nl2br($this->getPartial('notenotify', array('note_author' => $public_name, 'note_author_id' => $note->user_id, 'note_body' => NoteTable::prepareBodyForEmail($note->body, true), 'note_is_private' => $note->is_private)));
                         $email->save();
                     }
                 }
                 $db->commit();
             } catch (Exception $e) {
                 $db->rollback();
                 throw $e;
             }
             $this->redirect('home/notes');
         }
     }
     $page = $request->getParameter('page', 1);
     $num = $request->getParameter('num', 20);
     $withReplies = $request->getParameter('replies', 1);
     //get notes to/from the current user using sphinx
     $s = new LsSphinxClient($page, $num);
     $currentUserId = sfGuardUserTable::getCurrentUserId();
     if ($withReplies) {
         $s->setFilter('visible_to_user_ids', array($currentUserId));
     } else {
         $s->setFilter('user_id', array($currentUserId));
     }
     $this->note_pager = NoteTable::getSphinxPager($s, null, Doctrine::HYDRATE_ARRAY);
     //execute pager to get most recently indexed note
     $notes = $this->note_pager->execute();
     $lastNoteId = count($notes) ? $notes[0]['id'] : 1;
     //get new notes that may not yet be indexed
     $this->new_notes = LsDoctrineQuery::create()->from('Note n')->leftJoin('n.User u')->leftJoin('u.Profile p')->where('n.user_id = ?', $this->getUser()->getGuardUser()->id)->andWhere('n.id > ?', $lastNoteId)->orderBy('n.id DESC')->setHydrationMode(Doctrine::HYDRATE_ARRAY)->execute();
 }