Пример #1
0
 /**
  * Parses a given text for mentioned users and creates an mentioning for them.
  * 
  * @param HActiveRecordContent|HActiveRecordContentAddon $record
  * @param string $text
  */
 public static function parse($record, $text)
 {
     if ($record instanceof HActiveRecordContent || $record instanceof HActiveRecordContentAddon) {
         preg_replace_callback('@\\@\\-u([\\w\\-]*?)($|\\s|\\.)@', function ($hit) use(&$record) {
             $user = User::model()->findByAttributes(array('guid' => $hit[1]));
             if ($user !== null) {
                 // Check the user was already mentioned (e.g. edit)
                 $mention = UserMentioning::model()->findByAttributes(array('object_model' => get_class($record), 'object_id' => $record->getPrimaryKey(), 'user_id' => $user->id));
                 if ($mention === null) {
                     $mention = new UserMentioning();
                     $mention->object_model = get_class($record);
                     $mention->object_id = $record->getPrimaryKey();
                     $mention->user_id = $user->id;
                     $mention->save();
                     $mention->setUnderlyingObject($record);
                 }
             }
         }, $text);
     } else {
         throw new Exception("Mentioning can only used in HActiveRecordContent or HActiveRecordContentAddon objects!");
     }
 }