/**
  * Allows pre-save logic to be applied.
  * Subclasses may override this method.
  *
  * @param object $result    The current result item
  * @return void
  */
 protected function _preSave($result)
 {
     // Set these here rather than in the table class so that we can use the startTime
     if ($this->_rowAction === 'INSERT') {
         $this->_data['createdDate'] = (string) $this->_startTime->format('c');
     }
     $this->_data['lastModifiedDate'] = (string) $this->_startTime->format('c');
 }
 /**
  * Allows post-save logic to be applied.
  * Subclasses may override this method.
  *
  * @param object $result    The current result item
  * @return void
  */
 protected function _postSave($result)
 {
     // Save any email addresses to a separate table
     if (isset($result->email[0])) {
         // Create an object for the `tevoOfficeEmails` table
         $emailsTable = new OfficeEmailsTable();
         // Initialize an array of emails
         $emailArray = array();
         // Loop through the emails and add them to the `tevoOfficeEmails` table
         foreach ($result->email as $email) {
             $data = array('officeId' => (int) $result->id, 'email' => strtolower((string) $email), 'officeEmailsStatus' => (int) 1, 'lastModifiedDate' => (string) $this->_startTime->format('c'));
             if ($row = $emailsTable->fetchRow($emailsTable->select()->where("`officeId` = ?", $data['officeId'])->where("`email` = ?", $data['email']))) {
                 $row->setFromArray($data);
             } else {
                 $row = $emailsTable->createRow($data);
             }
             $row->save();
             unset($row);
             $emailArray[] = $data['email'];
             unset($data);
         }
         // End loop through emails for this office
         if ($this->_showProgress) {
             echo '<p>Saved ' . implode(', ', $emailArray) . ' to `tevoOfficeEmails` for this office</p>' . PHP_EOL;
         }
         // Now set `officeEmailsStatus` = 0 for any `tevoOfficeEmails` entries for
         // this office that are not in $emailArray. This will set any emails that
         // were attached to this office but are no longer to false/inactive
         if (isset($emailArray)) {
             $data = array('officeEmailsStatus' => (int) 0, 'lastModifiedDate' => (string) $this->_startTime->format('c'));
             $where = $emailsTable->getAdapter()->quoteInto("`officeId` = ?", $result->id);
             $where .= $emailsTable->getAdapter()->quoteInto(" AND `officeEmailsStatus` = ?", (int) 1);
             $where .= $emailsTable->getAdapter()->quoteInto(" AND `email` NOT IN (?)", $emailArray);
             $emailsTable->update($data, $where);
             unset($data);
             unset($where);
             unset($emailArray);
         }
     }
     // Save any office hours to a separate table
     if (isset($result->hours[0])) {
         // Create an object for the `tevoOfficeHours` table
         $hoursTable = new OfficeHoursTable();
         // Initialize an array of emails
         $hoursIdArray = array();
         // Loop through the emails and add them to the `tevoOfficeEmails` table
         foreach ($result->hours as $hour) {
             $openTime = new DateTime($hour->operating_hour->open);
             $closeTime = new DateTime($hour->operating_hour->close);
             $data = array('officeHoursId' => (int) $hour->operating_hour->id, 'officeId' => (int) $result->id, 'dayOfWeek' => (int) $hour->operating_hour->day_of_week, 'isClosed' => (int) $hour->operating_hour->closed, 'open' => (string) $openTime->format(DateTime::MYSQL_TIME), 'close' => (string) $closeTime->format(DateTime::MYSQL_TIME), 'officeHoursStatus' => (int) 1, 'lastModifiedDate' => (string) $this->_startTime->format('c'));
             if ($row = $hoursTable->fetchRow($hoursTable->select()->where("`officeHoursId` = ?", $data['officeHoursId']))) {
                 $row->setFromArray($data);
             } else {
                 $row = $hoursTable->createRow($data);
             }
             $row->save();
             unset($row);
             $hoursIdArray[] = $data['officeHoursId'];
             unset($data);
         }
         // End loop through hours for this office
         if ($this->_showProgress) {
             echo '<p>Saved ' . implode(', ', $hoursIdArray) . ' to `tevoOfficeHours` for this office</p>' . PHP_EOL;
         }
         // Now set `officeHoursStatus` = 0 for any `tevoOfficeHours` entries for
         // this office that are not in $hoursIdArray. This will set any hours that
         // were attached to this office but are no longer to false/inactive
         if (isset($hoursIdArray)) {
             $data = array('officeHoursStatus' => (int) 0, 'lastModifiedDate' => (string) $this->_startTime->format('c'));
             $where = $hoursTable->getAdapter()->quoteInto("`officeId` = ?", $result->id);
             $where .= $hoursTable->getAdapter()->quoteInto(" AND `officeHoursStatus` = ?", (int) 1);
             $where .= $hoursTable->getAdapter()->quoteInto(" AND `officeHoursId` NOT IN (?)", $hoursIdArray);
             $hoursTable->update($data, $where);
             unset($data);
             unset($where);
             unset($hoursIdArray);
         }
     }
 }