Commit 56abbf2a36124f86763773de1fc2236476c81cf1

Authored by Anastasia
1 parent bc1ea161

event, blog, social

Showing 2 changed files with 61 additions and 2 deletions   Show diff stats
behaviors/ManyToManyBehavior.php 0 → 100755
  1 +<?php
  2 +
  3 + namespace artweb\artbox\behaviors;
  4 +
  5 + use yii\base\Behavior;
  6 + use yii\db\ActiveRecord;
  7 +
  8 + /**
  9 + * Class ManyToManyBehavior
  10 + *
  11 + * @package artbox\catalog\behaviors
  12 + */
  13 + class ManyToManyBehavior extends Behavior
  14 + {
  15 + /**
  16 + * @param string $name
  17 + * @param ActiveRecord[] $models
  18 + * @param array $extraColumns
  19 + */
  20 + public function linkMany(string $name, array $models, array $extraColumns = [])
  21 + {
  22 + /**
  23 + * @var ActiveRecord $owner
  24 + */
  25 + $owner = $this->owner;
  26 +
  27 + $owner->unlinkAll($name, true);
  28 +
  29 + foreach ($models as $model) {
  30 + $owner->link($name, $model, $extraColumns);
  31 + }
  32 + }
  33 + }
0 34 \ No newline at end of file
... ...
models/Customer.php
... ... @@ -97,6 +97,7 @@
97 97 ],
98 98 'unique'
99 99 ],
  100 + [['social_type', 'social_id'], 'safe'],
100 101 [
101 102 [
102 103 'gender',
... ... @@ -171,6 +172,9 @@
171 172 ]
172 173 );
173 174 }
  175 + public function loginSocial(){
  176 + return Yii::$app->user->login($this, 3600 * 24 * 30);
  177 + }
174 178  
175 179 /**
176 180 * Get full name
... ... @@ -215,6 +219,28 @@
215 219 {
216 220 return false;
217 221 }
218   -
219   -
  222 +
  223 + public static function Social($user, $type)
  224 + {
  225 + $customer = static::findOne(
  226 + [
  227 + 'status' => self::STATUS_ACTIVE,
  228 + 'email'=> $user['email'],
  229 + 'social_type' => $type
  230 + ]
  231 + );
  232 + if ($customer == null){
  233 + $customer = new Customer();
  234 + $customer->email = $user['email'];
  235 + $customer->username = (isset($user['name'])) ? $user['name'] : $user['email'];
  236 + $customer->social_id = $user['id'];
  237 + $customer->social_type = $type;
  238 + $customer->setPassword($user['id']);
  239 + $customer->generateAuthKey();
  240 + $customer->status = self::STATUS_ACTIVE;
  241 + return $customer->save() ? $customer : null;
  242 + }else{
  243 + return $customer;
  244 + }
  245 + }
220 246 }
... ...