/**
  * Import WP Post into InstaBlog Section
  *
  * @param mixed $settings Array of settings
  * @param mixed $post Entry Posts array from WP XML
  * @param number $categoryGroupId Category Group ID for InstaBlog Categories
  * @param number $tagGroupId Tag Group ID for InstaBlog Tags
  * @param string $baseUrl Base url of WP blog
  * 
  * @return Null
  */
 protected function _importPost($settings, $post, $categoryGroupId, $tagGroupId, $baseUrl)
 {
     $postContent = $post['post_content'];
     $featuredImage = array();
     $categoryIds = array();
     $tagIds = array();
     // If entry slug exists abort
     if ($this->_getEntry($post['post_name'])) {
         Craft::log('Could not save the InstaBlog entry:' . $post['post_name'] . ' It already exists.', LogLevel::Info, false, 'application', 'InstaBlog');
         return;
     }
     // Import Tags / Categories associated with post
     if (array_key_exists('terms', $post)) {
         foreach ($post['terms'] as $term) {
             switch ($term['domain']) {
                 case 'category':
                     $categoryIds[] = $this->_importCategory($term['name'], $categoryGroupId);
                     break;
                 case 'post_tag':
                     $tagIds[] = $this->_importTag($term['name'], $tagGroupId);
                     break;
             }
         }
     }
     // Import Featured Images
     foreach ($post['postmeta'] as $postmetaArray) {
         if ($postmetaArray['key'] === '_thumbnail_id') {
             $featuredImage[] = $this->_importFeaturedImage($settings, $post['post_link'], $baseUrl);
         }
     }
     // Import Images referenced in post_content
     $postContent = $this->_importImages($settings, $postContent, $baseUrl);
     // Replace missing paragraph elements. Nice goin' WordPress.
     $postContent = $this->_wpautop($postContent);
     // Get values for entry
     $section = craft()->sections->getSectionByHandle('instaBlog');
     $entryTypes = $section->getEntryTypes();
     $entryType = $entryTypes[0];
     // Save Entry
     $entry = new EntryModel();
     $entry->sectionId = $section->id;
     $entry->typeId = $entryType->id;
     $entry->locale = craft()->i18n->getPrimarySiteLocaleId();
     $entry->authorId = $this->_getUserId($post['post_author']);
     $entry->enabled = $post['status'] === 'publish' ? true : false;
     $entry->postDate = $post['post_date'];
     $entry->slug = $post['post_name'];
     $entry->getContent()->title = $post['post_title'];
     $entry->setContentFromPost(array('instaBlogBody' => $postContent, 'instaBlogImage' => $featuredImage, 'instaBlogCategories' => $categoryIds, 'instaBlogTags' => $tagIds));
     if (craft()->entries->saveEntry($entry)) {
         Craft::log('InstaBlog entry created successfully.', LogLevel::Info, false, '_importPost', 'InstaBlog');
     } else {
         Craft::log('Could not save the InstaBlog entry.', LogLevel::Error, true, '_importPost', 'InstaBlog');
     }
 }
 /**
  * Populates an EntryModel with post data.
  *
  * @access private
  * @param $settings
  * @throws HttpException
  * @return EntryModel
  */
 private function _populateEntryModel($settings)
 {
     $entry = new EntryModel();
     $entry->sectionId = craft()->request->getRequiredPost('sectionId');
     $this->_section = craft()->sections->getSectionById($entry->sectionId);
     if (!$this->_section) {
         throw new HttpException(404);
     }
     // If we're allowing guest submissions and we've got a default author specified, grab the authorId.
     if ($settings->allowGuestSubmissions && isset($settings->defaultAuthors[$this->_section->handle]) && $settings->defaultAuthors[$this->_section->handle] !== 'none') {
         // We found a defaultAuthor
         $authorId = $settings->defaultAuthors[$this->_section->handle];
     } else {
         // Otherwise, complain loudly.
         throw new HttpException(403);
     }
     $localeId = craft()->request->getPost('locale');
     if ($localeId) {
         $entry->locale = $localeId;
     }
     $entry->typeId = craft()->request->getPost('typeId');
     $postDate = craft()->request->getPost('postDate');
     if ($postDate) {
         DateTime::createFromString($postDate, craft()->timezone);
     }
     $expiryDate = craft()->request->getPost('expiryDate');
     if ($expiryDate) {
         DateTime::createFromString($expiryDate, craft()->timezone);
     }
     $entry->authorId = $authorId;
     $entry->slug = craft()->request->getPost('slug');
     $entry->postDate = $postDate;
     $entry->expiryDate = $expiryDate;
     $entry->enabled = (bool) $settings->enabledByDefault[$this->_section->handle];
     if (($localeEnabled = craft()->request->getPost('localeEnabled', null)) === null) {
         $localeEnabled = true;
     }
     $entry->localeEnabled = (bool) $localeEnabled;
     $entry->getContent()->title = craft()->request->getPost('title');
     $fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields');
     $entry->setContentFromPost($fieldsLocation);
     $entry->parentId = craft()->request->getPost('parentId');
     return $entry;
 }
예제 #3
0
 /**
  * Populates an EntryModel with post data.
  *
  * @param EntryModel $entry
  *
  * @return null
  */
 private function _populateEntryModel(EntryModel $entry)
 {
     // Set the entry attributes, defaulting to the existing values for whatever is missing from the post data
     $entry->typeId = craft()->request->getPost('typeId', $entry->typeId);
     $entry->authorId = craft()->request->getPost('author', $entry->authorId ? $entry->authorId : craft()->userSession->getUser()->id);
     $entry->slug = craft()->request->getPost('slug', $entry->slug);
     $entry->postDate = ($postDate = craft()->request->getPost('postDate')) ? DateTime::createFromString($postDate, craft()->timezone) : $entry->postDate;
     $entry->expiryDate = ($expiryDate = craft()->request->getPost('expiryDate')) ? DateTime::createFromString($expiryDate, craft()->timezone) : null;
     $entry->enabled = (bool) craft()->request->getPost('enabled', $entry->enabled);
     $entry->localeEnabled = (bool) craft()->request->getPost('localeEnabled', $entry->localeEnabled);
     $entry->getContent()->title = craft()->request->getPost('title', $entry->title);
     $fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields');
     $entry->setContentFromPost($fieldsLocation);
     $entry->parentId = craft()->request->getPost('parentId');
     $entry->revisionNotes = craft()->request->getPost('revisionNotes');
 }