/**
  * Escapes commas and asterisks in a string so they are not treated as special characters in
  * {@link DbHelper::parseParam()}.
  *
  * @param string $value The param value.
  *
  * @return string The escaped param value.
  */
 public function literalFilter($value)
 {
     return DbHelper::escapeParam($value);
 }
Beispiel #2
0
 public function prepTags($data, $field)
 {
     $fieldData = array();
     if (!empty($data)) {
         $settings = $field->getFieldType()->getSettings();
         // Get tag group id
         $source = $settings->getAttribute('source');
         list($type, $groupId) = explode(':', $source);
         $tags = ArrayHelper::stringToArray($data);
         foreach ($tags as $tag) {
             $tagArray = array();
             if (!empty($tag)) {
                 // Find existing tag
                 $criteria = craft()->elements->getCriteria(ElementType::Tag);
                 $criteria->title = DbHelper::escapeParam($tag);
                 $criteria->limit = 1;
                 $criteria->groupId = $groupId;
                 if (!$criteria->total()) {
                     // Create tag if one doesn't already exist
                     $newtag = new TagModel();
                     $newtag->getContent()->title = $tag;
                     $newtag->groupId = $groupId;
                     // Save tag
                     if (craft()->tags->saveTag($newtag)) {
                         $tagArray = array($newtag->id);
                     }
                 } else {
                     $tagArray = $criteria->ids();
                 }
             }
             // Add tags to data array
             $fieldData = array_merge($fieldData, $tagArray);
         }
     }
     return $fieldData;
 }
