/** * @author LAHAXE Arnaud * * @param \Illuminate\Database\Eloquent\Model $model * @param \App\Libraries\Repository $repository * * @return mixed */ public function apply(Builder $query, Model $model, Repository $repository) { return $query->where($model->getUserIdFields(), $this->user->getKey()); }
/** * Check current superuser password * * @return bool */ protected function checkCurrentPassword() { $credentials = [$this->superuser->getKeyName() => $this->superuser->getKey(), 'password' => $this->secret('Enter superuser CURRENT password: ')]; return auth()->validate($credentials); }
/** * Tally time by month with no gaps * * @param Project $project Project model instance. * @param User $user User model instance. * @param integer $monthCount How many months to go back from present. * * @return array */ public static function forProjectAndUserByMonth(Project $project, User $user, $monthCount = 6) { if (empty($monthCount)) { $minStart = self::where('project_id', $project->getKey())->where('user_id', $user->getKey())->min('start'); if (empty($minStart)) { return []; } $monthCount = Carbon::now()->diffInMonths(Carbon::parse($minStart)); } $query = "\n WITH RECURSIVE\n tally_range(dt) AS (\n SELECT date('now', 'start of month')\n UNION ALL\n SELECT date(dt, '-1 month')\n FROM tally_range\n WHERE dt > date('now', 'start of month', :range)\n ),\n tallies(dt, minutes) AS (\n SELECT date(start, 'start of month') AS dt, SUM(minutes)\n FROM times\n WHERE project_id=:projectId\n AND user_id=:userId\n GROUP BY dt\n )\n SELECT * FROM tally_range NATURAL LEFT OUTER JOIN tallies\n "; $result = DB::select($query, ['range' => abs($monthCount) * -1 . ' months', 'projectId' => $project->getKey(), 'userId' => $user->getKey()]); $result = collect($result)->reduce(function ($accumulator, $item) { $accumulator[$item->dt] = (int) $item->minutes; return $accumulator; }, []); return $result; }