コード例 #1
0
 /**
  * Add activity metadata 
  * 
  * @param User $user
  * @param Activity $activity
  * @param array $data    key-value pair data to store
  * @param array $exclude When given exclude keys to be stored in the database
  */
 public static function addUserActivity(User $user, Activity $activity, array $data, array $exclude = [])
 {
     $rows = [];
     $exclude = array_map('strtolower', $exclude);
     // Create a session_id.
     // Session_id is use for easily identify groups of metadata
     $hashids = new Hashids('dma.activity.metadata', 6);
     $user_id = $user->getKey();
     $activity_id = $activity->getKey();
     // Add unixtime and microseconds to avoid session_id collisions
     $micro = microtime(true);
     $unixtime = floor($micro);
     $milseconds = floor(($micro - $unixtime) * pow(10, 8));
     // Create session_id
     $session_id = $hashids->encode($user_id, $activity_id, $unixtime, $milseconds);
     // Current date and time
     $now = date('Y-m-d H:i:s');
     foreach ($data as $key => $value) {
         $key = strtolower($key);
         if (!in_array($key, $exclude)) {
             $row = ['session_id' => $session_id, 'user_id' => $user_id, 'activity_id' => $activity_id, 'key' => $key, 'value' => $value, 'created_at' => $now, 'updated_at' => $now];
             $rows[] = $row;
         }
     }
     if (count($row) > 0) {
         static::insert($rows);
     }
 }
コード例 #2
0
ファイル: Usermeta.php プロジェクト: janusnic/OctoberFriends
 /**
  * Automatically creates a metadata entry for a user if not one already.
  * @param  RainLab\User\Models\User $user
  * @return DMA\Friends\Models\Usermeta
  */
 public static function getFromUser($user = null)
 {
     if (!$user) {
         return null;
     }
     if (!$user->metadata) {
         $meta = new static();
         User::find($user->getKey())->metadata()->save($meta);
         $user = User::find($user->getKey());
     }
     return $user->metadata;
 }
コード例 #3
0
 /**
  * Upload Avatar from Base64 encoded image
  * 
  * @param \RainLab\User\Models\User $user
  * @param string $source
  * string contend of an image on Base64 enconding 
  */
 public static function uploadAvatarFromString($user, $source)
 {
     $dst = '/tmp/avatar_' . $user->getKey() . '_' . uniqid();
     FileHelper::put($dst, base64_decode($source));
     $validImage = true;
     try {
         // Validated is a JPG or PNG
         $imageType = exif_imagetype($dst);
         $validImage = in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF]);
         // Validated is not bigger that xx by xx
         if ($validImage) {
             // Test if image is corrupted if OctoberCMS Resizer can open it
             // is more likely the image is ok
             Resizer::open($dst);
             // Test image dimensions
             list($width, $height, $type, $attr) = getimagesize($dst);
             $validImage = $width <= 400 && $height <= 400;
         }
         // Add right file extension to the upload file
         if ($validImage) {
             // Save image with correct image extension
             $extension = [IMAGETYPE_JPEG => 'jpg', IMAGETYPE_PNG => 'png', IMAGETYPE_GIF => 'gif'][$imageType];
             $newDst = $dst . '.' . $extension;
             rename($dst, $newDst);
             $dst = $newDst;
         }
     } catch (\Exception $e) {
         $validImage = false;
     }
     if (!$validImage) {
         throw new \Exception('Must be a valid JPG, GIF or PNG. And not bigger that 400x400 pixels.');
     }
     $file = new File();
     $file->data = $dst;
     $file->is_public = true;
     $file->save();
     if ($file) {
         $user->avatar()->add($file);
     }
 }
コード例 #4
0
 /**
  * Query scope to filter activity metadata by the given key and value
  * @param mixed $query
  * @param RainLab\User\Models\User $user
  * @param DMA\Friends\Models\Activity $activity
  * @param string $key
  * @param string $value
  */
 public function scopeHasMetadataValue($query, $user, $activity, $key, $value)
 {
     return self::where('user_id', $user->getKey())->where('activity_id', $activity->getKey())->where('key', $key)->where('value', $value);
 }