/** * Store a newly created resource in storage. * POST /actions * * @return Response */ public function store() { $response = new stdClass(); $statusCode = 201; $in = Input::only('tid', 'created_at'); $rules = array('tid' => 'required | integer'); $vd = Validator::make($in, $rules); if ($vd->fails()) { $errs = $vd->messages(); $statusCode = 400; $response = $errs->all(); } else { $authId = Auth::user()->id; // 先檢查是否在 Target 有登記 $uid = Target::where(array('id' => $in['tid'], 'uid' => $authId))->pluck('uid'); // 取得使用者的教會 $cid = UserChurch::where(array('uid' => $authId))->pluck('cid'); if ($uid == $authId) { $in['uid'] = Auth::user()->id; $in['cid'] = $cid; $action = Action::create($in); if ($in['created_at']) { $in['created_at'] = Carbon::createFromTimeStamp($in['created_at'], 'Asia/Taipei')->toDateTimeString(); $action->setCreatedAt($in['created_at']); $action->save(); } } else { $statusCode = 403; } } return Response::json($response, $statusCode); }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $response = new stdClass(); $statusCode = 201; $in = Input::only('name', 'lat', 'lng', 'cid'); $rules = array('name' => 'required | alpha_spaces | unique:churches', 'lat' => 'numeric', 'lng' => 'numeric', 'cid' => 'integer'); $vd = Validator::make($in, $rules); if ($vd->fails()) { $errs = $vd->messages(); $statusCode = 400; $response = $errs->all(); if ($errs->has('name')) { // 教會已有建檔,嘗試建立教會與使用者之間的關係 $cid = Church::where('name', $in['name'])->pluck('id'); $statusCode = 201; // 先檢查是否已經加入教會 $uc = UserChurch::where('uid', Auth::user()->id)->first(); if ($uc && $uc->uid === Auth::user()->id) { $uc->cid = $cid; $uc->save(); } else { $relation = array('cid' => $cid, 'uid' => Auth::user()->id); UserChurch::firstOrCreate($relation); } $response = new stdClass(); return Response::json($response, $statusCode); } } else { $in['qlink'] = XUtil::makeQlink($in['name']); $church = Church::create($in); // 先檢查是否已經加入教會 $uc = UserChurch::where('uid', Auth::user()->id)->first(); if ($uc && $uc->uid === Auth::user()->id) { $uc->cid = $church->id; $uc->save(); } else { // 建立教會與使用者之間的關係 $relation = array('cid' => $church->id, 'uid' => Auth::user()->id); UserChurch::firstOrCreate($relation); } $response = new stdClass(); } return Response::json($response, $statusCode); }
public function getStatMonthByWeeks() { $in = Input::only('qlink'); $out = array(); $rules = array('qlink' => 'required | alpha_num'); $vd = Validator::make($in, $rules); if ($vd->fails()) { return; } $cid = Church::where('qlink', $in['qlink'])->pluck('id'); $church = Church::where('qlink', $in['qlink'])->first(); if ($cid) { $actions = array_fill(1, 5, ''); foreach (Action::where('cid', $cid)->groupMonthByWeeks() as $k => $v) { foreach ($v as $key => $value) { $actions[$k]++; } } $users = array_fill(1, 5, ''); foreach (UserChurch::where('cid', $cid)->groupMonthByWeeks() as $k => $v) { foreach ($v as $key => $value) { $users[$k]++; } } $out['statistic_actions_month_by_weeks'] = array_values($actions); $out['statistic_users_month_by_weeks'] = array_values($users); } return Response::json($out); }