Merge pull request #169 from allerta-vvf/feat_schedules_holidays

Feat schedules holidays
This commit is contained in:
Matteo Gheza 2021-04-28 17:45:05 +02:00 committed by GitHub
commit 287a79b14e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 136 additions and 9 deletions

View File

@ -18,6 +18,7 @@
"brick/phonenumber": "^0.2.2",
"sentry/sdk": "^3.1",
"maximebf/debugbar": "^1.16",
"azuyalabs/yasumi": "^2.3",
"ministryofweb/php-osm-tiles": "^2.0",
"jenstornell/tiny-html-minifier": "dev-master"
},

72
server/composer.lock generated
View File

@ -4,8 +4,78 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c59f5a601b2cf561d3a7d98c6e145afd",
"content-hash": "6ed99ceb745c54c207e4366b68ef0637",
"packages": [
{
"name": "azuyalabs/yasumi",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/azuyalabs/yasumi.git",
"reference": "e2f37e6de3b15642b83275a24bbfe101cd5c7791"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/azuyalabs/yasumi/zipball/e2f37e6de3b15642b83275a24bbfe101cd5c7791",
"reference": "e2f37e6de3b15642b83275a24bbfe101cd5c7791",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=7.2"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"fzaninotto/faker": "^1.9",
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": "^8.5"
},
"suggest": {
"ext-calendar": "For calculating the date of Easter"
},
"type": "library",
"autoload": {
"psr-4": {
"Yasumi\\": "src/Yasumi/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Sacha Telgenhof",
"email": "me@sachatelgenhof.com",
"role": "Maintainer"
}
],
"description": "The easy PHP Library for calculating holidays.",
"homepage": "https://www.yasumi.dev",
"keywords": [
"Bank",
"calculation",
"calendar",
"celebration",
"date",
"holiday",
"holidays",
"national",
"time"
],
"support": {
"docs": "https://www.yasumi.dev",
"issues": "https://github.com/azuyalabs/yasumi/issues",
"source": "https://github.com/azuyalabs/yasumi"
},
"funding": [
{
"url": "https://www.buymeacoffee.com/sachatelgenhof",
"type": "other"
}
],
"time": "2020-06-22T12:46:20+00:00"
},
{
"name": "brick/phonenumber",
"version": "0.2.2",

View File

@ -403,6 +403,7 @@ class user
private $profile_names = null;
public $auth = null;
public $authenticated = false;
public $holidays = null;
public function __construct($database, $tools)
{
@ -432,6 +433,7 @@ class user
$this->authenticated = $this->auth->isLoggedIn();
$this->profile_names = $this->database->exec("SELECT `id`, `name` FROM `%PREFIX%_profiles`;", true);
$this->user_names = $this->database->exec("SELECT `id`, `username` FROM `%PREFIX%_users`;", true);
$this->holidays = Yasumi\Yasumi::create($this->database->get_option("holidays_provider") ?: "USA", date("Y"), $this->database->get_option("holidays_language") ?: "en_US");
}
public function authenticated()

View File

@ -36,7 +36,12 @@ $cronJobDateTime = [
];
$start = $database->get_option("cron_job_enabled") && ((isset($_POST['cron']) && $_POST['cron'] == "cron_job-".$database->get_option("cron_job_code")) || (isset($_SERVER['HTTP_CRON']) && $_SERVER['HTTP_CRON'] == "cron_job-".$database->get_option("cron_job_code")));
$start_reset = $execDateTime == $cronJobDateTime;
$start_reset = ( $execDateTime["day"] == $cronJobDateTime["day"] &&
$execDateTime["day"] == $cronJobDateTime["day"] &&
$execDateTime["month"] == $cronJobDateTime["month"] &&
$execDateTime["year"] == $cronJobDateTime["year"] &&
$execDateTime["hour"] == $cronJobDateTime["hour"] &&
$execDateTime["minutes"] - $cronJobDateTime["minutes"] < 5 );
$action = "Availability Minutes ";
if($start) {
@ -96,6 +101,11 @@ if($start) {
];
$id = $row["id"];
$user_id = $row["user"];
$selected_holidays = json_decode($row["holidays"]);
$selected_holidays_dates = [];
foreach ($selected_holidays as $holiday){
$selected_holidays_dates[] = $user->holidays->getHoliday($holiday)->format('Y-m-d');
}
foreach ($row["schedules"] as $key => $value) {
$schedule = [
"day" => (int) $value[0]+1,
@ -115,7 +125,7 @@ if($start) {
$now["minutes"] - $schedule["minutes"] <= 30
){
if(!in_array($user_id,$schedules_users)) $schedules_users[] = $user_id;
if($schedule["hour"] == $last_exec["hour"] ? $schedule["minutes"] !== $last_exec["minutes"] : true){
if($schedule["hour"] == $last_exec["hour"] ? $schedule["minutes"] !== $last_exec["minutes"] : true && !in_array(date('Y-m-d'), $selected_holidays_dates)){
$last_exec_new = $schedule["day"].";".sprintf("%02d", $schedule["hour"]).":".sprintf("%02d", $schedule["minutes"]);
$database->exec("UPDATE `%PREFIX%_schedules` SET `last_exec` = :last_exec WHERE `id` = :id;", false, [":id" => $id, ":last_exec" => $last_exec_new]);
$database->exec("UPDATE `%PREFIX%_profiles` SET available = '1', availability_last_change = 'cron' WHERE `id` = :user_id;", false, [":user_id" => $user_id]);

View File

@ -424,6 +424,7 @@ CREATE TABLE `".$prefix."_schedules` (
`user` INT NOT NULL,
`profile_name` VARCHAR(500) NOT NULL DEFAULT 'default',
`schedules` VARCHAR(10000) NULL DEFAULT NULL,
`holidays` VARCHAR(10000) NULL DEFAULT NULL,
`last_exec` VARCHAR(7) NULL DEFAULT NULL,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
@ -498,6 +499,8 @@ function initOptions($name, $visible, $developer, $password, $report_email, $own
'use_location_picker' => 1,
'force_language' => 0,
'force_remember_cookie' => 0,
'holidays_provider' => 'USA',
'holidays_language' => 'en_US',
'messages' => "{}"
];
$query = "";

View File

@ -52,8 +52,10 @@ if(!empty($result)){
$hour = $hour[0] == "0" ? substr($hour,1) : $hour;
$old_schedules[$schedule[0]][$hour] = true;
}
$old_holidays = json_decode($result[0]["holidays"]);
} else {
$old_schedules = [];
$old_holidays = [];
}
?>
<style>
@ -127,6 +129,35 @@ endif;
</tbody>
</table>
<br>
<?php
$holidays_selection_question = t("Do you want to exclude holidays from schedules?",false);
$holidays_select_all = t("Select all", false);
$holidays_select_none = t("Remove selections", false);
echo(<<<EOL
<div class="form-group">
<label>{$holidays_selection_question}</label>
<a onclick="$('.holiday_check').prop('checked', true);" class="text-primary">{$holidays_select_all}</a> / <a onclick="$('.holiday_check').prop('checked', false);" class="text-primary">{$holidays_select_none}</a>
EOL);
$i = 0;
foreach ($user->holidays as $holiday) {
$i++;
$holiday_name = $holiday->getName();
$holiday_shortname = $holiday->shortName;
$is_holiday_selected = in_array($holiday_shortname, $old_holidays) ? "checked" : "";
echo(<<<EOT
<div class="form-check">
<input class="form-check-input holiday_check" name="holiday_check" type="checkbox" value="{$holiday_shortname}" id="holidayCheckbox{$i}" {$is_holiday_selected}>
<label class="form-check-label" for="holidayCheckbox{$i}">
{$holiday_name} ({$holiday})
</label>
</div>
EOT);
}
echo("</div>");
?>
<script>
function init_modal() {
<?php if($orienation == "landscape"){ ?>$(".modal-dialog").css("max-width", "99%");<?php } ?>
@ -218,11 +249,13 @@ function extractSelections(){
function submit_changes(){
let hours = extractSelections();
let holidays = $.map($('input[name="holiday_check"]:checked'), function(c){return c.value; });
$.ajax({
url: "resources/ajax/ajax_availability_schedule.php",
method: "POST",
data: {
hours: hours
hours: hours,
holidays: holidays
},
success: function (data) {
console.log(data);

View File

@ -7,15 +7,17 @@ $user_id = $user->auth->getUserId();
$result = $database->exec("SELECT * FROM `%PREFIX%_schedules` WHERE `user`={$user_id};", true);
if(!empty($result)){
$result[0]["schedules"] = json_decode($result[0]["schedules"]);
$result[0]["holidays"] = json_decode($result[0]["holidays"]);
}
if(isset($_POST["hours"])){
$hours = (string) json_encode($_POST["hours"]);
echo($hours);
$holidays = (string) json_encode($_POST["holidays"]);
echo($hours."-".$holidays);
if(!empty($result)){
$database->exec("UPDATE `%PREFIX%_schedules` SET `schedules` = :schedules WHERE `id` = :id;", false, [":id" => $result[0]["id"], ":schedules" => $hours]);
$database->exec("UPDATE `%PREFIX%_schedules` SET schedules = :schedules, holidays = :holidays WHERE `id` = :id;", false, [":id" => $result[0]["id"], ":schedules" => $hours, ":holidays" => $holidays]);
} else {
$database->exec("INSERT INTO `%PREFIX%_schedules` (`user`, `schedules`) VALUES (:user, :schedules);", false, [":user" => $user_id, ":schedules" => $hours]);
$database->exec("INSERT INTO `%PREFIX%_schedules` (`user`, `schedules`, `holidays`) VALUES (:user, :schedules, :holidays);", false, [":user" => $user_id, ":schedules" => $hours, ":holidays" => $holidays]);
}
} else {
echo(json_encode(empty($result) ? [] : $result[0]));

View File

@ -121,5 +121,8 @@ return [
"Thu" => "Thu",
"Fri" => "Fri",
"Sat" => "Sat",
"Sun" => "Sun"
"Sun" => "Sun",
"Do you want to exclude holidays from schedules?" => "Do you want to exclude holidays from schedules?",
"Select all" => "Select all",
"Remove selections" => "Remove selections"
];

View File

@ -121,5 +121,8 @@ return [
"Thu" => "Gio",
"Fri" => "Ven",
"Sat" => "Sab",
"Sun" => "Dom"
"Sun" => "Dom",
"Do you want to exclude holidays from schedules?" => "Vuoi escudere giorni festivi dalla programmazione degli orari?",
"Select all" => "Seleziona tutti",
"Remove selections" => "Rimuovi selezioni"
];