raspisms/controllers/internals/Group.php

142 lines
4.0 KiB
PHP
Raw Normal View History

<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace controllers\internals;
/**
* Classe des groups.
*/
class Group extends StandardController
{
2020-01-17 18:19:25 +01:00
protected $model;
/**
2020-01-17 18:19:25 +01:00
* Create a new group for a user.
*
* @param int $id_user : user id
* @param stirng $name : Group name
2020-01-17 18:19:25 +01:00
* @param array $contacts_ids : Ids of the contacts of the group
*
* @return mixed bool|int : false on error, new group id
*/
2020-01-17 18:19:25 +01:00
public function create(int $id_user, string $name, array $contacts_ids)
{
$group = [
'id_user' => $id_user,
'name' => $name,
];
$id_group = $this->get_model()->insert($group);
if (!$id_group)
{
return false;
}
$internal_contact = new Contact($this->bdd);
foreach ($contacts_ids as $contact_id)
{
$contact = $internal_contact->get_for_user($id_user, $contact_id);
if (!$contact)
{
continue;
}
$this->get_model()->insert_group_contact_relation($id_group, $contact_id);
}
$internal_event = new Event($this->bdd);
2020-01-17 18:19:25 +01:00
$internal_event->create($id_user, 'GROUP_ADD', 'Ajout group : '.$name);
return $id_group;
}
/**
2020-01-17 18:19:25 +01:00
* Update a group for a user.
*
* @param int $id_user : User id
* @param int $id_group : Group id
* @param stirng $name : Group name
2020-01-17 18:19:25 +01:00
* @param array $contacts_ids : Ids of the contacts of the group
*
* @return bool : False on error, true on success
*/
public function update_for_user(int $id_user, int $id_group, string $name, array $contacts_ids)
{
$group = [
'name' => $name,
];
$result = $this->get_model()->update_for_user($id_user, $id_group, $group);
$this->get_model()->delete_group_contact_relations($id_group);
$internal_contact = new Contact($this->bdd);
$nb_contact_insert = 0;
foreach ($contacts_ids as $contact_id)
{
$contact = $internal_contact->get_for_user($id_user, $contact_id);
if (!$contact)
{
continue;
}
if ($this->get_model()->insert_group_contact_relation($id_group, $contact_id))
{
2020-01-17 18:19:25 +01:00
++$nb_contact_insert;
}
}
if (!$result && $nb_contact_insert !== \count($contacts_ids))
{
return false;
}
return true;
}
/**
2020-01-17 18:19:25 +01:00
* Return a group by his name for a user.
*
* @param int $id_user : User id
* @param string $name : Group name
*
* @return array
*/
2020-01-17 18:19:25 +01:00
public function get_by_name_for_user(int $id_user, string $name)
{
return $this->get_model()->get_by_name_for_user($id_user, $name);
}
/**
2020-01-17 18:19:25 +01:00
* Get groups contacts.
*
* @param int $id_group : Group id
2020-01-17 18:19:25 +01:00
*
* @return array : Contacts of the group
*/
public function get_contacts($id_group)
{
return $this->get_model()->get_contacts($id_group);
}
2020-01-17 18:19:25 +01:00
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Group($this->bdd);
return $this->model;
}
}