Skip to content

Commit

Permalink
Migrate Calendar subitems and DatabaseInstance to Twig
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 authored and trasher committed Jan 24, 2024
1 parent 82d0405 commit 49d85fe
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 419 deletions.
12 changes: 10 additions & 2 deletions css/includes/components/_buttons-group.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
Override the override of mixins by tabler over bootstrap
which broke styles of btn-group > .btn-check
*/
// TODO Can this be removed yet?

// TODO Can this mixin be removed yet?
@mixin button-check(
$color,
$color-hover: color-contrast($color),
Expand All @@ -63,3 +62,12 @@ which broke styles of btn-group > .btn-check
@include button-check($value);
}
}

// Make border color of secondary outline buttons match input border
.btn-group {
@each $color, $value in $theme-colors {
.btn-outline-secondary {
border: var(--tblr-border-width) solid var(--tblr-border-color);
}
}
}
4 changes: 0 additions & 4 deletions css/includes/components/_select2.scss
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@
}
}
}

& ~ .btn.btn-outline-secondary {
border: var(--tblr-border-width) solid var(--tblr-border-color);
}
}

// fix z-index issue for select2 dropdown container with a boostrap modal
Expand Down
135 changes: 44 additions & 91 deletions src/CalendarSegment.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* ---------------------------------------------------------------------
*/

use Glpi\Application\View\TemplateRenderer;
use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QueryFunction;

Expand All @@ -48,7 +49,6 @@ class CalendarSegment extends CommonDBChild
public static $itemtype = 'Calendar';
public static $items_id = 'calendars_id';


/**
* @since 0.84
**/
Expand All @@ -60,13 +60,11 @@ public function getForbiddenStandardMassiveAction()
return $forbidden;
}


public static function getTypeName($nb = 0)
{
return _n('Time range', 'Time ranges', $nb);
}


