/**
  * Saving event data to eventRecord
  *
  * @return  bool
  */
 public function saveEventData($id, $attributes)
 {
     $model = new Venti_EventModel();
     $model->eventid = $id;
     $model->setAttributes($attributes);
     $timezone = craft()->getTimeZone();
     #
     # Validate's the model
     if ($model->validate()) {
         #
         # Convert startDate to UTC time for database
         $startDateString = $model->startDate->format(DateTime::MYSQL_DATETIME, DateTime::UTC);
         $model->startDate = DateTime::createFromFormat(DateTime::MYSQL_DATETIME, $startDateString, DateTime::UTC);
         #
         # Convert endDate to UTC time for database
         $startEndString = $model->endDate->format(DateTime::MYSQL_DATETIME, DateTime::UTC);
         $model->endDate = DateTime::createFromFormat(DateTime::MYSQL_DATETIME, $startEndString, DateTime::UTC);
         #
         # If there is no current event record at specific id create new record
         if (null === ($record = $this->eventRecord->findByAttributes(array("eventid" => $id)))) {
             $record = $this->eventRecord->create();
         }
         #
         # Map model attributes to event record attributes
         $record->setAttributes($model->getAttributes(), false);
         if ($record->save()) {
             $model->setAttribute('eventid', $record->getAttribute('eventid'));
             #
             # If repeat checkbox is selected (exists in attributes array) save recurring dates
             # if not selected see if there are dates in dateRecord associated with event id
             # if there are delete them.
             if (array_key_exists('repeat', $attributes)) {
                 if ($attributes['repeat'] == 1) {
                     //$dates = $this->getRecurDates($model->getAttribute('startDate'),$model->getAttribute('rRule'));
                     $this->saveRecurringDates($model);
                 }
             } else {
                 if ($dates = $this->eventRecord->findByAttributes(array("eventid" => $id, "isrepeat" => 1))) {
                     $this->eventRecord->deleteAllByAttributes(array("eventid" => $id, "isrepeat" => 1));
                 }
             }
             return true;
         } else {
             $model->addErrors($record->getErrors());
             craft()->userSession->setError(Craft::t('Event not saved.'));
             return false;
         }
     } else {
         craft()->userSession->setError(Craft::t("Event dates can't be empty."));
         return false;
     }
 }
 /**
  * Saving event data to eventRecord
  *
  * @return  bool
  */
 public function saveEventData(BaseElementModel $element, $attributes)
 {
     $model = new Venti_EventModel();
     $model->eventid = $element->id;
     $model->locale = $element->locale;
     $model->setAttributes($attributes);
     $model->startDate = DateTime::createFromString($attributes['startDate']['date'], craft()->getTimeZone());
     $model->endDate = DateTime::createFromString($attributes['endDate']['date'], craft()->getTimeZone());
     $timezone = craft()->getTimeZone();
     #
     # Validate's the model
     if ($model->validate()) {
         #
         # If there is no current event record at specific id create new record
         if (null === ($record = $this->eventRecord->findByAttributes(array("eventid" => $element->id, "locale" => $element->locale)))) {
             $record = $this->eventRecord->create();
         }
         #
         # Map model attributes to event record attributes
         $record->setAttributes($model->getAttributes(), false);
         if ($record->save()) {
             $model->setAttribute('eventid', $record->getAttribute('eventid'));
             #
             # If repeat checkbox is selected (exists in attributes array) save recurring dates
             # if not selected see if there are dates in dateRecord associated with event id
             # if there are delete them.
             if (array_key_exists('repeat', $attributes)) {
                 if ($attributes['repeat'] == 1) {
                     $this->saveRecurringDates($model);
                 }
             } else {
                 if ($dates = $this->eventRecord->findByAttributes(array("eventid" => $element->id, "isrepeat" => 1, "locale" => $element->locale))) {
                     $this->eventRecord->deleteAllByAttributes(array("eventid" => $element->id, "isrepeat" => 1));
                 }
             }
             #
             # Delete templates caches with this eventid/element->id
             craft()->templateCache->deleteCachesByElementId($element->id);
             return true;
         } else {
             $model->addErrors($record->getErrors());
             craft()->userSession->setError(Craft::t('Event not saved.'));
             return false;
         }
     } else {
         craft()->userSession->setError(Craft::t("Event dates can't be empty."));
         return false;
     }
 }