/**
  * 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 corresponding set
         // method.  This object relates to these object(s) by a
         // foreign key reference.
         if ($this->aLeasingAppointmentLeads !== null) {
             if ($this->aLeasingAppointmentLeads->isModified() || $this->aLeasingAppointmentLeads->isNew()) {
                 $affectedRows += $this->aLeasingAppointmentLeads->save($con);
             }
             $this->setLeasingAppointmentLeads($this->aLeasingAppointmentLeads);
         }
         if ($this->aLeasingUnit !== null) {
             if ($this->aLeasingUnit->isModified() || $this->aLeasingUnit->isNew()) {
                 $affectedRows += $this->aLeasingUnit->save($con);
             }
             $this->setLeasingUnit($this->aLeasingUnit);
         }
         if ($this->isNew() || $this->isModified()) {
             // persist changes
             if ($this->isNew()) {
                 $this->doInsert($con);
             } else {
                 $this->doUpdate($con);
             }
             $affectedRows += 1;
             $this->resetModified();
         }
         if ($this->leasingAppointmentAssignmentsScheduledForDeletion !== null) {
             if (!$this->leasingAppointmentAssignmentsScheduledForDeletion->isEmpty()) {
                 foreach ($this->leasingAppointmentAssignmentsScheduledForDeletion as $leasingAppointmentAssignment) {
                     // need to save related object because we set the relation to null
                     $leasingAppointmentAssignment->save($con);
                 }
                 $this->leasingAppointmentAssignmentsScheduledForDeletion = null;
             }
         }
         if ($this->collLeasingAppointmentAssignments !== null) {
             foreach ($this->collLeasingAppointmentAssignments as $referrerFK) {
                 if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) {
                     $affectedRows += $referrerFK->save($con);
                 }
             }
         }
         $this->alreadyInSave = false;
     }
     return $affectedRows;
 }
 public function saveAppointmentRequestAction()
 {
     $request = $this->getRequest();
     $postid = $request->request->get('unitid');
     $date = $request->request->get('date');
     $time = $request->request->get('time');
     $leasePeriod = $request->request->get('leasePeriod');
     $targetMoveIn = $request->request->get('targetMoveIn');
     $fname = $request->request->get('fname');
     $lname = $request->request->get('lname');
     $contact = $request->request->get('contact');
     $email = $request->request->get('email');
     $country = LeasingCountryPeer::getIdByCountryName($request->request->get('country'));
     $nationality = LeasingNationalityPeer::getIdByNationalityName($request->request->get('nationality'));
     $notes = $request->request->get('notes');
     $clientIp = $request->request->get('clientIp');
     $firstHeard = $request->request->get('firstHeard');
     //UNIT DETAILS
     $unit = LeasingUnitPeer::getUnitByPostId($postid);
     $lead = new LeasingAppointmentLeads();
     $lead->setFname($fname);
     $lead->setLname($lname);
     $lead->setEmail($email);
     $lead->setMobile($contact);
     $lead->setCountryId($country->getId());
     $lead->setNationalityId($nationality->getId());
     $lead->setClientIp($clientIp);
     $lead->save();
     $now = new \DateTime('now');
     $app = new LeasingAppointments();
     $app->setAppointmentLeadsId($lead->getId());
     $app->setUnitId($unit->getId());
     $app->setPreferredDate($date);
     $app->setPreferredTime($time);
     $app->setLeasePeriod($leasePeriod);
     $app->setTargetMoveIn($targetMoveIn);
     $app->setFirstHeard($firstHeard);
     $app->setNotes($notes);
     $app->setDateAdded($now->format(C::DATETIMEFORMAT));
     $app->setStatus(C::PENDING);
     $app->setPrevStatus(C::PENDING);
     $app->save();
     $tl1 = new LeasingTimelineActivity();
     $tl1->setLeadTypeId(C::APPOINTMENT);
     $tl1->setLeadId($lead->getId());
     $tl1->setUser('Lead');
     $tl1->setActivity('Requested Unit Viewing');
     $tl1->setTimestamp($now->format(C::DATETIMEFORMAT));
     $tl1->setStatus('Pending');
     $tl1->setStatusId(C::PENDING);
     $tl1->save();
     $this->get('session')->set('thank-you', 1);
     $client = new GlobeClient();
     $msg = "Thank you, " . $lead->getFname() . " " . $lead->getLname() . "! Your request to view " . $app->getLeasingUnit()->getName() . " is now being processed. We will update you ASAP once an agent is assigned to assist you. This msg is FREE.";
     $sms = $client->sms($this->globeShortCode);
     $response = $sms->sendMessage($lead->getMobile(), $msg, $this->appId, $this->appSecret);
     if ($response && !isset($response['error'])) {
         $badge = new LeasingLeadBadges();
         $badge->setBadgeId(11);
         $badge->setLeadTypeId(C::APPOINTMENT);
         $badge->setLeadId($lead->getId());
         $badge->setStatus(1);
         $badge->save();
         $tl2 = new LeasingTimelineActivity();
         $tl2->setLeadTypeId(C::APPOINTMENT);
         $tl2->setLeadId($lead->getId());
         $tl2->setUser('System');
         $tl2->setActivity('Verified mobile number');
         $tl2->setTimestamp($now->format(C::DATETIMEFORMAT));
         $tl2->setStatus('Mobile Verified');
         $tl2->setStatusId(C::MOBILE_VERIFIED);
         $tl2->save();
     }
     $ve = VerifyEmail::verifyThisEmail($lead->getEmail());
     if ($ve = 'valid') {
         $badge = new LeasingLeadBadges();
         $badge->setBadgeId(12);
         $badge->setLeadTypeId(C::PARKING);
         $badge->setLeadId($lead->getId());
         $badge->setStatus(1);
         $badge->save();
         $tl3 = new LeasingTimelineActivity();
         $tl3->setLeadTypeId(C::PARKING);
         $tl3->setLeadId($lead->getId());
         $tl3->setUser('System');
         $tl3->setActivity('Verified email address');
         $tl3->setTimestamp($now->format(C::DATETIMEFORMAT));
         $tl3->setStatus('Email Verified');
         $tl3->setStatusId(C::EMAIL_VERIFIED);
         $tl3->save();
     }
     echo 1;
     exit;
     return new Response();
 }