/**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     if (!craft()->db->columnExists('venti_events', 'locale')) {
         $this->addColumnAfter('venti_events', 'locale', array(ColumnType::Char, 'default' => craft()->language), 'summary');
         VentiPlugin::log("Locale column in Venti_Events table created", LogLevel::Info, true);
     } else {
         VentiPlugin::log("Locale column already exists in Venti_Events table", LogLevel::Info, true);
     }
     return true;
 }
 /**
  * Runs a task step.
  *
  * @param int $step
  * @return bool
  */
 public function runStep($step)
 {
     try {
         $element = $this->_elements[$step];
         $query = craft()->db->createCommand()->select('venti.eventid,venti.startDate,venti.endDate,venti.allDay,venti.repeat,venti.rRule,venti.summary,venti.isrepeat')->from('venti_events venti');
         $query->andWhere(array("venti.eventid" => $element->id));
         $query->andWhere("venti.isrepeat is NULL");
         $attributes = $query->queryAll();
         $attributes[0]['startDate'] = DateTime::createFromString($attributes[0]['startDate'], null, true);
         $attributes[0]['endDate'] = DateTime::createFromString($attributes[0]['endDate'], null, true);
         if (craft()->venti_eventManage->saveEventData($element, $attributes[0])) {
             return true;
         } else {
             VentiPlugin::log('Recurring event with ID ' . $element->id . " was not saved.", LogLevel::Error);
             return 'Recurring event with ID ' . $element->id . " was not saved.";
         }
     } catch (Exception $e) {
         VentiPlugin::log('An exception was thrown while trying to save the recurring event with the ID “' . $this->_elementIds[$step] . '”: ' . $e->getMessage(), LogLevel::Error);
         return 'An exception was thrown while trying to save the recurring event with the ID “' . $this->_elementIds[$step] . '”: ' . $e->getMessage();
     }
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     // Step 1 :: Add Venti field(s) to content table
     $rows = craft()->db->createCommand()->select('fields.id, fields.handle, fields.context, fields.type')->from('fields fields')->where(['fields.type' => 'Venti_Event'])->queryAll();
     for ($i = 0; $i < count($rows); $i++) {
         if ($rows[$i]['context'] == 'global') {
             $fieldHandle = 'field_' . $rows[$i]['handle'];
             if (!craft()->db->columnExists('content', $fieldHandle)) {
                 $this->addColumn('content', $fieldHandle, array(ColumnType::Text));
                 VentiPlugin::log($fieldHandle . " column in content table created", LogLevel::Info, true);
             } else {
                 VentiPlugin::log($fieldHandle . " column already exists in the Content table", LogLevel::Info, true);
             }
         }
     }
     // Step 2 :: Add field date to newly created Venti field(s)
     craft()->templateCache->deleteCachesByElementType('Entry');
     //Get unique list of element ids from venti events
     $ids = craft()->db->createCommand()->select('venti.eventid')->from('venti_events venti')->queryAll();
     $flatIds = [];
     foreach ($ids as $key => $value) {
         array_push($flatIds, $value['eventid']);
     }
     $entryIds = array_unique($flatIds);
     /*craft()->sections->getAllSections()
       
       {% for section in sections %}
           {% if section.getEntryTypes()[0].getFieldLayout().getFields()[0].getField().handle == 'specificfield' %}
               do something
           {% endif %}
       {% endfor %}*/
     foreach ($entryIds as $entryId) {
         $entry = craft()->entries->getEntryById($entryId);
         if (!$entry) {
             continue;
         }
         $event = craft()->db->createCommand()->select('venti.eid, venti.eventid, venti.startDate, venti.endDate, venti.summary, venti.rRule, venti.repeat, venti.allDay, venti.isrepeat ')->from('venti_events venti')->where('venti.eventid = :eventid', array(':eventid' => $entryId))->andWhere('venti.isrepeat IS NULL')->queryAll();
         //VentiPlugin::log("Entry ID: " . $entryId . " | Event ID: " . $event[0]['eventid']." | Event Count: " . count($event), LogLevel::Info);
         $fieldData = array('startDate' => $event[0]['startDate'], 'endDate' => $event[0]['endDate'], 'allDay' => $event[0]['allDay'], 'repeat' => $event[0]['repeat'], 'summary' => $event[0]['summary'], 'rRule' => $event[0]['rRule']);
         $fields = $entry->fieldLayout->fields;
         foreach ($fields as $field) {
             if ($field->getField()->type == 'Venti_Event') {
                 $fieldHandle = $field->getField()->handle;
                 //Update new field with data
                 VentiPlugin::log($fieldHandle, LogLevel::Info);
                 $entry->setContentFromPost(array($fieldHandle => $fieldData));
             }
         }
         // Save it
         $success = craft()->entries->saveEntry($entry);
         if (!$success) {
             // Log the error in this plugin's log file (in craft/storage/runtime/logs/fooplugin.log)
             $error = 'Encountered the following validation errors when trying to save entry # "' . $entry->id . '":';
             foreach ($entry->getAllErrors() as $attributeError) {
                 $error .= "\n - {$attributeError}";
             }
             VentiPlugin::log($error, LogLevel::Error);
         }
     }
     return true;
 }
 public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
 {
     // If custom field, get field handle
     if (strncmp($attribute, 'field:', 6) === 0) {
         $fieldId = substr($attribute, 6);
         $field = craft()->fields->getFieldById($fieldId);
         $attribute = $field->handle;
     }
     $attributes = $entry->getAttributes();
     if ($fieldData = craft()->venti_eventManage->getEventFieldData($attributes['id'], $attributes['locale'])) {
         switch ($attribute) {
             case 'ventiStartDate':
                 return $fieldData['startDate']->format('M d, Y');
                 break;
             case 'ventiEndDate':
                 return $fieldData['endDate']->format('M d, Y');
                 break;
             case 'ventiRepeat':
                 return $fieldData['repeat'];
                 break;
             case 'ventiAllDay':
                 return $fieldData['allDay'];
                 break;
             case 'ventiRepeatSummary':
                 return $fieldData['summary'];
                 break;
         }
     } else {
         // throw new Exception(Craft::t('Event with id “{id}” in locale “{locale}”. Try resaving events.', array('id' => $attributes['id'], 'locale' => $attributes['locale'])));
         VentiPlugin::log("Entry table attributes can't be found for Venti attributes.", LogLevel::Error, true);
     }
 }
 /**
  * Get field type data based on eventid
  *
  * @return  array
  */
 public function getEventFieldData($id, $localeId = null)
 {
     $event = array();
     $eventRecord = Venti_EventRecord::model();
     if ($record = $this->eventRecord->findByAttributes(array("eventid" => $id, "locale" => $localeId))) {
         $event = array('startDate' => $record->startDate, 'endDate' => $record->endDate, 'allDay' => (bool) $record->allDay, 'repeat' => (bool) $record->repeat, 'summary' => $record->summary, 'rRule' => $record->rRule, 'locale' => $record->locale);
         return $event;
     } else {
         //craft()->userSession->setError(Craft::t("Event with id (". $id .") can't be found for event field data."));
         VentiPlugin::log("Venti_EventManageService::getEventFieldData – can't find event by id " . $id . ".", LogLevel::Error, true);
     }
 }