Beispiel #3
0
 public function importSingleNode($node, $feed, $settings)
 {
     $canSaveEntry = true;
     $existingEntry = false;
     $fieldData = array();
     $entry = array();
     $fields = $settings['fields'];
     //
     // Lets get started!
     //
     $criteria = craft()->feedMe_entry->setCriteria($feed);
     // Start looping through all the mapped fields - grab their data from the feed node
     foreach ($fields as $itemNode => $handle) {
         // Fetch the value for the field from the feed node. Deep-search.
         $data = craft()->feedMe_feed->getValueForNode($itemNode, $node);
         // While we're in the loop, lets check for unique data to match existing entries on.
         if (isset($feed['fieldUnique'][$itemNode]) && intval($feed['fieldUnique'][$itemNode]) == 1 && !empty($data)) {
             $criteria->{$handle} = DbHelper::escapeParam($data);
         }
         //
         // Each field needs special processing, sort that out here
         //
         try {
             // Grab the field's content - formatted specifically for it
             $content = craft()->feedMe_fields->prepForFieldType($data, $handle);
             // The first key of $content will always be the field handle - grab that to create our field data.
             $contentKeys = array_keys($content);
             $fieldHandle = $contentKeys[0];
             // Then, we check if we've already got any partial content for the field. Most commongly, this is
             // the case for Matrix and Table fields, but also likely other Third-Party fields. So its important to
             // combine values, rather than overwriting or omitting as each feed node contains just part of the data.
             if (array_key_exists($fieldHandle, $fieldData) && is_array($fieldData[$fieldHandle])) {
                 $fieldData[$fieldHandle] = array_replace_recursive($fieldData[$fieldHandle], $content[$fieldHandle]);
             } else {
                 $fieldData[$fieldHandle] = $content[$fieldHandle];
             }
         } catch (\Exception $e) {
             FeedMePlugin::log($feed->name . ': FeedMeError: ' . $e->getMessage() . '.', LogLevel::Error, true);
             return array('result' => false);
         }
     }
     $existingEntry = $criteria->first();
     //
     // Check for Add/Update/Delete for existing entries
     //
     // If there's an existing matching entry
     if ($existingEntry && $feed['duplicateHandle']) {
         // If we're deleting
         if ($feed['duplicateHandle'] == FeedMe_Duplicate::Delete) {
             // Fill new EntryModel with match
             $entry = $existingEntry;
         }
         // If we're updating
         if ($feed['duplicateHandle'] == FeedMe_Duplicate::Update) {
             // Fill new EntryModel with match
             $entry = $existingEntry;
         }
         // If we're adding, make sure not to overwrite existing entry
         if ($feed['duplicateHandle'] == FeedMe_Duplicate::Add) {
             $canSaveEntry = false;
         }
     } else {
         // Prepare a new EntryModel (for this section and entrytype)
         $entry = craft()->feedMe_entry->setModel($feed);
     }
     //
     //
     //
     if ($canSaveEntry && $entry) {
         // Any post-processing on our nice collection of entry-ready data.
         craft()->feedMe_fields->postForFieldType($fieldData, $entry);
         // Prepare Element model (the default stuff)
         $entry = craft()->feedMe_entry->prepForElementModel($fieldData, $entry);
         // Set our data for this EntryModel (our mapped data)
         if (!$feed['locale']) {
             $entry->setContentFromPost($fieldData);
         }
         //echo '<pre>';
         //print_r($fieldData);
         //echo '</pre>';
         // Set enabled based on feed settings
         $entry->enabled = (bool) $feed->status;
         try {
             // Save the entry!
             if (!craft()->entries->saveEntry($entry)) {
                 FeedMePlugin::log($feed->name . ': ' . json_encode($entry->getErrors()), LogLevel::Error, true);
                 return array('result' => false);
             } else {
                 // If we're importing into a specific locale, we need to create this entry if it doesn't already exist
                 // completely blank of custom field content. After thats saved, we then re-fetch the entry for the specific
                 // locale and then add our field data. Doing this ensures its not copied across all locales.
                 if ($feed['locale']) {
                     $entryLocale = craft()->entries->getEntryById($entry->id, $feed['locale']);
                     $entryLocale->setContentFromPost($fieldData);
                     if (!craft()->entries->saveEntry($entryLocale)) {
                         FeedMePlugin::log($feed->name . ': ' . json_encode($entryLocale->getErrors()), LogLevel::Error, true);
                         return array('result' => false);
                     } else {
                         // Successfully saved/added entry
                         if ($feed['duplicateHandle'] == FeedMe_Duplicate::Update) {
                             FeedMePlugin::log($feed->name . ': Entry successfully updated: ' . $entryLocale->id, LogLevel::Info, true);
                         } else {
                             FeedMePlugin::log($feed->name . ': Entry successfully added: ' . $entryLocale->id, LogLevel::Info, true);
                         }
                         return array('result' => true, 'entryId' => $entryLocale->id);
                     }
                 } else {
                     // Successfully saved/added entry
                     if ($feed['duplicateHandle'] == FeedMe_Duplicate::Update) {
                         FeedMePlugin::log($feed->name . ': Entry successfully updated: ' . $entry->id, LogLevel::Info, true);
                     } else {
                         FeedMePlugin::log($feed->name . ': Entry successfully added: ' . $entry->id, LogLevel::Info, true);
                     }
                     return array('result' => true, 'entryId' => $entry->id);
                 }
             }
         } catch (\Exception $e) {
             FeedMePlugin::log($feed->name . ': Entry FeedMeError: ' . $e->getMessage() . '.', LogLevel::Error, true);
             return array('result' => false, 'entryId' => $entry->id);
         }
     } else {
         if ($existingEntry) {
             FeedMePlugin::log($feed->name . ': Entry skipped: ' . $existingEntry->id . '.', LogLevel::Error, true);
         }
         return array('result' => true);
     }
 }
 /**
  * Fork of tags/searchForTags adjusted to cope with any element
  */
 public function actionSearchForElements()
 {
     $this->requirePostRequest();
     $this->requireAjaxRequest();
     $search = craft()->request->getPost('search');
     $excludeIds = craft()->request->getPost('excludeIds', array());
     // Get the post data
     $elementType = craft()->request->getPost('elementType');
     $sources = craft()->request->getPost('sources');
     // Deal with Entries
     if ($elementType == ElementType::Entry) {
         // Start the criteria
         $criteria = craft()->elements->getCriteria(ElementType::Entry);
         // Fangle the sections out of the sources
         $sections = array();
         if (is_array($sources)) {
             foreach ($sources as $source) {
                 switch ($source) {
                     case 'singles':
                         $sections = array_merge($sections, craft()->sections->getSectionsByType(SectionType::Single));
                         break;
                     default:
                         if (preg_match('/^section:(\\d+)$/', $source, $matches)) {
                             $section = craft()->sections->getSectionById($matches[1]);
                             if ($section) {
                                 $sections = array_merge($sections, array($section));
                             }
                         }
                 }
             }
         }
         $criteria->section = $sections;
     } else {
         if ($elementType == ElementType::Category) {
             // Start the criteria
             $criteria = craft()->elements->getCriteria(ElementType::Category);
         }
     }
     // Add and exclude ids
     $notIds = array('and');
     foreach ($excludeIds as $id) {
         $notIds[] = 'not ' . $id;
     }
     // Set the rest of the criteria
     $criteria->title = '*' . DbHelper::escapeParam($search) . '*';
     $criteria->id = $notIds;
     $criteria->status = null;
     $criteria->limit = 20;
     $elements = $criteria->find();
     $return = array();
     $exactMatches = array();
     $exactMatch = false;
     $normalizedSearch = StringHelper::normalizeKeywords($search);
     foreach ($elements as $element) {
         if ($elementType == ElementType::Entry) {
             if (!is_array($sources)) {
                 $sourceKey = "*";
             } else {
                 if ($element->section->type == SectionType::Single) {
                     $sourceKey = "singles";
                 } else {
                     $sourceKey = "section:" . $element->section->id;
                 }
             }
             $return[$sourceKey][] = array('id' => $element->id, 'title' => $element->getContent()->title, 'status' => $element->status, 'sourceName' => $element->section->name);
         } else {
             if ($elementType == ElementType::Category) {
                 $sourceKey = "group:" . $element->group->id;
                 $return[$sourceKey][] = array('id' => $element->id, 'title' => $element->getContent()->title, 'status' => $element->status, 'sourceName' => $element->group->name);
             }
         }
         $normalizedTitle = StringHelper::normalizeKeywords($element->getContent()->title);
         if ($normalizedTitle == $normalizedSearch) {
             $exactMatches[] = 1;
             $exactMatch = true;
         } else {
             $exactMatches[] = 0;
         }
     }
     // NOTE: We’ve lost the sorting by exact match
     // array_multisort($exactMatches, SORT_DESC, $return);
     $this->returnJson(array('elements' => $return, 'exactMatch' => $exactMatch));
 }
Beispiel #5
0
 public function importSingleNode($node, $feed, $settings)
 {
     $canSaveEntry = true;
     $existingEntry = false;
     $fieldData = array();
     $entry = array();
     $fields = $settings['fields'];
     //
     // Lets get started!
     //
     $criteria = craft()->feedMe_entry->setCriteria($feed);
     // Start looping through all the mapped fields - grab their data from the feed node
     foreach ($fields as $itemNode => &$destination) {
         // Fetch the value for the field from the feed node. Deep-search.
         $data = craft()->feedMe_feed->getValueForNode($itemNode, $node);
         // While we're in the loop, lets check for unique data to match existing entries on.
         if (isset($feed['fieldUnique'][$itemNode]) && intval($feed['fieldUnique'][$itemNode]) == 1 && !empty($data)) {
             $criteria->{$destination} = DbHelper::escapeParam($data);
         }
         //
         // Each field needs special processing, sort that out here
         //
         try {
             // The field handle needs to be modified in some cases (Matrix and Table). Here, we don't override
             // the original handle for future iterations. We use the original handle to identify Matrix/Table fields.
             $handle = $destination;
             // Grab the field's content - formatted specifically for it
             $content = craft()->feedMe_fields->prepForFieldType($data, $handle);
             // Check to see if this is a Matrix field - need to merge any other fields mapped elsewhere in the feed
             // along with fields we've processed already. Involved due to multiple blocks can be defined at once.
             if (substr($destination, 0, 10) == '__matrix__') {
                 $content = craft()->feedMe_fields->handleMatrixData($fieldData, $handle, $content);
             }
             // And another special case for Table data
             if (substr($destination, 0, 9) == '__table__') {
                 $content = craft()->feedMe_fields->handleTableData($fieldData, $handle, $content);
             }
             // And another special case for SuperTable data
             if (substr($destination, 0, 14) == '__supertable__') {
                 $content = craft()->feedMe_fields->handleSuperTableData($fieldData, $handle, $content);
             }
             // Finally - we have our mapped data, formatted for the particular field as required
             $fieldData[$handle] = $content;
         } catch (\Exception $e) {
             FeedMePlugin::log($feed->name . ': FeedMeError: ' . $e->getMessage() . '.', LogLevel::Error, true);
             return false;
         }
     }
     $existingEntry = $criteria->first();
     //
     // Check for Add/Update/Delete for existing entries
     //
     // If there's an existing matching entry
     if ($existingEntry && $feed['duplicateHandle'] != FeedMe_Duplicate::Delete) {
         // If we're updating
         if ($feed['duplicateHandle'] == FeedMe_Duplicate::Update) {
             // Fill new EntryModel with match
             $entry = $existingEntry;
             // If we're adding, make sure not to overwrite existing entry
         } else {
             if ($feed['duplicateHandle'] == FeedMe_Duplicate::Add) {
                 $canSaveEntry = false;
             }
         }
     } else {
         // Prepare a new EntryModel (for this section and entrytype)
         $entry = craft()->feedMe_entry->setModel($feed);
     }
     //
     //
     //
     if ($canSaveEntry && $entry) {
         // Prepare Element model (the default stuff)
         $entry = craft()->feedMe_entry->prepForElementModel($fieldData, $entry);
         // Set our data for this EntryModel (our mapped data)
         $entry->setContentFromPost($fieldData);
         //echo '<pre>';
         //print_r($entry->title);
         //echo '</pre>';
         try {
             // Save the entry!
             if (!craft()->entries->saveEntry($entry)) {
                 FeedMePlugin::log($feed->name . ': ' . json_encode($entry->getErrors()), LogLevel::Error, true);
                 return false;
             } else {
                 // Successfully saved/added entry
                 if ($feed['duplicateHandle'] == FeedMe_Duplicate::Update) {
                     FeedMePlugin::log($feed->name . ': Entry successfully updated: ' . $entry->id, LogLevel::Info, true);
                 } else {
                     FeedMePlugin::log($feed->name . ': Entry successfully added: ' . $entry->id, LogLevel::Info, true);
                 }
                 return true;
             }
         } catch (\Exception $e) {
             FeedMePlugin::log($feed->name . ': Entry FeedMeError: ' . $e->getMessage() . '.', LogLevel::Error, true);
             return false;
         }
     }
 }
 /**
  * Searches for tags.
  *
  * @return null
  */
 public function actionSearchForTags()
 {
     $this->requirePostRequest();
     $this->requireAjaxRequest();
     $search = craft()->request->getPost('search');
     $tagGroupId = craft()->request->getPost('tagGroupId');
     $excludeIds = craft()->request->getPost('excludeIds', array());
     $notIds = array('and');
     foreach ($excludeIds as $id) {
         $notIds[] = 'not ' . $id;
     }
     $criteria = craft()->elements->getCriteria(ElementType::Tag);
     $criteria->groupId = $tagGroupId;
     $criteria->title = DbHelper::escapeParam($search) . '*';
     $criteria->id = $notIds;
     $tags = $criteria->find();
     $return = array();
     $exactMatches = array();
     $tagTitleLengths = array();
     $exactMatch = false;
     $normalizedSearch = StringHelper::normalizeKeywords($search);
     foreach ($tags as $tag) {
         $return[] = array('id' => $tag->id, 'title' => $tag->getContent()->title);
         $tagTitleLengths[] = mb_strlen($tag->getContent()->title);
         $normalizedTitle = StringHelper::normalizeKeywords($tag->getContent()->title);
         if ($normalizedTitle == $normalizedSearch) {
             $exactMatches[] = 1;
             $exactMatch = true;
         } else {
             $exactMatches[] = 0;
         }
     }
     array_multisort($exactMatches, SORT_DESC, $tagTitleLengths, $return);
     $this->returnJson(array('tags' => $return, 'exactMatch' => $exactMatch));
 }