/** * Display fieldtype * * @param string $name Our fieldtype handle * @return string Return our fields input template */ public function getInputHtml($name, $value) { $id = craft()->templates->formatInputId($name); $namespaceId = craft()->templates->namespaceInputId($id); craft()->templates->includeJsResource('recurringdate/js/advanceddate.js'); if (!empty($value)) { $ruleModel = RecurringDate_RuleModel::populateModel($value); } else { $ruleModel = new RecurringDate_RuleModel(); $ruleModel->handle = $name; } $attr = $ruleModel->getAttributes(); if (strpos($attr['rrule'], 'EXDATE') !== false) { $attr['exdates'] = array(); $exDatesArray = explode('EXDATE=', $attr['rrule']); $exDatesString = $exDatesArray[1]; $exDatesString = rtrim($exDatesString, ";"); $exDatesArray = explode(',', $exDatesString); foreach ($exDatesArray as $index => $date) { $attr['exdates'][] = DateTime::createFromFormat('Ymd', $date); } } else { $attr['exdates'] = array(); } $attr['namespaceId'] = $namespaceId; return craft()->templates->render('recurringdate/fields', $attr); }
/** * @return array */ public function provideValidFieldsForElement() { $now = new DateTime(); $tomorrow = new DateTime('+1 days'); $defaultExpectedAttributes = array('id' => null, 'enabled' => true, 'archived' => false, 'locale' => 'en_gb', 'localeEnabled' => true, 'slug' => null, 'uri' => null, 'dateCreated' => null, 'dateUpdated' => null, 'root' => null, 'lft' => null, 'rgt' => null, 'level' => null, 'searchScore' => null, 'sectionId' => null, 'typeId' => null, 'authorId' => 1, 'postDate' => null, 'expiryDate' => null, 'parentId' => null, 'revisionNotes' => null); return array('Basic attributes' => array('fields' => array('title' => 'test', 'id' => 1, 'locale' => 'nl_nl', 'authorId' => 2, 'slug' => 'Test slug', 'postDate' => $now->getTimestamp(), 'expiryDate' => $tomorrow->getTimestamp(), 'enabled' => '0'), 'expectedAttributes' => array_merge($defaultExpectedAttributes, array('id' => 1, 'enabled' => false, 'locale' => 'nl_nl', 'slug' => 'test-slug', 'authorId' => 2, 'postDate' => $now, 'expiryDate' => $tomorrow))), 'No author given' => array('fields' => array(), 'expectedAttributes' => $defaultExpectedAttributes), 'String author given' => array('fields' => array('authorId' => 'String author'), 'expectedAttributes' => $defaultExpectedAttributes), 'Parent given' => array('fields' => array('parentId' => 'news'), 'expectedAttributes' => array_merge($defaultExpectedAttributes, array('parentId' => 1))), 'Ancestors given' => array('fields' => array('ancestors' => 'news and stuff'), 'expectedAttributes' => array_merge($defaultExpectedAttributes, array('parentId' => 2)))); }
/** * Creates a DateInterval object from seconds. * * @param int $seconds * * @return \Craft\DateInterval */ protected function getDateIntervalFromSeconds($seconds) { $d1 = new DateTime(); $d2 = new DateTime(); $d2->add(new DateInterval('PT' . $seconds . 'S')); return $d2->diff($d1); }
/** * Which Carts need to be deleted * * @return Market_OrderModel[] */ public function getCartsToPurge() { $configInterval = craft()->market_settings->getSettings()->purgeIncompleteCartDuration; $edge = new DateTime(); $interval = new DateInterval($configInterval); $interval->invert = 1; $edge->add($interval); $records = Market_OrderRecord::model()->findAllByAttributes(['dateOrdered' => null], 'dateUpdated <= :edge', ['edge' => $edge->format('Y-m-d H:i:s')]); return Market_OrderModel::populateModels($records); }
/** * Loops through the data and converts the times to DateTime objects. * * @access private * @param array &$value */ public function convertTimes(&$value, $timezone = null) { if (isset($value) && is_array($value)) { foreach ($value as &$day) { if (is_string($day['open']) && $day['open'] || is_array($day['open']) && $day['open']['time']) { $day['open'] = DateTime::createFromString($day['open'], $timezone); } else { $day['open'] = ''; } if (is_string($day['close']) && $day['close'] || is_array($day['close']) && $day['close']['time']) { $day['close'] = DateTime::createFromString($day['close'], $timezone); } else { $day['close'] = ''; } } } }
private function addCreatorHelpers(&$helper, $creator) { /* -- Computed identity strings */ $now = new DateTime(); $period = ". "; $name = $creator['genericCreatorName']; if ($name) { if ($name[strlen($name) - 1] == '.') { $period = " "; } } $helper['creatorCopyrightNotice'] = Craft::t("Copyright") . " ©" . $now->year() . " " . $name . $period . Craft::t("All rights reserved."); $helper['creatorAddressString'] = ''; $helper['creatorAddressHtml'] = ''; $helper['creatorMapUrl'] = ''; if ($creator['genericCreatorStreetAddress'] && $creator['genericCreatorAddressLocality'] && $creator['genericCreatorAddressRegion'] && $creator['genericCreatorPostalCode']) { $helper['creatorAddressString'] = $creator['genericCreatorName'] . ", " . $creator['genericCreatorStreetAddress'] . ", " . $creator['genericCreatorAddressLocality'] . ", " . $creator['genericCreatorAddressRegion'] . " " . $creator['genericCreatorPostalCode'] . ", " . $creator['genericCreatorAddressCountry']; $helper['creatorAddressHtml'] = $creator['genericCreatorName'] . "<br />" . $creator['genericCreatorStreetAddress'] . "<br />" . $creator['genericCreatorAddressLocality'] . ", " . $creator['genericCreatorAddressRegion'] . " " . $creator['genericCreatorPostalCode'] . "<br />" . $creator['genericCreatorAddressCountry'] . "<br />"; $params = array(); $params = count($params) ? '&' . http_build_query($params) : ''; $query = urlencode($helper['creatorAddressString']); $helper['creatorMapUrl'] = "http://maps.google.com/maps?q={$query}{$params}"; } }
/** * @return Market_ProductModel * @throws Exception */ private function _setProductFromPost() { $productId = craft()->request->getPost('productId'); if ($productId) { $product = craft()->market_product->getById($productId); if (!$product) { throw new Exception(Craft::t('No product with the ID “{id}”', ['id' => $productId])); } } else { $product = new Market_ProductModel(); } $availableOn = craft()->request->getPost('availableOn'); $expiresOn = craft()->request->getPost('expiresOn'); $product->availableOn = $availableOn ? DateTime::createFromString($availableOn, craft()->timezone) : $product->availableOn; $product->expiresOn = $expiresOn ? DateTime::createFromString($expiresOn, craft()->timezone) : null; $product->typeId = craft()->request->getPost('typeId'); $product->enabled = craft()->request->getPost('enabled'); $product->promotable = craft()->request->getPost('promotable'); $product->freeShipping = craft()->request->getPost('freeShipping'); $product->authorId = craft()->userSession->id; $product->taxCategoryId = craft()->request->getPost('taxCategoryId', $product->taxCategoryId); if (!$product->availableOn) { $product->availableOn = new DateTime(); } $product->getContent()->title = craft()->request->getPost('title', $product->title); $product->slug = craft()->request->getPost('slug', $product->slug); $product->setContentFromPost('fields'); return $product; }
function strtodate($str, $format) { return DateTime::createFromFormat($format, $str); }
private function buildRRule($settings) { $allday = $settings['allday']; $startDate = $settings['start_date']; $endDate = $settings['end_date']; $startTime = $settings['start_time']; $endTime = $settings['end_time']; $repeats = $settings['repeats']; //Does it repeat? $frequency = $settings['frequency']; //Weekly, Daily, Monthly, Yearly $interval = $settings['interval']; // i.e. Every 1-30 Months? $weekDays = $settings['weekdays']; //Which weekdays $repeatBy = $settings['repeat_by']; //Monthly, by day of week, or day of month $ends = $settings['ends']; //how it ends (never, after, until) $count = $settings['count']; // if ending occurs amounts $untilDate = $settings['until']; // if ending until date $exDates = $settings['exdates']; $dbString = ''; //Builds RRULE based on UI Elements Input if ($repeats) { $rule = new Recurr\RecurrenceRule(); $rule->setStartDate(DateTime::createFromString($startDate, craft()->getTimeZone())); $rule->setInterval($interval); if ($ends == 'until') { $rule->setEndDate(DateTime::createFromString($untilDate, craft()->getTimeZone())); } else { if ($ends == 'after') { $rule->setCount($count); } } switch ($frequency) { case 'daily': $rule->setFreq(Recurr\RecurrenceRule::FREQ_DAILY); break; case 'weekly': $rule->setFreq(Recurr\RecurrenceRule::FREQ_WEEKLY); if (empty($weekDays)) { //If weekdays empty set monday by default $rule->setByDay(array('MO')); } else { $rule->setByDay($weekDays); } break; case 'monthly': $rule->setFreq(Recurr\RecurrenceRule::FREQ_MONTHLY); if ($repeatBy == 'month') { $dayOfMonth = date('j', $startDate); $rule->setByMonthDay(array($dayOfMonth)); } else { if ($repeatBy == 'week') { $uStartDate = $startDate; $dayOfWeek = strtoupper(substr(date('D', $uStartDate), 0, -1)); $numberOfWeek = ceil(date('j', $uStartDate) / 7); $rule->setByDay(array('+' . $numberOfWeek . $dayOfWeek)); } } break; case 'yearly': $rule->setFreq(Recurr\RecurrenceRule::FREQ_YEARLY); break; } if ($startTime) { $time = date('Ymd\\THis', strtotime(date('Y-m-d', $startDate) . date(' H:i:s', strtotime($startTime)))); $dbString .= 'DTSTART=' . $time . ';' . $rule->getString(); } else { $time = $startDate; var_dump($startDate); $dbString .= 'DTSTART=' . date('Ymd', $time) . ';' . $rule->getString(); } if (count($exDates) > 0) { $dbString .= ';EXDATE='; foreach ($exDates as $index => $date) { $dbString .= date('Ymd', $date); if ($date !== end($exDates)) { $dbString .= ','; } } $dbString .= ';'; } } else { if ($startTime) { $time = date('Ymd\\THis', strtotime(date('Ymd', $startDate) . date('\\THis', strtotime($startTime)))); $dbString .= 'DTSTART=' . $time . ';'; } else { $dbString .= 'DTSTART=' . date('Ymd', $startDate) . ';'; } } return $dbString; }