public function actionSaveDefault()
 {
     $this->requirePostRequest();
     // check if this is a new or existing default
     if (craft()->request->getPost('sproutseo_fields[id]') == null) {
         $id = false;
     } else {
         $id = craft()->request->getPost('sproutseo_fields[id]');
     }
     $model = new SproutSeo_MetaModel();
     $model->id = $id;
     $defaultFields = craft()->request->getPost('sproutseo_fields');
     // Convert Checkbox Array into comma-delimited String
     if (isset($defaultFields['robots'])) {
         $defaultFields['robots'] = SproutSeoMetaHelper::prepRobotsAsString($defaultFields['robots']);
     }
     // Make our images single IDs instead of an array
     $defaultFields['ogImage'] = !empty($defaultFields['ogImage']) ? $defaultFields['ogImage'][0] : null;
     $defaultFields['twitterImage'] = !empty($defaultFields['twitterImage']) ? $defaultFields['twitterImage'][0] : null;
     $model->setAttributes($defaultFields);
     if (sproutSeo()->defaults->saveDefault($model)) {
         craft()->userSession->setNotice(Craft::t('New default saved.'));
         $this->redirectToPostedUrl();
     } else {
         craft()->userSession->setError(Craft::t("Couldn't save the default."));
         // Send the field back to the template
         craft()->urlManager->setRouteVariables(array('default' => $model));
     }
 }
 /**
  * @param $handle
  * @return BaseModel|SproutSeo_MetaModel
  */
 public function getDefaultByHandle($handle)
 {
     $query = craft()->db->createCommand()->select('*')->from('sproutseo_defaults')->where('handle=:handle', array(':handle' => $handle))->queryRow();
     if (isset($query)) {
         $model = SproutSeo_MetaModel::populateModel($query);
     } else {
         return new SproutSeo_MetaModel();
     }
     $model->robots = $model->robots ? SproutSeoMetaHelper::prepRobotsForSettings($model->robots) : null;
     $model->position = SproutSeoMetaHelper::prepareGeoPosition($model);
     return $model;
 }
 /**
  * Performs any additional actions after the element has been saved.
  */
 public function onAfterElementSave()
 {
     // grab only the geo fields
     $fields = isset($_POST['fields']['sproutseo_fields']) ? $_POST['fields']['sproutseo_fields'] : null;
     if (!isset($fields)) {
         return;
     }
     $entryId = isset($_POST['entryId']) && $_POST['entryId'] != "" ? $_POST['entryId'] : $this->element->id;
     $locale = $this->element->locale;
     $model = sproutSeo()->overrides->getOverrideByEntryId($entryId, $locale);
     // Test to see if we have any values in our Sprout SEO fields
     $saveSproutSeoFields = false;
     foreach ($_POST['fields']['sproutseo_fields'] as $key => $value) {
         if ($value) {
             $saveSproutSeoFields = true;
             continue;
         }
     }
     // If we don't have any values in our Sprout SEO fields
     // don't add a record to the database
     // but if a record already exists, we also should delete it.
     if (!$saveSproutSeoFields) {
         // Remove record since it is now blank
         if ($model->id) {
             sproutSeo()->overrides->deleteOverrideById($model->id);
         }
         return;
     }
     if (isset($_POST['fields']['sproutseo_fields']['robots'])) {
         $fields['robots'] = SproutSeoMetaHelper::prepRobotsAsString($_POST['fields']['sproutseo_fields']['robots']);
     }
     // Add the entry ID to the field data we will submit for Sprout SEO
     $attributes['entryId'] = $entryId;
     $attributes['locale'] = $locale;
     // Grab all the other Sprout SEO fields.
     $attributes = array_merge($attributes, $fields);
     // Make sure all of our images are strings (twitter/og)
     // We need to do this in case another seo field with images exists
     $attributes['twitterImage'] = !empty($attributes['twitterImage']) ? $attributes['twitterImage'][0] : null;
     $attributes['ogImage'] = !empty($attributes['ogImage']) ? $attributes['ogImage'][0] : null;
     // If our override entry exists update it,
     // if not create it
     if ($model->entryId) {
         sproutSeo()->overrides->updateOverride($model->id, $attributes);
     } else {
         sproutSeo()->overrides->createOverride($attributes);
     }
 }
 public function getMetaTagData(SproutSeo_MetaModel $meta)
 {
     $tagData = array();
     foreach ($this->getAttributes() as $key => $value) {
         if ($key == 'latitude' or $key == 'longitude') {
             break;
         }
         if ($meta->{$key}) {
             $value = $meta[$key];
             if ($key == 'position') {
                 $value = SproutSeoMetaHelper::prepareGeoPosition($meta);
             }
             $tagData[$this->getMetaTagName($key)] = $value;
         }
     }
     return $tagData;
 }
 /**
  * @param string $type
  * @param array $overrideInfo
  * @return $this
  * @throws \Exception
  */
 public function setMeta($type = 'fallback', $overrideInfo = array())
 {
     switch ($type) {
         case 'entry':
             $this->setAttributes($this->getEntryOverride($overrideInfo));
             break;
         case 'code':
             $this->setAttributes($this->getCodeOverride($overrideInfo));
             break;
         case 'default':
             $this->setAttributes($this->getDefault($overrideInfo));
             break;
         case 'fallback':
             $this->setAttributes($this->getGlobalFallback($overrideInfo));
             break;
     }
     SproutSeoMetaHelper::prepareAssetUrls($this);
     return $this;
 }
 /**
  * Prioritize our meta data
  * ------------------------------------------------------------
  *
  * Loop through and select the highest ranking value for each attribute in our SproutSeo_MetaData model
  *
  * 1) Entry Override (Set by adding `id` override in Twig template code and using Meta Fields)
  * 2) On-Page Override (Set in Twig template code)
  * 3) Default (Set in control panel)
  * 4) Global Fallback (Set in control panel)
  * 5) Blank (Automatic)
  *
  * Once we have added all the content we need to be outputting to our array we will loop through that array and create the HTML we will output to our page.
  *
  * While we don't define HTML in our PHP as much as possible, the goal here is to be as easy to use as possible on the front end so we want to simplify the front end code to a single function and wrangle what we need to here.
  *
  * @return array
  * @throws \Exception
  */
 public function getOptimizedMeta()
 {
     $entryOverrideMetaModel = new SproutSeo_MetaModel();
     $codeOverrideMetaModel = new SproutSeo_MetaModel();
     $defaultMetaModel = new SproutSeo_MetaModel();
     $globalFallbackMetaModel = new SproutSeo_MetaModel();
     // Prepare a SproutSeo_MetaModel for each of our levels of priority
     $entryOverrideMetaModel = $entryOverrideMetaModel->setMeta('entry', $this->getMeta());
     $codeOverrideMetaModel = $codeOverrideMetaModel->setMeta('code', $this->getMeta());
     $defaultMetaModel = $defaultMetaModel->setMeta('default', $this->getMeta());
     $globalFallbackMetaModel = $globalFallbackMetaModel->setMeta('fallback');
     $prioritizedMetaModel = new SproutSeo_MetaModel();
     $this->divider = craft()->plugins->getPlugin('sproutseo')->getSettings()->seoDivider;
     // Default to the Current URL
     $prioritizedMetaModel->canonical = SproutSeoMetaHelper::prepareCanonical($prioritizedMetaModel);
     foreach ($prioritizedMetaModel->getAttributes() as $key => $value) {
         // Test for a value on each of our models in their order of priority
         if ($entryOverrideMetaModel->getAttribute($key)) {
             $prioritizedMetaModel[$key] = $entryOverrideMetaModel[$key];
         } elseif ($codeOverrideMetaModel->getAttribute($key)) {
             $prioritizedMetaModel[$key] = $codeOverrideMetaModel[$key];
         } elseif ($defaultMetaModel->getAttribute($key)) {
             $prioritizedMetaModel[$key] = $defaultMetaModel->getAttribute($key);
         } elseif ($globalFallbackMetaModel->getAttribute($key)) {
             $prioritizedMetaModel[$key] = $globalFallbackMetaModel->getAttribute($key);
         } else {
             $prioritizedMetaModel[$key] = $prioritizedMetaModel->getAttribute($key);
         }
     }
     // @todo - reorganize how this stuff works / robots need love.
     $prioritizedMetaModel->title = SproutSeoMetaHelper::prepareAppendedSiteName($prioritizedMetaModel, $defaultMetaModel, $globalFallbackMetaModel);
     $prioritizedMetaModel->robots = SproutSeoMetaHelper::prepRobotsAsString($prioritizedMetaModel->robots);
     return $prioritizedMetaModel;
 }