/** * Create new school * * @param Request $r * @return array * @throws InvalidDatabaseOperationException * @throws InvalidParameterException */ public static function apiCreate(Request $r) { self::authenticateRequest($r); Validators::isStringNonEmpty($r["name"], "name"); Validators::isNumber($r["state_id"], "state_id", false); if (!is_null($r["state_id"])) { try { $r["state"] = StatesDAO::getByPK($r["state_id"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r["state"])) { throw new InvalidParameterException("parameterNotFound", "state"); } } // Create school object $school = new Schools(array("name" => $r["name"], "state_id" => $r["state_id"])); $school_id = 0; try { $existing = SchoolsDAO::findByName($r["name"]); if (count($existing) > 0) { $school_id = $existing[0]->getSchoolId(); } else { // Save in db SchoolsDAO::save($school); $school_id = $school->getSchoolId(); } } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } return array("status" => "ok", "school_id" => $school_id); }
/** * Update user profile * * @param Request $r * @return array * @throws InvalidDatabaseOperationException * @throws InvalidParameterException */ public static function apiUpdate(Request $r) { self::authenticateRequest($r); Validators::isStringNonEmpty($r['name'], 'name', false); Validators::isStringNonEmpty($r['country_id'], 'country_id', false); if (!is_null($r['country_id'])) { try { $r['country'] = CountriesDAO::getByPK($r['country_id']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } } if ($r['state_id'] === 'null') { $r['state_id'] = null; } Validators::isNumber($r['state_id'], 'state_id', false); if (!is_null($r['state_id'])) { try { $r['state'] = StatesDAO::getByPK($r['state_id']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } } if (!is_null($r['school_id'])) { if (is_numeric($r['school_id'])) { try { $r['school'] = SchoolsDAO::getByPK($r['school_id']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r['school'])) { throw new InvalidParameterException('parameterInvalid', 'school'); } } elseif (empty($r['school_name'])) { $r['school_id'] = null; } else { try { $schoolR = new Request(array('name' => $r['school_name'], 'state_id' => $r['state_id'], 'auth_token' => $r['auth_token'])); $response = SchoolController::apiCreate($schoolR); $r['school_id'] = $response['school_id']; } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } } } Validators::isStringNonEmpty($r['scholar_degree'], 'scholar_degree', false); if (!is_null($r['graduation_date'])) { if (is_numeric($r['graduation_date'])) { $r['graduation_date'] = (int) $r['graduation_date']; } else { Validators::isDate($r['graduation_date'], 'graduation_date', false); $r['graduation_date'] = strtotime($r['graduation_date']); } } if (!is_null($r['birth_date'])) { if (is_numeric($r['birth_date'])) { $r['birth_date'] = (int) $r['birth_date']; } else { Validators::isDate($r['birth_date'], 'birth_date', false); $r['birth_date'] = strtotime($r['birth_date']); } } if (!is_null($r['locale'])) { // find language in Language $query = LanguagesDAO::search(new Languages(array('name' => $r['locale']))); if (sizeof($query) == 1) { $r['current_user']->setLanguageId($query[0]->getLanguageId()); } } $valueProperties = array('name', 'country_id', 'state_id', 'scholar_degree', 'school_id', 'graduation_date' => array('transform' => function ($value) { return gmdate('Y-m-d', $value); }), 'birth_date' => array('transform' => function ($value) { return gmdate('Y-m-d', $value); })); self::updateValueProperties($r, $r['current_user'], $valueProperties); try { UsersDAO::save($r['current_user']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } // Expire profile cache Cache::deleteFromCache(Cache::USER_PROFILE, $r['current_user']->getUsername()); $sessionController = new SessionController(); $sessionController->InvalidateCache(); return array('status' => 'ok'); }