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