Newer
Older
flow-task / server / App / Controller / GroupsController.php
<?php

namespace App\Controller;

use \App\Models\Groups;
use \App\Entity\Session;
use \App\Entity\Group;

class GroupsController {
	public function create() {
		list($title) = get_expected_vars($_GET, ["title"]);
		$title = sanitize_input($title);

		$session = get_current_session();

		$groups = new Groups();
		$group = $groups -> create_group(
			$session -> user() -> get_id(), 
			$title,
			"user"
		);

		if(!$group) {
			return error_response("create_group", "Something wrong");
		}

		return success_response([
			"group_id" => $group -> get_id(),
		]);
	}

	public function get_list() {
		$session = get_current_session();

		$groups = $session -> user() -> get_groups();

		$groups = array_map(function($group){
			return $group -> to_array();
		}, $groups);

		return success_response([
			"groups" => $groups
		]);
	}

	public function remove() {

	}

	public function change_position() {
		$session = get_current_session();
		list($group_id, $position) = get_expected_vars($_GET, ["group_id", "position"]);

		$position = intval($position);
		$group_id = intval($group_id);

		$groups = new Groups();
		if(!$groups -> change_group_position($group_id, $position)) {
			return error_response("group_change_position", "Something wrong");
		}

		return success_response();
	}	

	public function change_title() {
		$session = get_current_session();

		list($group_id, $title) = get_expected_vars($_GET, ["group_id", "title"]);

		$group_id = intval($group_id);
		$new_title = sanitize_input($title);

		$group = new Group();
		if(!$group -> init_by_id($group_id)) {
			return error_response("change_group_title", "Wrong group_id");
		}

		if($group -> user_id != $session -> user_id) {
			return error_response("change_group_title", "Access denied");
		}

		$group -> title = $new_title;

		if(!$group -> update()) {
			return error_response("change_group_title", "Something wrong");
		}

		return success_response();
	}
}