/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aPerson !== null) { if ($this->aPerson->isModified() || $this->aPerson->isNew()) { $affectedRows += $this->aPerson->save($con); } $this->setPerson($this->aPerson); } if ($this->aEmailLetter !== null) { if ($this->aEmailLetter->isModified() || $this->aEmailLetter->isNew()) { $affectedRows += $this->aEmailLetter->save($con); } $this->setEmailLetter($this->aEmailLetter); } if ($this->isNew()) { $this->modifiedColumns[] = EmailQueuePeer::ID; } // If this object has been modified, then save it to the database. if ($this->isModified()) { if ($this->isNew()) { $pk = EmailQueuePeer::doInsert($this, $con); $affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which // should always be true here (even though technically // BasePeer::doInsert() can insert multiple rows). $this->setId($pk); //[IMV] update autoincrement primary key $this->setNew(false); } else { $affectedRows += EmailQueuePeer::doUpdate($this, $con); } $this->resetModified(); // [HL] After being saved an object is no longer 'modified' } $this->alreadyInSave = false; } return $affectedRows; }
public function executeEmailBulkQueue(sfWebRequest $request) { $subject = $request->getParameter('subject'); $to = $request->getParameter('recipients'); $sender_email = $request->getParameter('sender_email'); $sender_name = $request->getParameter('sender_name'); $message = $request->getParameter('message'); $send_date = $request->getParameter('send_date'); $priority = $request->getParameter('priority'); $errors = array(); // validate recievers if (empty($to)) { $errors['email'][0] = 'please specify email address'; } else { $recievers = explode(',', $to); $pat = '/^([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})$/i'; $full_pat = '/^([- a-z0-9]+)\\<(([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,}))\\>$/i'; $emails = array(); foreach ($recievers as $reciever) { $reciever = trim($reciever); if (empty($reciever)) { continue; } if (preg_match($full_pat, $reciever, $matches)) { $emails[$matches[2]] = $matches[1]; } elseif (preg_match($pat, $reciever, $matches)) { $emails[$matches[0]] = ''; } else { if (!isset($errors['email'])) { $errors['email'] = array(); } $errors['email'][] = $reciever . ' is unrecognizable email'; } } } // validate subject if (empty($subject)) { $errors['subject'] = 'subject required'; } // validate send_date if (empty($send_date)) { $errors['send_date'] = 'Send date required'; } // validate sender $v_email = new sfValidatorEmail(array(), array('invalid' => 'please specify email address')); try { $sender_email = $v_email->clean($sender_email); } catch (sfValidatorError $e) { $errors['sender_email'] = $e->__toString(); } // attachments $upload_dir = sfConfig::get('sf_upload_dir') . '/' . 'bulk-email-attachments' . '/'; $files = array(); foreach ($request->getFiles() as $file) { if ($file[0]['error'] != 0) { continue; } if (move_uploaded_file($file[0]["tmp_name"], $upload_dir . $file[0]["name"])) { $files[] = array('path' => $upload_dir, 'name' => $file[0]['name']); } else { //echo '<h1>Error while uploading '.$file[0]["tmp_name"].'/'.$file[0]["name"].' to '.$upload_dir . $file[0]["name"].'</h1>'; $error['attachment'] = 'Error while uploading ' . $file[0]["name"]; } } // catch errors if ($errors) { $this->email_templates = EmailTemplatePeer::getByPersonId($this->getUser()->getId()); $this->errors = $errors; $this->recipients = $to; $this->subject = $subject; $this->sender_email = $sender_email; $this->sender_name = $sender_name; $this->message = $message; $this->priority = $priority; $this->send_date = $send_date; if (!empty($error['attachment'])) { $this->attachement_error = $error['attachment']; } $this->setTemplate('sendBulkQueue'); return sfView::SUCCESS; } $email_letter = new EmailLetter(); $email_letter->setSubject($subject); $email_letter->setSenderEmail($sender_email); $email_letter->setSenderName($sender_name); $email_letter->setBody($message); $email_letter->setAttachFilePath(serialize($files)); $email_letter->setRecipients(implode(',', array_keys($emails))); $email_letter->save(); $email_queue = new EmailQueue(); $email_queue->setPersonId($this->getUser()->getId()); $email_queue->setLetterId($email_letter->getId()); $email_queue->setRequestDate(date('Y-m-d H:i:s', time())); $email_queue->setSendDate($send_date); $email_queue->setPriority($priority); $email_queue->setSendStatus('pending'); $email_queue->save(); /* $this->getComponent('mail', 'sendBulkQueue', array( 'subject' => $subject, 'recievers' => $emails, 'sender' => array($sender_email => $sender_name), 'body' => $message, 'files' => $files, )); */ $this->getUser()->setFlash('success', 'Bulk email have successfully queued!'); $this->redirect($request->getReferer()); }