예제 #1
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 = '';
     }
 }
 /**
  * Parse entry fields.
  *
  * @param EntryModel $entry
  * @param bool       $empty
  *
  * @return array
  */
 public function fields(EntryModel $entry, $empty = false)
 {
     // Always save id and title
     $fields = array('id' => array('label' => Craft::t('ID'), 'value' => $entry->id), 'title' => array('label' => Craft::t('Title'), 'value' => (string) $entry->getTitle()), 'section' => array('label' => Craft::t('Section'), 'value' => (string) $entry->getSection()));
     // Get element type
     $elementType = craft()->elements->getElementType(ElementType::Entry);
     // Get nice attributes
     $availableAttributes = $elementType->defineAvailableTableAttributes();
     // Make 'em fit
     $attributes = array();
     foreach ($availableAttributes as $key => $result) {
         $attributes[$key] = $result['label'];
     }
     // Get static "fields"
     foreach ($entry->getAttributes() as $handle => $value) {
         // Only show nice attributes
         if (array_key_exists($handle, $attributes)) {
             $fields[$handle] = array('label' => $attributes[$handle], 'value' => StringHelper::arrayToString(is_array($value) ? array_filter(ArrayHelper::flattenArray($value), 'strlen') : $value, ', '));
         }
     }
     // Get fieldlayout
     $entrytype = $entry->getType();
     if ($entrytype) {
         $tabs = craft()->fields->getLayoutById($entrytype->fieldLayoutId)->getTabs();
         foreach ($tabs as $tab) {
             foreach ($tab->getFields() as $field) {
                 // Get field values
                 $field = $field->getField();
                 $handle = $field->handle;
                 $label = $field->name;
                 $value = $empty ? '' : craft()->auditLog->parseFieldData($handle, $entry->{$handle});
                 // Set on fields
                 $fields[$handle] = array('label' => $label, 'value' => $value);
             }
         }
     }
     // Return
     return $fields;
 }