public function get_teams($team_id, $joint_id = 0) { // if it's a joint, let's deal it as a joing if ($joint_id > 0) { // get all the joint entries so we have all the teams $joint = new Joint(); $joint->where("joint_id", $joint_id)->get(); // not an existing joint? if ($joint->result_count() < 1) { log_message('error', 'get_teams: joint -> joint not found'); return false; } // result array $teamarray = array(); foreach ($joint->all as $key => $join) { if (!($team = $this->get_cached($join->team_id))) { $team = new Team(); $team->where('id', $join->team_id); $team->get(); } $teamarray[] = $team->get_clone(); } if (empty($teamarray)) { log_message('error', 'get_teams: joint -> no teams found'); return false; } return $teamarray; } // if we're here, it means it's a simple team if (!($team = $this->get_cached($team_id))) { $team = new Team($team_id); } return array($team); }
/** * Handles both creating of new chapters in the database and editing old ones. * It determines if it should update or not by checking if $this->id has * been set. It can get the values from both the $data array and direct * variable assignation. Be aware that array > variables. The latter ones * will be overwritten. Particularly, the variables that the user isn't * allowed to set personally are unset and reset with the automated values. * It's quite safe to throw stuff at it. * * @author Woxxy * @param array $data contains the minimal data * @return object the comic the chapter derives from. */ public function update_chapter_db($data = array()) { // Check if we're updating or creating a new chapter by looking at $data["id"]. // False is returned if the chapter ID was not found. if (isset($data["id"]) && $data['id'] != "") { $this->where("id", $data["id"])->get(); if ($this->result_count() == 0) { set_notice('error', _('The chapter you tried to edit doesn\'t exist.')); log_message('error', 'update_chapter_db: failed to find requested id'); return false; } // Save the stub in case it gets changed (different chapter number/name etc.) // Stub is always automatized. $old_stub = $this->stub; } else { // if we're here, it means that we're creating a new chapter // Set the creator name if it's a new chapter. if (!isset($this->comic_id)) { set_notice('error', 'You didn\'t select a comic to refer to.'); log_message('error', 'update_chapter_db: comic_id was not set'); return false; } // Check that the related comic is defined, and exists. $comic = new Comic($this->comic_id); if ($comic->result_count() == 0) { set_notice('error', _('The comic you were referring to doesn\'t exist.')); log_message('error', 'update_chapter_db: comic_id does not exist in comic database'); return false; } // Set the creator. This happens only on new chapter creation. $this->creator = $this->logged_id(); } // Always set the editor $this->editor = $this->logged_id(); // Unset the sensible variables. // Not even admins should touch these, for database stability. unset($data["creator"]); unset($data["editor"]); unset($data["uniqid"]); unset($data["stub"]); unset($data["team_id"]); unset($data["joint_id"]); // Loop over the array and assign values to the variables. foreach ($data as $key => $value) { $this->$key = $value; } // Double check that we have all the necessary automated variables if (!isset($this->uniqid)) $this->uniqid = uniqid(); if (!isset($this->stub)) $this->stub = $this->stub(); // This is necessary to make the checkbox work. /** * @todo make the checkbox work consistently across the whole framework */ if (!isset($data['hidden']) || $data['hidden'] != 1) $this->hidden = 0; // Prepare a new stub. $this->stub = $this->chapter . '_' . $this->subchapter . '_' . $this->name; // stub() is also able to restub the $this->stub. Already stubbed values won't change. $this->stub = $this->stub(); // If the new stub is different from the old one (if the chapter was // already existing), rename the folder. if (isset($old_stub) && $old_stub != $this->stub) { $this->get_comic(); $dir_old = "content/comics/" . $this->comic->directory() . "/" . $old_stub . "_" . $this->uniqid; $dir_new = "content/comics/" . $this->comic->directory() . "/" . $this->stub . "_" . $this->uniqid; rename($dir_old, $dir_new); } // $data['team'] must be an array of team NAMES if (isset($data['team'])) { // Remove the empty values in the array of team names. // It happens that the POST contains extra empty values. if (is_array($data['team'])) { foreach ($data['team'] as $key => $value) { if ($value == "") { unset($data['team'][$key]); } } sort($data["team"]); } // In case there's more than a team name in array, get the joint_id. // The joint model is able to create new joints on the fly, do not worry. // Worry rather that the team names must exist. if (count($data['team']) > 1) { // Set team_id to 0 since it's a joint. $this->team_id = 0; $joint = new Joint(); // If the search returns false, something went wrong. // GUI errors are inside the function. if (!$this->joint_id = $joint->add_joint_via_name($data['team'])) { log_message('error', 'update_chapter_db: error with joint_id'); return false; } } // In case there's only one team in the array, find the team. // return false in case one of the names doesn't exist. else if (count($data['team']) == 1) { // Set joint_id to 0 since it's a single team $this->joint_id = 0; $team = new Team(); $team->where("name", $data['team'][0])->get(); if ($team->result_count() == 0) { set_notice('error', _('The team you were referring this chapter to doesn\'t exist.')); log_message('error', 'update_chapter_db: team_id does not exist in team database'); return false; } $this->team_id = $team->id; } else { set_notice('error', _('You must select at least one team for this chapter')); log_message('error', 'update_chapter_db: team_id not defined'); return false; } } else if (!isset($this->team)) { // If we're here it means that this is a new chapter with no teams assigned. // The system doesn't allow chapters without related teams. It must be at // least "anonymous" or a default anonymous team. set_notice('error', _('You haven\'t selected any team in relation to this chapter.')); log_message('error', 'update_chapter_db: team_id does not defined'); return false; } // Save with validation. Push false if fail, true if good. $success = $this->save(); if (!$success) { if (!$this->valid) { log_message('error', $this->error->string); set_notice('error', _('Check that you have inputted all the required fields.')); log_message('error', 'update_chapter_db: failed validation'); } else { set_notice('error', _('Failed to save to database for unknown reasons.')); log_message('error', 'update_chapter_db: failed to save'); } return false; } else { // Here we go! return true; } }
public function remove_team_from_all($team_id) { $joints = new Joint(); $joints->where('team_id', $team_id)->get(); if (!$joints->delete_all()) { set_notice('error', _('Couldn\'t remove the team from all the joints.')); log_message('error', 'remove_team_from_all (joint.php): removing failed'); return false; } }
function joint_get() { if (!$this->get('id') || !is_numeric($this->get('id'))) { $this->response(NULL, 400); } if (!$this->get('page') || !is_numeric($this->get('page')) || $this->get('page') < 1) $page = 1; else $page = (int) $this->get('page'); $page = ($page * 100) - 100; $joint = new Joint(); $joint->where('joint_id', $this->get('id'))->limit(1)->get(); if ($joint->result_count() == 1) { $team = new Team(); $teams = $team->get_teams(0, $this->get('id')); $result = array(); foreach ($teams as $item) { $result['teams'][] = $item->to_array(); } $chapters = new Chapter(); $chapters->where('joint_id', $joint->joint_id); $chapters->limit(100, $page)->get(); $chapters->get_comic(); $result['chapters'] = array(); foreach ($chapters->all as $key => $chapter) { $result['chapters'][$key]['comic'] = $chapter->comic->to_array(); $result['chapters'][$key]['chapter'] = $chapter->to_array(); $result['chapters'][$key]['teams'] = $result['teams']; } $this->response($result, 200); // 200 being the HTTP response code } else { $this->response(array('error' => _('Team could not be found')), 404); } }
/** * Returns chapters per page by joint ID * Also returns the teams * * This is not a method light enough to lookup teams. use api/members/joint for that * * Available filters: id (required), page, per_page (default:30, max:100), orderby * * @author Woxxy */ function joint_get() { // check that the id is at least a valid number $this->_check_id(); // grab by joint_id, id for joints means nothing much $joint = new Joint(); $joint->where('joint_id', $this->get('id'))->limit(1)->get(); if ($joint->result_count() == 1) { // good old get_teams() will give us all Team objects in an array $team = new Team(); $teams = $team->get_teams(0, $this->get('id')); // $teams is a normal array, so we have to do a loop $result = array(); foreach ($teams as $item) { $result['teams'][] = $item->to_array(); } // grab all the chapters from the same joint $chapters = new Chapter(); $chapters->where('joint_id', $joint->joint_id); // apply the limit and orderby filters $this->_orderby($chapters); $this->_page_to_offset($chapters); $chapters->get(); $chapters->get_comic(); // let's put the chapters in a nice [comic][chapter][teams] list $result['chapters'] = array(); foreach ($chapters->all as $key => $chapter) { $result['chapters'][$key]['comic'] = $chapter->comic->to_array(); $result['chapters'][$key]['chapter'] = $chapter->to_array(); $result['chapters'][$key]['teams'] = $result['teams']; } // all good $this->response($result, 200); // 200 being the HTTP response code } else { // nothing for this joint or page $this->response(array('error' => _('Team could not be found')), 404); } }
public function get_teams($team_id, $joint_id = 0) { if ($joint_id > 0) { $joint = new Joint(); $joint->where("joint_id", $joint_id)->get(); if ($joint->result_count() < 1) { log_message('error', 'get_teams: joint -> joint not found'); return false; } $teamarray = array(); $team = new Team(); foreach ($joint->all as $key => $join) { $team->where('id', $join->team_id); $team->get(); $teamarray[] = $team->get_clone(); } if ($team->result_count() < 1) { log_message('error', 'get_teams: joint -> no teams found'); return false; } return $teamarray; } $team = new Team($team_id); return array($team); }