public function prepareInputForAdd($input)
{

Expand All @@ -92,15 +90,13 @@ public function prepareInputForAdd($input)

public function post_addItem()
{

// Update calendar cache
// Update calendar cache
$cal = new Calendar();
$cal->updateDurationCache($this->fields['calendars_id']);

parent::post_addItem();
}


public function post_deleteFromDB()
{

Expand All @@ -111,7 +107,6 @@ public function post_deleteFromDB()
parent::post_deleteFromDB();
}


/**
* Get segments of a calendar between 2 date
*
Expand Down Expand Up @@ -145,7 +140,6 @@ public static function getSegmentsBetween($calendars_id, $begin_day, $begin_time
);
}


/**
* Get active time between begin and end time in a day
*
Expand Down Expand Up @@ -187,7 +181,6 @@ public static function getActiveTimeBetween($calendars_id, $day, $begin_time, $e
return $sum;
}


/**
* Add a delay of a starting hour in a specific day
*
Expand Down Expand Up @@ -250,19 +243,19 @@ public static function addDelayInDay(
]);

foreach ($iterator as $data) {
list($hour, $minute, $second) = explode(':', $data['TDIFF']);
[$hour, $minute, $second] = explode(':', $data['TDIFF']);
$tstamp = (int)$hour * HOUR_TIMESTAMP + (int)$minute * MINUTE_TIMESTAMP + (int)$second;

// Delay is completed
if ($delay <= $tstamp) {
if (!$negative_delay) {
// Add time
list($begin_hour, $begin_minute, $begin_second) = explode(':', $data['BEGIN']);
[$begin_hour, $begin_minute, $begin_second] = explode(':', $data['BEGIN']);
$beginstamp = (int)$begin_hour * HOUR_TIMESTAMP + (int)$begin_minute * MINUTE_TIMESTAMP + (int)$begin_second;
$endstamp = $beginstamp + $delay;
} else {
// Substract time
list($begin_hour, $begin_minute, $begin_second) = explode(':', $data['END']);
[$begin_hour, $begin_minute, $begin_second] = explode(':', $data['END']);
$beginstamp = (int)$begin_hour * HOUR_TIMESTAMP + (int)$begin_minute * MINUTE_TIMESTAMP + (int)$begin_second;
$endstamp = $beginstamp - $delay;
}
Expand All @@ -277,7 +270,6 @@ public static function addDelayInDay(
return false;
}


/**
* Get first working hour of a day
*
Expand All @@ -303,7 +295,6 @@ public static function getFirstWorkingHour($calendars_id, $day)
return $result['minb'];
}


/**
* Get last working hour of a day
*
Expand All @@ -329,7 +320,6 @@ public static function getLastWorkingHour($calendars_id, $day)
return $result['mend'];
}


/**
* Is the hour passed is a working hour ?
*
Expand Down Expand Up @@ -358,7 +348,6 @@ public static function isAWorkingHour($calendars_id, $day, $hour)
return $result['cpt'] > 0;
}


/**
* Show segments of a calendar
*
Expand All @@ -378,6 +367,7 @@ public static function showForCalendar(Calendar $calendar)
$rand = mt_rand();

$iterator = $DB->request([
'SELECT' => ['id', 'day', 'begin', 'end'],
'FROM' => 'glpi_calendarsegments',
'WHERE' => [
'calendars_id' => $ID
Expand All @@ -388,105 +378,68 @@ public static function showForCalendar(Calendar $calendar)
'end'
]
]);
$numrows = count($iterator);

$daysofweek = Toolbox::getDaysOfWeekArray();
if ($canedit) {
echo "<div class='firstbloc'>";
echo "<form name='calendarsegment_form$rand' id='calendarsegment_form$rand' method='post'
action='";
echo Toolbox::getItemTypeFormURL(__CLASS__) . "'>";
echo "<table class='tab_cadre_fixe'>";
echo "<tr class='tab_bg_1'><th colspan='7'>" . __('Add a schedule') . "</tr>";

echo "<tr class='tab_bg_2'><td class='center'>" . _n('Day', 'Days', 1) . "</td><td>";
echo "<input type='hidden' name='calendars_id' value='$ID'>";
Dropdown::showFromArray('day', Toolbox::getDaysOfWeekArray());
echo "</td><td class='center'>" . __('Start') . '</td><td>';
Dropdown::showHours("begin", ['value' => date('H') . ":00"]);
echo "</td><td class='center'>" . __('End') . '</td><td>';
Dropdown::showHours("end", ['value' => (date('H') + 1) . ":00"]);
echo "</td><td class='center'>";
echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='btn btn-primary'>";
echo "</td></tr>";

echo "</table>";
Html::closeForm();
echo "</div>";
TemplateRenderer::getInstance()->display('pages/setup/calendarsegment.html.twig', [
'item' => new self(),
'calendars_id' => $ID,
'days_of_week' => $daysofweek,
'begin' => date('H') . ":00",
'end' => ((int) date('H') + 1) . ":00",
'params' => [
'canedit' => true,
]
]);
}

echo "<div class='spaced'>";
if ($canedit && $numrows) {
Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
$massiveactionparams = ['num_displayed' => min($_SESSION['glpilist_limit'], $numrows),
'container' => 'mass' . __CLASS__ . $rand
$entries = [];
foreach ($iterator as $data) {
$entries[] = [
'itemtype' => __CLASS__,
'id' => $data['id'],
'day' => $daysofweek[$data['day']],
'begin' => $data['begin'],
'end' => $data['end']
];
Html::showMassiveActions($massiveactionparams);
}
echo "<table class='tab_cadre_fixehov'>";
echo "<tr>";
if ($canedit && $numrows) {
echo "<th width='10'>";
echo Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
echo "</th>";
}
echo "<th>" . _n('Day', 'Days', 1) . "</th>";
echo "<th>" . __('Start') . "</th>";
echo "<th>" . __('End') . "</th>";
echo "</tr>";

$daysofweek = Toolbox::getDaysOfWeekArray();

if ($numrows) {
foreach ($iterator as $data) {
echo "<tr class='tab_bg_1'>";

if ($canedit) {
echo "<td>";
Html::showMassiveActionCheckBox(__CLASS__, $data["id"]);
echo "</td>";
}

echo "<td>";
echo $daysofweek[$data['day']];
echo "</td>";
echo "<td>" . $data["begin"] . "</td>";
echo "<td>" . $data["end"] . "</td>";
}
echo "</tr>";
}
echo "</table>";
if ($canedit && $numrows) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
echo "</div>";
TemplateRenderer::getInstance()->display('components/datatable.html.twig', [
'is_tab' => true,
'nofilter' => true,
'columns' => [
'day' => _n('Day', 'Days', 1),
'begin' => __('Start'),
'end' => __('End')
],
'entries' => $entries,
'total_number' => count($entries),
'filtered_number' => count($entries),
'showmassiveactions' => $canedit,
'massiveactionparams' => [
'num_displayed' => min($_SESSION['glpilist_limit'], count($entries)),
'container' => 'mass' . __CLASS__ . $rand
],
]);
}


public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{

if (!$withtemplate) {
$nb = 0;
if ($item instanceof Calendar) {
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = countElementsInTable(
$this->getTable(),
['calendars_id' => $item->getID()]
);
$nb = countElementsInTable(static::getTable(), ['calendars_id' => $item->getID()]);
}
return self::createTabEntry(self::getTypeName(Session::getPluralNumber()), $nb, $item::getType());
}
}
return '';
}


public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{

if ($item->getType() == 'Calendar') {
if ($item::class === 'Calendar') {
self::showForCalendar($item);
}
return true;
Expand Down
Loading

0 comments on commit 49d85fe

Please sign in to comment.