/**
  * @param mixed|null $element
  *
  * @throws \Exception
  * @return array|string
  */
 public function getRecipients($element = null)
 {
     $recipientsString = $this->getAttribute('recipients');
     // Possibly called from entry edit screen
     if (is_null($element)) {
         return $recipientsString;
     }
     // Previously converted to array somehow?
     if (is_array($recipientsString)) {
         return $recipientsString;
     }
     // Previously stored as JSON string?
     if (stripos($recipientsString, '[') === 0) {
         return JsonHelper::decode($recipientsString);
     }
     // Still a string with possible twig generator code?
     if (stripos($recipientsString, '{') !== false) {
         try {
             $recipients = craft()->templates->renderObjectTemplate($recipientsString, $element);
             return array_unique(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::stringToArray($recipients)));
         } catch (\Exception $e) {
             throw $e;
         }
     }
     // Just a regular CSV list
     if (!empty($recipientsString)) {
         return ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::stringToArray($recipientsString));
     }
     return array();
 }
Exemplo n.º 2
0
 public function slugify($slug)
 {
     // Remove HTML tags
     $slug = preg_replace('/<(.*?)>/u', '', $slug);
     // Remove inner-word punctuation.
     $slug = preg_replace('/[\'"‘’“”]/u', '', $slug);
     // Make it lowercase
     $slug = mb_strtolower($slug, 'UTF-8');
     // Get the "words".  Split on anything that is not a unicode letter or number.
     // Periods are OK too.
     preg_match_all('/[\\p{L}\\p{N}\\.]+/u', $slug, $words);
     $words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
     $slug = implode('-', $words);
     return $slug;
 }
Exemplo n.º 3
0
 /**
  * Creates a slug based on a given string.
  *
  * @param string $str
  *
  * @return string
  */
 public static function createSlug($str)
 {
     // Remove HTML tags
     $slug = preg_replace('/<(.*?)>/u', '', $str);
     // Remove inner-word punctuation.
     $slug = preg_replace('/[\'"‘’“”\\[\\]\\(\\)\\{\\}:]/u', '', $slug);
     if (craft()->config->get('allowUppercaseInSlug') === false) {
         // Make it lowercase
         $slug = StringHelper::toLowerCase($slug);
     }
     // Get the "words". Split on anything that is not alphanumeric, or a period, underscore, or hyphen.
     preg_match_all('/[\\p{L}\\p{N}\\._-]+/u', $slug, $words);
     $words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
     $slug = implode(craft()->config->get('slugWordSeparator'), $words);
     return $slug;
 }
Exemplo n.º 4
0
 /**
  * Generates an entry slug based on its title.
  *
  * @access private
  * @param EntryModel $entry
  */
 private function _generateEntrySlug(EntryModel $entry)
 {
     $slug = $entry->slug ? $entry->slug : $entry->getTitle();
     // Remove HTML tags
     $slug = preg_replace('/<(.*?)>/', '', $slug);
     // Remove apostrophes
     $slug = str_replace(array('\'', '’'), array('', ''), $slug);
     // Make it lowercase
     $slug = strtolower($slug);
     // Convert extended ASCII characters to basic ASCII
     $slug = StringHelper::asciiString($slug);
     // Slug must start and end with alphanumeric characters
     $slug = preg_replace('/^[^a-z0-9]+/', '', $slug);
     $slug = preg_replace('/[^a-z0-9]+$/', '', $slug);
     // Get the "words"
     $words = preg_split('/[^a-z0-9]+/', $slug);
     $words = ArrayHelper::filterEmptyStringsFromArray($words);
     $slug = implode('-', $words);
     if ($slug) {
         // Make it unique
         $conditions = array('and', 'sectionId = :sectionId', 'locale = :locale', 'slug = :slug');
         $params = array(':sectionId' => $entry->sectionId, ':locale' => $entry->locale);
         if ($entry->id) {
             $conditions[] = 'id != :entryId';
             $params[':entryId'] = $entry->id;
         }
         for ($i = 0; true; $i++) {
             $testSlug = $slug . ($i != 0 ? "-{$i}" : '');
             $params[':slug'] = $testSlug;
             $totalEntries = craft()->db->createCommand()->select('count(id)')->from('entries_i18n')->where($conditions, $params)->queryScalar();
             if ($totalEntries == 0) {
                 break;
             }
         }
         $entry->slug = $testSlug;
     } else {
         $entry->slug = '';
     }
 }
Exemplo n.º 5
0
 /**
  * Function that (almost) mimics Craft's inner slugify process.
  * But... we allow forward slashes to stay, so we can create full uri's.
  *
  * @param string $slug
  *
  * @return string
  */
 public function slugify($slug)
 {
     // Remove HTML tags
     $slug = preg_replace('/<(.*?)>/u', '', $slug);
     // Remove inner-word punctuation.
     $slug = preg_replace('/[\'"‘’“”\\[\\]\\(\\)\\{\\}:]/u', '', $slug);
     if (craft()->config->get('allowUppercaseInSlug') === false) {
         // Make it lowercase
         $slug = StringHelper::toLowerCase($slug);
     }
     // Get the "words".  Split on anything that is not a unicode letter or number. Periods, underscores, hyphens and forward slashes get a pass.
     preg_match_all('/[\\p{L}\\p{N}\\.\\/_-]+/u', $slug, $words);
     $words = ArrayHelper::filterEmptyStringsFromArray($words[0]);
     $slug = implode(craft()->config->get('slugWordSeparator'), $words);
     return $slug;
 }
Exemplo n.º 6
0
 /**
  * Splits a string into an array of the words in the string.
  *
  * @param string $string The string
  *
  * @return string[]
  */
 public static function splitOnWords($string)
 {
     // Split on anything that is not alphanumeric, or a period, underscore, or hyphen.
     // Reference: http://www.regular-expressions.info/unicode.html
     preg_match_all('/[\\p{L}\\p{N}\\p{M}\\._-]+/u', $string, $matches);
     return ArrayHelper::filterEmptyStringsFromArray($matches[0]);
 }
