/**
  * Saves a calendar
  */
 public function actionSaveCalendar()
 {
     $this->requirePostRequest();
     $calendar = new Events_CalendarModel();
     // Shared attributes
     $calendar->id = craft()->request->getPost('calendarId');
     $calendar->name = craft()->request->getPost('name');
     $calendar->handle = craft()->request->getPost('handle');
     // Set the field layout
     $fieldLayout = craft()->fields->assembleLayoutFromPost();
     $fieldLayout->type = 'Events_Event';
     $calendar->setFieldLayout($fieldLayout);
     // Save it
     if (craft()->events_calendars->saveCalendar($calendar)) {
         craft()->userSession->setNotice(Craft::t('Calendar saved.'));
         $this->redirectToPostedUrl($calendar);
     } else {
         craft()->userSession->setError(Craft::t('Couldn’t save calendar.'));
     }
     // Send the calendar back to the template
     craft()->urlManager->setRouteVariables(array('calendar' => $calendar));
 }
 /**
  * Saves a calendar.
  *
  * @param Events_CalendarModel $calendar
  * @throws \Exception
  * @return bool
  */
 public function saveCalendar(Events_CalendarModel $calendar)
 {
     if ($calendar->id) {
         $calendarRecord = Events_CalendarRecord::model()->findById($calendar->id);
         if (!$calendarRecord) {
             throw new Exception(Craft::t('No calendar exists with the ID “{id}”', array('id' => $calendar->id)));
         }
         $oldCalendar = Events_CalendarModel::populateModel($calendarRecord);
         $isNewCalendar = false;
     } else {
         $calendarRecord = new Events_CalendarRecord();
         $isNewCalendar = true;
     }
     $calendarRecord->name = $calendar->name;
     $calendarRecord->handle = $calendar->handle;
     $calendarRecord->validate();
     $calendar->addErrors($calendarRecord->getErrors());
     if (!$calendar->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if (!$isNewCalendar && $oldCalendar->fieldLayoutId) {
                 // Drop the old field layout
                 craft()->fields->deleteLayoutById($oldCalendar->fieldLayoutId);
             }
             // Save the new one
             $fieldLayout = $calendar->getFieldLayout();
             craft()->fields->saveLayout($fieldLayout);
             // Update the calendar record/model with the new layout ID
             $calendar->fieldLayoutId = $fieldLayout->id;
             $calendarRecord->fieldLayoutId = $fieldLayout->id;
             // Save it!
             $calendarRecord->save(false);
             // Now that we have a calendar ID, save it on the model
             if (!$calendar->id) {
                 $calendar->id = $calendarRecord->id;
             }
             // Might as well update our cache of the calendar while we have it.
             $this->_calendarsById[$calendar->id] = $calendar;
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }