public function safeUp()
 {
     //First get all option types and values and make dropdown fields
     $alloptionTypes = craft()->db->createCommand()->select('*')->from('market_optiontypes')->queryAll();
     $fields = [];
     foreach ($alloptionTypes as $optionType) {
         // Need to know if the field name is already taken
         $exists = craft()->fields->getFieldByHandle(ElementHelper::createSlug($optionType['handle']));
         //make a new field name is field name already taken
         if ($exists && $exists->id) {
             $optionType['handle'] = $optionType['handle'] . " option";
         }
         //Make a new field
         $field = new FieldModel();
         $field->groupId = 1;
         $field->context = 'global';
         $field->name = $optionType['name'];
         $field->handle = ElementHelper::createSlug($optionType['handle']);
         $field->type = "Dropdown";
         // Add the values
         $optionValues = craft()->db->createCommand()->select('*')->from('market_optionvalues')->where('optionTypeId=:id', [':id' => $optionType['id']])->queryAll();
         $options = [];
         foreach ($optionValues as $ov) {
             $value = ['label' => $ov['displayName'], 'value' => $ov['name']];
             $options['options'][] = $value;
         }
         $field->settings = $options;
         //save the field
         craft()->fields->saveField($field);
         $fields[] = $field;
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * Uses the matched headers to create an anchor for them
  *
  * @param array $matches
  *
  * @return string
  */
 public function handleMatch(array $matches = array())
 {
     $tag = $matches['tag'];
     $text = $matches['text'];
     $slug = ElementHelper::createSlug($text);
     $clean = strip_tags($text);
     $currentHeaderLevel = (int) substr($tag, 1, 1);
     if ($this->startingHeaderLevel) {
         $tag = sprintf('h%s', min(6, $currentHeaderLevel + ($this->startingHeaderLevel - 1)));
     }
     return "<{$tag} id=\"{$slug}\">{$text} <a class=\"anchor\" href=\"#{$slug}\" title=\"{$clean}\">#</a></{$tag}>";
 }
Exemplo n.º 3
0
 /**
  * Prepare reserved ElementModel values.
  *
  * @param array            &$fields
  * @param BaseElementModel $element
  *
  * @return BaseElementModel
  */
 public function prepForElementModel(array &$fields, BaseElementModel $element)
 {
     // Set slug
     $slug = Import_ElementModel::HandleSlug;
     if (isset($fields[$slug])) {
         $element->{$slug} = ElementHelper::createSlug($fields[$slug]);
         unset($fields[$slug]);
     }
     // Set title
     $title = Import_ElementModel::HandleTitle;
     if (isset($fields[$title])) {
         $element->getContent()->{$title} = $fields[$title];
         unset($fields[$title]);
     }
     // Return element
     return $element;
 }
 /**
  * Open file and parse translate tags.
  *
  * @param string               $path
  * @param string               $file
  * @param ElementCriteriaModel $criteria
  *
  * @return array
  */
 protected function _parseFile($path, $file, ElementCriteriaModel $criteria)
 {
     // Collect matches in file
     $occurences = array();
     // Get file contents
     $contents = IOHelper::getFileContents($file);
     // Get extension
     $extension = IOHelper::getExtension($file);
     // Get matches per extension
     foreach ($this->_expressions[$extension] as $regex) {
         // Match translation functions
         if (preg_match_all($regex, $contents, $matches)) {
             // Collect
             foreach ($matches[2] as $original) {
                 // Translate
                 $translation = Craft::t($original, array(), null, $criteria->locale);
                 // Show translation in textfield
                 $field = craft()->templates->render('_includes/forms/text', array('id' => ElementHelper::createSlug($original), 'name' => 'translation[' . $original . ']', 'value' => $translation, 'placeholder' => $translation));
                 // Fill element with translation data
                 $element = TranslateModel::populateModel(array('id' => ElementHelper::createSlug($original), 'original' => $original, 'translation' => $translation, 'source' => $path, 'file' => $file, 'locale' => $criteria->locale, 'field' => $field));
                 // If searching, only return matches
                 if ($criteria->search && !stristr($element->original, $criteria->search) && !stristr($element->translation, $criteria->search)) {
                     continue;
                 }
                 // If wanting one status, ditch the rest
                 if ($criteria->status && $criteria->status != $element->getStatus()) {
                     continue;
                 }
                 // Collect in array
                 $occurences[$original] = $element;
             }
         }
     }
     // Return occurences
     return $occurences;
 }
 /**
  * Prepare reserved ElementModel values.
  *
  * @param array            &$fields
  * @param BaseElementModel $element
  *
  * @return BaseElementModel
  */
 public function prepForElementModel(array &$fields, BaseElementModel $element)
 {
     foreach ($fields as $handle => $value) {
         switch ($handle) {
             case Import_ElementModel::HandleLocale:
                 $element->localeEnabled = true;
             case Import_ElementModel::HandleId:
                 $element->{$handle} = $value;
                 break;
             case Import_ElementModel::HandleSlug:
                 $element->{$handle} = ElementHelper::createSlug($value);
                 break;
             case Import_ElementModel::HandleTitle:
                 $element->getContent()->{$handle} = $value;
                 break;
             default:
                 continue 2;
         }
         unset($fields[$handle]);
     }
     return $element;
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
 /**
  * Slugify function.
  *
  * @param string $slug
  *
  * @return string
  */
 public function slugify($slug)
 {
     return ElementHelper::createSlug($slug);
 }
Exemplo n.º 8
0
 /**
  * @param string $text
  *
  * @return string
  */
 public function createHandle($text)
 {
     $text = ElementHelper::createSlug($text);
     $words = explode('-', $text);
     $start = array_shift($words);
     if (count($words)) {
         foreach ($words as $word) {
             $start = $start . ucfirst($word);
         }
     }
     return $start;
 }
Exemplo n.º 9
0
 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)
 {
     $author = Import_ElementModel::HandleAuthor;
     $parent = Import_ElementModel::HandleParent;
     $checkAncestors = !isset($fields[$parent]);
     foreach ($fields as $handle => $value) {
         switch ($handle) {
             case Import_ElementModel::HandleLocale:
                 $element->localeEnabled = true;
             case Import_ElementModel::HandleId:
                 $element->{$handle} = $value;
                 break;
             case Import_ElementModel::HandleAuthor:
                 $element->{$handle} = $this->prepAuthorForElement($value);
                 break;
             case Import_ElementModel::HandleSlug:
                 $element->{$handle} = ElementHelper::createSlug($value);
                 break;
             case Import_ElementModel::HandlePostDate:
             case Import_ElementModel::HandleExpiryDate:
                 $element->{$handle} = DateTime::createFromString($value, craft()->timezone);
                 break;
             case Import_ElementModel::HandleEnabled:
                 $element->{$handle} = (bool) $value;
                 break;
             case Import_ElementModel::HandleTitle:
                 $element->getContent()->{$handle} = $value;
                 break;
             case Import_ElementModel::HandleParent:
                 $element->{$handle} = $this->prepareParentForElement($value, $element->sectionId);
                 break;
             case Import_ElementModel::HandleAncestors:
                 if ($checkAncestors) {
                     $element->{$parent} = $this->prepareAncestorsForElement($value, $element->sectionId);
                 }
                 break;
             default:
                 continue 2;
         }
         unset($fields[$handle]);
     }
     // Set default author if not set
     if (!$element->{$author}) {
         $user = craft()->userSession->getUser();
         $element->{$author} = $element->{$author} ? $element->{$author} : ($user ? $user->id : 1);
     }
     // Return element
     return $element;
 }
 /**
  * Populates a SproutEmail_EntryModel with post data
  *
  * @param SproutEmail_EntryModel $entry
  *
  * @return \Craft\SproutEmail_EntryModel
  */
 protected function populateEntryModel(SproutEmail_EntryModel $entry)
 {
     $entry->campaignId = $this->campaign->id;
     $entry->slug = craft()->request->getPost('slug', $entry->slug);
     $entry->enabled = (bool) craft()->request->getPost('enabled', $entry->enabled);
     $entry->fromName = craft()->request->getPost('sproutEmail.fromName');
     $entry->fromEmail = craft()->request->getPost('sproutEmail.fromEmail');
     $entry->replyTo = craft()->request->getPost('sproutEmail.replyTo');
     $entry->subjectLine = craft()->request->getRequiredPost('subjectLine');
     $entry->getContent()->title = $entry->subjectLine;
     if (empty($entry->slug)) {
         $entry->slug = ElementHelper::createSlug($entry->subjectLine);
     }
     $fieldsLocation = craft()->request->getParam('fieldsLocation', 'fields');
     $entry->setContentFromPost($fieldsLocation);
     $entry->setContentPostLocation($fieldsLocation);
     return $entry;
 }