public static function scanTags($inputTags) { if (!is_array($inputTags)) { return false; } $inputTags = array_unique($inputTags); $result = []; foreach ($inputTags as $v) { if (empty($v)) { continue; } $slug = StringHelper::generateCleanStr($v); if (!$slug) { continue; } $model = static::find()->andWhere(['name' => $slug])->one(); if ($model) { $result[] = $model->mid; } else { $tag = new self(); $tag->name = $tag->slug = $slug; if ($tag->insert(false)) { $result[] = $tag->mid; } } } return $result; }
/** * 检测生成缩略名 * @param $attribute * @param $params */ public function checkSlugName($attribute, $params) { if (!$this->hasErrors()) { $name = StringHelper::generateCleanStr($this->{$attribute}); if ($attribute == 'name' && empty($name) || $attribute == 'slug' && !empty($this->slug) && empty($name)) { $this->addError($attribute, $this->getAttributeLabel($attribute) . '全部为非法字符,无法转换'); } if ($attribute == 'slug' && empty($this->slug)) { $this->{$attribute} = $this->name; } else { $this->{$attribute} = $name; } } }
public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($insert) { $this->created = time(); $this->slug = \common\helpers\StringHelper::generateCleanStr($this->title) . '-' . sha1(microtime(true)); $this->title = Html::encode($this->title); $this->text = json_encode($this->text); } return true; } else { return false; } }
/** * @inheritdoc */ public function rules() { return [[['title', 'slug'], 'string', 'max' => 200], [['slug'], 'filter', 'filter' => function ($value) { return StringHelper::generateCleanStr($value); }], [['slug'], 'unique'], [['title'], 'default', 'value' => function ($model, $attribute) { return '未命名文档'; }], [['title'], 'filter', 'filter' => function ($value) { return Html::encode($value); }], [['order', 'allowComment', 'allowPing', 'allowFeed'], 'filter', 'filter' => function ($value) { return intval($value); }], [['status'], 'filter', 'filter' => function ($value) { return in_array($value, [self::STATUS_PUBLISH, self::STATUS_HIDDEN]) ? $value : self::STATUS_PUBLISH; }], [['created'], 'filter', 'filter' => function ($value) { if ($value == '') { return time(); } else { return strtotime($value); } }], [['text'], 'safe']]; }