/** * Returns whether or not the entry meets the criteria necessary to trigger the event * * @param mixed $options * @param EntryModel $entry * @param array $params * * @return bool */ public function validateOptions($options, EntryModel $entry, array $params = array()) { $isNewEntry = isset($params['isNewEntry']) && $params['isNewEntry']; $whenNew = isset($options['craft']['saveEntry']['whenNew']) && $options['craft']['saveEntry']['whenNew']; $whenUpdated = isset($options['craft']['saveEntry']['whenUpdated']) && $options['craft']['saveEntry']['whenUpdated']; SproutEmailPlugin::log(Craft::t("Sprout Email '" . $this->getTitle() . "' event has been triggered")); // If any section ids were checked, make sure the entry belongs in one of them if (!empty($options['craft']['saveEntry']['sectionIds']) && count($options['craft']['saveEntry']['sectionIds'])) { if (!in_array($entry->getSection()->id, $options['craft']['saveEntry']['sectionIds'])) { SproutEmailPlugin::log(Craft::t('Saved entry not in any selected Section.')); return false; } } if (!$whenNew && !$whenUpdated) { SproutEmailPlugin::log(Craft::t("No settings have been selected. Please select 'When an entry is created' or 'When\n\t\t\tan entry is updated' from the options on the Rules tab.")); return false; } // Make sure new entries are new if ($whenNew && !$isNewEntry && !$whenUpdated) { SproutEmailPlugin::log(Craft::t("No match. 'When an entry is created' is selected but the entry is being updated\n\t\t\t.")); return false; } // Make sure updated entries are not new if ($whenUpdated && $isNewEntry && !$whenNew) { SproutEmailPlugin::log(Craft::t("No match. 'When an entry is updated' is selected but the entry is new.")); return false; } return true; }
/** * Enforces all Edit Entry permissions. * * @param EntryModel $entry * * @return null */ protected function enforceEditEntryPermissions(EntryModel $entry) { $userSessionService = craft()->userSession; $permissionSuffix = ':' . $entry->sectionId; if (craft()->isLocalized()) { // Make sure they have access to this locale $userSessionService->requirePermission('editLocale:' . $entry->locale); } // Make sure the user is allowed to edit entries in this section $userSessionService->requirePermission('editEntries' . $permissionSuffix); // Is it a new entry? if (!$entry->id) { // Make sure they have permission to create new entries in this section $userSessionService->requirePermission('createEntries' . $permissionSuffix); } else { switch ($entry->getClassHandle()) { case 'Entry': // If it's another user's entry (and it's not a Single), make sure they have permission to edit those if ($entry->authorId != $userSessionService->getUser()->id && $entry->getSection()->type != SectionType::Single) { $userSessionService->requirePermission('editPeerEntries' . $permissionSuffix); } break; case 'EntryDraft': // If it's another user's draft, make sure they have permission to edit those if ($entry->getClassHandle() == 'EntryDraft' && $entry->creatorId != $userSessionService->getUser()->id) { $userSessionService->requirePermission('editPeerEntryDrafts' . $permissionSuffix); } break; } } }
/** * Returns whether or not the entry meets the criteria necessary to trigger the event * * @param mixed $options * @param EntryModel $entry * @param array $params * * @return bool */ public function validateOptions($options, EntryModel $entry, array $params = array()) { $isNewEntry = isset($params['isNewEntry']) && $params['isNewEntry']; $onlyWhenNew = isset($options['entriesSaveEntryOnlyWhenNew']) && $options['entriesSaveEntryOnlyWhenNew']; // If any section ids were checked // Make sure the entry belongs in one of them if (!empty($options['entriesSaveEntrySectionIds']) && count($options['entriesSaveEntrySectionIds'])) { if (!in_array($entry->getSection()->id, $options['entriesSaveEntrySectionIds'])) { return false; } } // If only new entries was checked // Make sure the entry is new if (!$onlyWhenNew || $onlyWhenNew && $isNewEntry) { return true; } return false; }
/** * Checks if an entry was submitted with a new parent entry selected. * * @param EntryModel $entry * * @return bool */ private function _checkForNewParent(EntryModel $entry) { // Make sure this is a Structure section if ($entry->getSection()->type != SectionType::Structure) { return false; } // Is it a brand new entry? if (!$entry->id) { return true; } // Was a parentId actually submitted? if ($entry->parentId === null) { return false; } // Is it set to the top level now, but it hadn't been before? if ($entry->parentId === '' && $entry->level != 1) { return true; } // Is it set to be under a parent now, but didn't have one before? if ($entry->parentId !== '' && $entry->level == 1) { return true; } // Is the parentId set to a different entry ID than its previous parent? $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->ancestorOf = $entry; $criteria->ancestorDist = 1; $criteria->status = null; $criteria->localeEnabled = null; $oldParent = $criteria->first(); $oldParentId = $oldParent ? $oldParent->id : ''; if ($entry->parentId != $oldParentId) { return true; } // Must be set to the same one then return false; }
/** * Displays an entry. * * @param EntryModel $entry * * @throws HttpException * @return null */ private function _showEntry(EntryModel $entry) { $section = $entry->getSection(); $type = $entry->getType(); if ($section && $type) { craft()->setLanguage($entry->locale); if (!$entry->postDate) { $entry->postDate = new DateTime(); } craft()->templates->getTwig()->disableStrictVariables(); $this->renderTemplate($section->template, array('entry' => $entry)); } else { Craft::log('Attempting to preview an entry that doesn’t have a section/type', LogLevel::Error); throw new HttpException(404); } }
/** * Displays an entry. * * @param EntryModel $entry * * @throws HttpException * @return null */ private function _showEntry(EntryModel $entry) { $section = $entry->getSection(); $type = $entry->getType(); if (!$section || !$type) { Craft::log('Attempting to preview an entry that doesn’t have a section/type', LogLevel::Error); throw new HttpException(404); } craft()->setLanguage($entry->locale); if (!$entry->postDate) { $entry->postDate = new DateTime(); } // Have this entry override any freshly queried entries with the same ID/locale craft()->elements->setPlaceholderElement($entry); craft()->templates->getTwig()->disableStrictVariables(); $this->renderTemplate($section->template, array('entry' => $entry)); }
/** * Parse entry fields. * * @param EntryModel $entry * @param bool $empty * * @return array */ public function fields(EntryModel $entry, $empty = false) { // Always save id and title $fields = array('id' => array('label' => Craft::t('ID'), 'value' => $entry->id), 'title' => array('label' => Craft::t('Title'), 'value' => (string) $entry->getTitle()), 'section' => array('label' => Craft::t('Section'), 'value' => (string) $entry->getSection())); // Get element type $elementType = craft()->elements->getElementType(ElementType::Entry); // Get nice attributes $availableAttributes = $elementType->defineAvailableTableAttributes(); // Make 'em fit $attributes = array(); foreach ($availableAttributes as $key => $result) { $attributes[$key] = $result['label']; } // Get static "fields" foreach ($entry->getAttributes() as $handle => $value) { // Only show nice attributes if (array_key_exists($handle, $attributes)) { $fields[$handle] = array('label' => $attributes[$handle], 'value' => StringHelper::arrayToString(is_array($value) ? array_filter(ArrayHelper::flattenArray($value), 'strlen') : $value, ', ')); } } // Get fieldlayout $entrytype = $entry->getType(); if ($entrytype) { $tabs = craft()->fields->getLayoutById($entrytype->fieldLayoutId)->getTabs(); foreach ($tabs as $tab) { foreach ($tab->getFields() as $field) { // Get field values $field = $field->getField(); $handle = $field->handle; $label = $field->name; $value = $empty ? '' : craft()->auditLog->parseFieldData($handle, $entry->{$handle}); // Set on fields $fields[$handle] = array('label' => $label, 'value' => $value); } } } // Return return $fields; }