コード例 #1
0
 /**
  * @inheritDoc BaseFieldType::validate()
  *
  * @param mixed $value
  *
  * @return true|string|array
  */
 public function validate($value)
 {
     $settings = $this->getSettings();
     // This wasn't always a setting.
     $columnType = !$settings->getAttribute('columnType') ? ColumnType::Text : $settings->getAttribute('columnType');
     $postContentSize = strlen($value);
     $maxDbColumnSize = DbHelper::getTextualColumnStorageCapacity($columnType);
     // Give ourselves 10% wiggle room.
     $maxDbColumnSize = ceil($maxDbColumnSize * 0.9);
     if ($postContentSize > $maxDbColumnSize) {
         // Give ourselves 10% wiggle room.
         $maxDbColumnSize = ceil($maxDbColumnSize * 0.9);
         if ($postContentSize > $maxDbColumnSize) {
             return Craft::t('{attribute} is too long.', array('attribute' => Craft::t($this->model->name)));
         }
     }
     return true;
 }
コード例 #2
0
 /**
  * Indexes keywords for a specific element attribute/field.
  *
  * @param int         $elementId
  * @param string      $attribute
  * @param string      $fieldId
  * @param string|null $localeId
  * @param string      $dirtyKeywords
  *
  * @return null
  */
 private function _indexElementKeywords($elementId, $attribute, $fieldId, $localeId, $dirtyKeywords)
 {
     $attribute = StringHelper::toLowerCase($attribute);
     if (!$localeId) {
         $localeId = craft()->i18n->getPrimarySiteLocaleId();
     }
     // Clean 'em up
     $cleanKeywords = StringHelper::normalizeKeywords($dirtyKeywords);
     // Save 'em
     $keyColumns = array('elementId' => $elementId, 'attribute' => $attribute, 'fieldId' => $fieldId, 'locale' => $localeId);
     if ($cleanKeywords !== null && $cleanKeywords !== false && $cleanKeywords !== '') {
         // Add padding around keywords
         $cleanKeywords = ' ' . $cleanKeywords . ' ';
     }
     $cleanKeywordsLength = strlen($cleanKeywords);
     $maxDbColumnSize = DbHelper::getTextualColumnStorageCapacity(ColumnType::Text);
     // Give ourselves 10% wiggle room.
     $maxDbColumnSize = ceil($maxDbColumnSize * 0.9);
     if ($cleanKeywordsLength > $maxDbColumnSize) {
         // Time to truncate.
         $cleanKeywords = mb_strcut($cleanKeywords, 0, $maxDbColumnSize);
         // Make sure we don't cut off a word in the middle.
         if ($cleanKeywords[mb_strlen($cleanKeywords) - 1] !== ' ') {
             $position = mb_strrpos($cleanKeywords, ' ');
             if ($position) {
                 $cleanKeywords = mb_substr($cleanKeywords, 0, $position + 1);
             }
         }
     }
     // Insert/update the row in searchindex
     craft()->db->createCommand()->insertOrUpdate('searchindex', $keyColumns, array('keywords' => $cleanKeywords), false);
 }