Exemplo n.º 7
0
 /**
  * Parse field values.
  *
  * @param string $handle
  * @param mixed  $data
  *
  * @return string
  */
 protected function parseFieldData($handle, $data)
 {
     // Do we have any data at all
     if (!is_null($data)) {
         // Get field info
         $field = craft()->fields->getFieldByHandle($handle);
         // If it's a field ofcourse
         if (!is_null($field)) {
             // For some fieldtypes the're special rules
             switch ($field->type) {
                 case ExportModel::FieldTypeEntries:
                 case ExportModel::FieldTypeCategories:
                 case ExportModel::FieldTypeAssets:
                 case ExportModel::FieldTypeUsers:
                     // Show names
                     $data = $data instanceof ElementCriteriaModel ? implode(', ', $data->find()) : $data;
                     break;
                 case ExportModel::FieldTypeLightswitch:
                     // Make data human readable
                     switch ($data) {
                         case '0':
                             $data = Craft::t('No');
                             break;
                         case '1':
                             $data = Craft::t('Yes');
                             break;
                     }
                     break;
                 case ExportModel::FieldTypeTable:
                     // Parse table checkboxes
                     $table = array();
                     foreach ($data as $row) {
                         // Keep track of column #
                         $i = 1;
                         // Loop through columns
                         foreach ($row as $column => $value) {
                             // Get column
                             $column = isset($field->settings['columns'][$column]) ? $field->settings['columns'][$column] : (isset($field->settings['columns']['col' . $i]) ? $field->settings['columns']['col' . $i] : array('type' => 'dummy'));
                             // Keep track of column #
                             $i++;
                             // Parse
                             $table[] = $column['type'] == 'checkbox' ? $value == 1 ? Craft::t('Yes') : Craft::t('No') : $value;
                         }
                     }
                     // Return parsed data as array
                     $data = $table;
                     break;
                 case ExportModel::FieldTypeRichText:
                 case ExportModel::FieldTypeDate:
                 case ExportModel::FieldTypeRadioButtons:
                 case ExportModel::FieldTypeDropdown:
                     // Resolve to string
                     $data = (string) $data;
                     break;
                 case ExportModel::FieldTypeCheckboxes:
                 case ExportModel::FieldTypeMultiSelect:
                     // Parse multi select values
                     $multi = array();
                     foreach ($data as $row) {
                         $multi[] = $row->value;
                     }
                     // Return parsed data as array
                     $data = $multi;
                     break;
             }
         }
         // Get other operations
         craft()->plugins->call('registerExportOperation', array(&$data, $handle));
     } else {
         // Don't return null, return empty
         $data = '';
     }
     // If it's an object or an array, make it a string
     if (is_array($data)) {
         $data = StringHelper::arrayToString(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::flattenArray($data)), ', ');
     }
     // If it's an object, make it a string
     if (is_object($data)) {
         $data = StringHelper::arrayToString(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::flattenArray(get_object_vars($data))), ', ');
     }
     return $data;
 }
 protected function parseFieldData($field, $data)
 {
     if (!is_null($data)) {
         if (!is_null($field)) {
             switch ($field->type) {
                 case ExportModel::FieldTypeEntries:
                 case ExportModel::FieldTypeCategories:
                 case ExportModel::FieldTypeAssets:
                 case ExportModel::FieldTypeUsers:
                     $data = $data instanceof ElementCriteriaModel ? implode(', ', $data->find()) : $data;
                     break;
                 case ExportModel::FieldTypeLightswitch:
                     switch ($data) {
                         case '0':
                             $data = Craft::t('No');
                             break;
                         case '1':
                             $data = Craft::t('Yes');
                             break;
                     }
                     break;
                 case ExportModel::FieldTypeTable:
                     $table = array();
                     foreach ($data as $row) {
                         $i = 1;
                         foreach ($row as $column => $value) {
                             $column = isset($field->settings['columns'][$column]) ? $field->settings['columns'][$column] : (isset($field->settings['columns']['col' . $i]) ? $field->settings['columns']['col' . $i] : array('type' => 'dummy'));
                             $i++;
                             $table[] = $column['type'] == 'checkbox' ? $value == 1 ? Craft::t('Yes') : Craft::t('No') : $value;
                         }
                     }
                     $data = $table;
                     break;
                 case ExportModel::FieldTypeRichText:
                 case ExportModel::FieldTypeDate:
                 case ExportModel::FieldTypeRadioButtons:
                 case ExportModel::FieldTypeDropdown:
                     $data = (string) $data;
                     break;
                 case ExportModel::FieldTypeCheckboxes:
                 case ExportModel::FieldTypeMultiSelect:
                     $multi = array();
                     foreach ($data as $row) {
                         $multi[] = $row->value;
                     }
                     $data = $multi;
                     break;
             }
         }
     } else {
         $data = '';
     }
     if (is_array($data)) {
         $data = StringHelper::arrayToString(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::flattenArray($data)), ', ');
     }
     if (is_object($data)) {
         $data = StringHelper::arrayToString(ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::flattenArray(get_object_vars($data))), ', ');
     }
     return $data;
 }
Exemplo n.º 9
0
 /**
  * Ensures that a valid list of parseable headers is returned
  *
  * @param string $headerString
  *
  * @return array
  */
 public function getHeadersToParse($headerString = '')
 {
     $allowedHeaders = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
     $headers = ArrayHelper::filterEmptyStringsFromArray(ArrayHelper::stringToArray($headerString));
     if (count($headers)) {
         foreach ($headers as $key => $header) {
             $header = strtolower($header);
             if (!in_array($header, $allowedHeaders)) {
                 unset($headers[$key]);
             }
         }
     }
     return $headers;
 }
Exemplo n.º 10
0
 /**
  * Splits a string into an array of the words in the string.
  *
  * @param string $string The string
  *
  * @return string[]
  */
 public static function splitOnWords($string)
 {
     // Split on anything that is not alphanumeric, or a period, underscore, or hyphen.
     preg_match_all('/[\\p{L}\\p{N}\\._-]+/u', $string, $matches);
     return ArrayHelper::filterEmptyStringsFromArray($matches[0]);
 }