public function prepForElementModel(&$fields, EntryModel $element) { // Set author $author = FeedMe_Element::Author; if (isset($fields[$author])) { $user = craft()->users->getUserByUsernameOrEmail($fields[$author]); $element->{$author} = is_numeric($fields[$author]) ? $fields[$author] : ($user ? $user->id : 1); //unset($fields[$author]); } else { $user = craft()->userSession->getUser(); $element->{$author} = $element->{$author} ? $element->{$author} : ($user ? $user->id : 1); } // Set slug $slug = FeedMe_Element::Slug; if (isset($fields[$slug])) { $element->{$slug} = ElementHelper::createSlug($fields[$slug]); //unset($fields[$slug]); } // Set postdate $postDate = FeedMe_Element::PostDate; if (isset($fields[$postDate])) { $d = date_parse($fields[$postDate]); $date_string = date('Y-m-d H:i:s', mktime($d['hour'], $d['minute'], $d['second'], $d['month'], $d['day'], $d['year'])); $element->{$postDate} = DateTime::createFromString($date_string, craft()->timezone); //unset($fields[$postDate]); } // Set expiry date $expiryDate = FeedMe_Element::ExpiryDate; if (isset($fields[$expiryDate])) { $d = date_parse($fields[$expiryDate]); $date_string = date('Y-m-d H:i:s', mktime($d['hour'], $d['minute'], $d['second'], $d['month'], $d['day'], $d['year'])); $element->{$expiryDate} = DateTime::createFromString($date_string, craft()->timezone); //unset($fields[$expiryDate]); } // Set enabled $enabled = FeedMe_Element::Enabled; if (isset($fields[$enabled])) { $element->{$enabled} = (bool) $fields[$enabled]; //unset($fields[$enabled]); } // Set title $title = FeedMe_Element::Title; if (isset($fields[$title])) { $element->getContent()->{$title} = $fields[$title]; //unset($fields[$title]); } // Set parent or ancestors $parent = FeedMe_Element::Parent; $ancestors = FeedMe_Element::Ancestors; if (isset($fields[$parent])) { $data = $fields[$parent]; // Don't connect empty fields if (!empty($data)) { // Find matching element $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $element->sectionId; $criteria->search = '"' . $data . '"'; // Return the first found element for connecting if ($criteria->total()) { $element->{$parent} = $criteria->first()->id; } } //unset($fields[$parent]); } elseif (isset($fields[$ancestors])) { $data = $fields[$ancestors]; // Don't connect empty fields if (!empty($data)) { // Get section data $section = new SectionModel(); $section->id = $element->sectionId; // This we append before the slugified path $sectionUrl = str_replace('{slug}', '', $section->getUrlFormat()); // Find matching element by URI (dirty, not all structures have URI's) $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $element->sectionId; $criteria->uri = $sectionUrl . craft()->feedMe->slugify($data); $criteria->limit = 1; // Return the first found element for connecting if ($criteria->total()) { $element->{$parent} = $criteria->first()->id; } } //unset($fields[$ancestors]); } // Return element return $element; }
/** * Prepare reserved ElementModel values. * * @param array &$fields * @param BaseElementModel $element * * @return BaseElementModel */ public function prepForElementModel(array &$fields, BaseElementModel $element) { // Set author $author = Import_ElementModel::HandleAuthor; if (isset($fields[$author])) { $user = craft()->users->getUserByUsernameOrEmail($fields[$author]); $element->{$author} = is_numeric($fields[$author]) ? $fields[$author] : ($user ? $user->id : 1); unset($fields[$author]); } else { $user = craft()->userSession->getUser(); $element->{$author} = $element->{$author} ? $element->{$author} : ($user ? $user->id : 1); } // Set slug $slug = Import_ElementModel::HandleSlug; if (isset($fields[$slug])) { $element->{$slug} = ElementHelper::createSlug($fields[$slug]); unset($fields[$slug]); } // Set postdate $postDate = Import_ElementModel::HandlePostDate; if (isset($fields[$postDate])) { $element->{$postDate} = DateTime::createFromString($fields[$postDate], craft()->timezone); unset($fields[$postDate]); } // Set expiry date $expiryDate = Import_ElementModel::HandleExpiryDate; if (isset($fields[$expiryDate])) { $element->{$expiryDate} = DateTime::createFromString($fields[$expiryDate], craft()->timezone); unset($fields[$expiryDate]); } // Set enabled $enabled = Import_ElementModel::HandleEnabled; if (isset($fields[$enabled])) { $element->{$enabled} = (bool) $fields[$enabled]; unset($fields[$enabled]); } // Set title $title = Import_ElementModel::HandleTitle; if (isset($fields[$title])) { $element->getContent()->{$title} = $fields[$title]; unset($fields[$title]); } // Set parent or ancestors $parent = Import_ElementModel::HandleParent; $ancestors = Import_ElementModel::HandleAncestors; if (isset($fields[$parent])) { // Get data $data = $fields[$parent]; // Fresh up $data $data = str_replace("\n", '', $data); $data = str_replace("\r", '', $data); $data = trim($data); // Don't connect empty fields if (!empty($data)) { // Find matching element $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $element->sectionId; // Exact match $criteria->search = '"' . $data . '"'; // Return the first found element for connecting if ($criteria->total()) { $element->{$parent} = $criteria->first()->id; } } unset($fields[$parent]); } elseif (isset($fields[$ancestors])) { // Get data $data = $fields[$ancestors]; // Fresh up $data $data = str_replace("\n", '', $data); $data = str_replace("\r", '', $data); $data = trim($data); // Don't connect empty fields if (!empty($data)) { // Get section data $section = new SectionModel(); $section->id = $element->sectionId; // This we append before the slugified path $sectionUrl = str_replace('{slug}', '', $section->getUrlFormat()); // Find matching element by URI (dirty, not all structures have URI's) $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $element->sectionId; $criteria->uri = $sectionUrl . craft()->import->slugify($data); $criteria->limit = 1; // Return the first found element for connecting if ($criteria->total()) { $element->{$parent} = $criteria->first()->id; } } unset($fields[$ancestors]); } // Return element return $element; }
/** * @param string $data * @param int $sectionId * * @return null|int */ private function prepareAncestorsForElement($data, $sectionId) { $parentId = null; $data = $this->freshenString($data); // Don't connect empty fields if (!empty($data)) { // Get section data $section = new SectionModel(); $section->id = $sectionId; // This we append before the slugified path $sectionUrl = str_replace('{slug}', '', $section->getUrlFormat()); // Find matching element by URI (dirty, not all structures have URI's) $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $sectionId; $criteria->uri = $sectionUrl . craft()->import->slugify($data); $criteria->limit = 1; // Return the first found element for connecting if ($criteria->total()) { $parentId = $criteria->ids()[0]; } } return $parentId; }