Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto open task edit form in some circunstances #36

Merged
merged 4 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ before_script:
- composer self-update
- composer require --dev atoum/atoum
- git clone --depth=1 https://github.com/glpi-project/glpi -b $GLPIVER ../glpi && cd ../glpi
- composer install --optimize-autoloader --no-dev
- sed -e '/"php":/d' -i composer.json
- sed -i 's/CssMin/cssmin/;s/Faker/Faker/;s/vfsStream/vfsstream/' composer.json
- composer install --optimize-autoloader --no-dev
- rm -f composer.lock
- mysql -u root -e 'create database glpitest;'
# Both 9.3 and 9.4:
Expand All @@ -36,7 +37,7 @@ script:
- ./vendor/bin/atoum --debug -bf tests/bootstrap.php -d tests/units/


# Permit failure on Glpi 9.4 until fix test environment
# Permit failure on PHP nightly until Travis fix missing gd extension
matrix:
exclude:
- php: 5.6
Expand Down
4 changes: 2 additions & 2 deletions ajax/timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
if (! PluginActualtimeTask::checkUserFree(Session::getLoginUserID())) {

// action=start, timer=off, current user is alerady using timer
$opcional=PluginActualtimeTask::getTicket(Session::getLoginUserID());
$ticket_id = PluginActualtimeTask::getTicket(Session::getLoginUserID());
$result=[
'mensage' => __("You are already doing a task", 'actualtime')." <a onclick='showtaskform(event)' href='/front/ticket.form.php?id=".$opcional."'>".__("Ticket")."</a>",
'mensage' => __("You are already doing a task", 'actualtime')." <a onclick='actualtime_showTaskForm(event)' href='/front/ticket.form.php?id=" . $ticket_id . "'>" . __("Ticket") . "$ticket_id</a>",
'title' => __('Warning'),
'class' => 'warn_msg',
];
Expand Down
8 changes: 6 additions & 2 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ function plugin_actualtime_install() {
return true;
}

function plugin_activetime_item_stats($item) {
function plugin_actualtime_item_stats($item) {
PluginActualtimeTask::showStats($item);
}

function plugin_activetime_item_update($item) {
function plugin_actualtime_item_update($item) {
PluginActualtimeTask::preUpdate($item);
}

function plugin_actualtime_item_add($item) {
PluginActualtimeTask::afterAdd($item);
}

/**
* Uninstall previously installed elements of the plugin
*
Expand Down
91 changes: 73 additions & 18 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@ function show_hide_options(val) {
echo "</td>";
echo "</tr>";

//echo "<tr class='tab_bg_1' name='optional$rand' $style>
// <td>example settings 2";
//echo "</td>";
//echo "</tr>";
echo "<tr class='tab_bg_1' name='optional$rand' $style>";
echo "<td>" . __("Automatically open new created tasks", "actualtime") . "</td><td>";
Dropdown::showYesNo('autoopennew', $this->autoOpenNew(), -1);
echo "</td>";
echo "</tr>";

echo "<tr class='tab_bg_1' align='center'>";
echo "<tr class='tab_bg_1' name='optional$rand' $style>";
echo "<td>" . __("Automatically open task with timer running", "actualtime") . "</td><td>";
Dropdown::showYesNo('autoopenrunning', $this->autoOpenRunning(), -1);
echo "</td>";
echo "</tr>";

echo "<tr class='tab_bg_1' align='center'>";
$this->showFormButtons(['candel'=>false]);
}

Expand Down Expand Up @@ -135,6 +141,25 @@ function showTimerPopup() {
return ($this->fields['showtimerpopup'] ? true : false);
}

/**
* Auto open the form for the task that was just created (new tasks)?
*
* @return boolean
*/
function autoOpenNew() {
return ($this->fields['autoopennew'] ? true : false);
}

/**
* Auto open the form for the task with a currently running timer
* when listing tickets' tasks?
*
* @return boolean
*/
function autoOpenRunning() {
return ($this->fields['autoopenrunning'] ? true : false);
}

static function install(Migration $migration) {
global $DB;

Expand All @@ -145,35 +170,65 @@ static function install(Migration $migration) {
$query = "CREATE TABLE IF NOT EXISTS $table (
`id` int(11) NOT NULL auto_increment,
`enable` boolean NOT NULL DEFAULT true,
`showtimerpopup` boolean NOT NULL DEFAULT true,
`autoopennew` boolean NOT NULL DEFAULT false,
`autoopenrunning` boolean NOT NULL DEFAULT false,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die($DB->error());
}

if ($DB->tableExists($table)) {

if (! $DB->fieldExists($table, 'showtimerpopup')) {
// Add new field showtimerpopup
$migration->addField(
$table,
'showtimerpopup',
'boolean',
[
'update' => true,
'value' => true,
'after' => 'enable'
]
);
}
if (! $DB->fieldExists($table, 'autoopennew')) {
// Add new field autoopennew
$migration->addField(
$table,
'autoopennew',
'boolean',
[
'update' => false,
'value' => false,
'after' => 'showtimerpopup',
]
);
}
if (! $DB->fieldExists($table, 'autoopenrunning')) {
// Add new field autoopenrunning
$migration->addField(
$table,
'autoopenrunning',
'boolean',
[
'update' => false,
'value' => false,
'after' => 'autoopennew',
]
);
}
// Create default record (if it does not exist)
$reg = $DB->request($table);
if (! count($reg)) {
$DB->insert(
$table, [
'enable' => 1
'enable' => true
]
);
}

$migration->addField(
$table,
'showtimerpopup',
'boolean',
[
'update' => 1,
'value' => 1,
'after' => 'enable'
]
);

}

}
Expand Down
102 changes: 100 additions & 2 deletions inc/task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ static function getTypeName($nb = 0) {
}

static public function postForm($params) {

global $CFG_GLPI;

$item = $params['item'];

switch ($item->getType()) {
Expand All @@ -26,7 +26,7 @@ static public function postForm($params) {

$task_id = $item->getID();
$rand = mt_rand();
$buttons = self::checkTech($task_id);
$buttons = (self::checkTech($task_id) && $item->can($task_id, UPDATE));
$time = self::totalEndTime($task_id);
$text_restart = __('Restart', 'actualtime');
$text_pause = __('Pause', 'actualtime');
Expand Down Expand Up @@ -448,6 +448,22 @@ static function getSegment($task_id) {
return $html;
}

static function afterAdd(TicketTask $item) {
global $DB;
$config = new PluginActualtimeConfig;
if ($config->autoOpenNew()) {
if ($item->getField('state')==1 && $item->fields['id']) {
// Empty record means just added task (for postShowItem)
$DB->insert(
'glpi_plugin_actualtime_tasks', [
'tasks_id' => $item->fields['id'],
'users_id' => Session::getLoginUserID(),
]
);
}
}
}

static function preUpdate(TicketTask $item) {
global $DB;

Expand Down Expand Up @@ -482,6 +498,88 @@ static function postShowTab($params) {
}
}

static function postShowItem($params) {
global $DB;

$item = $params['item'];
if (! is_object($item) || ! method_exists($item, 'getType')) {
// Sometimes, params['item'] is just an array, like 'Solution'
return;
}
switch ($item->getType()) {
case 'TicketTask':

$config = new PluginActualtimeConfig;
$task_id = $item->getID();
// Auto open needs to use correct item randomic number
$rand = $params['options']['rand'];

// Verify if this is a new task just created now
$autoopennew = false;
if ($config->autoOpenNew() && $item->fields['state']==1 && $task_id) {
// New created task opens automatically
$query=[
'FROM'=>self::getTable(),
'WHERE'=>[
'tasks_id' => $task_id,
'actual_begin' => null,
'actual_end' => null,
'users_id' => Session::getLoginUserID(),
]
];
$req = $DB->request($query);
if ($row = $req->next()) {
$autoopennew = true;
}
}
if ($autoopennew || ($config->autoOpenRunning() && self::checkUser($task_id, Session::getLoginUserID()))) {
// New created task or user has running timer on this task
// Open edit window automatically
$ticket_id = $item->fields['tickets_id'];
$div = "<div id='actualtime_autoEdit_{$task_id}_{$rand}' onclick='javascript:viewEditSubitem$ticket_id$rand(event, \"TicketTask\", $task_id, this, \"viewitemTicketTask$task_id$rand\")'></div>";
echo $div;
$script=<<<JAVASCRIPT
$(document).ready(function(){
$("#actualtime_autoEdit_{$task_id}_{$rand}").click();
if ($("[id^='actualtime_autoEdit_']").attr('id') == "actualtime_autoEdit_{$task_id}_{$rand}") {
// Only scroll the first task if two (first=newly opened, second=timer running)
function waitForFormLoad(i){
if ($("#viewitemTicketTask$task_id$rand textarea[name='content']").length) {
$([document.documentElement, document.body]).animate({
scrollTop: $("#viewitemTicketTask$task_id$rand").siblings("div.h_info").offset().top
}, 1000);
$("#viewitemTicketTask$task_id$rand textarea[name='content']").focus();
} else if (i > 10) {
return;
} else {
setTimeout(function() {
waitForFormLoad(++i)
}, 500);
}
}
waitForFormLoad(0);
}
});
JAVASCRIPT;

print_r(Html::scriptBlock($script));
if ($autoopennew) {
// Remove empty record
$DB->delete(
'glpi_plugin_actualtime_tasks', [
'id' => $row['id'],
'actual_begin' => null,
'actual_end' => null,
'users_id' => Session::getLoginUserID(),
]
);
}
}
break;

}
}

static function install(Migration $migration) {
global $DB;

Expand Down
2 changes: 1 addition & 1 deletion js/actualtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function actualtime_showTimerPopup(ticket) {
of = $("#actualtime_popup").parent().offset();
$("#actualtime_popup").parent().css('position', 'fixed');
$("#actualtime_popup").parent().offset(of);
}, 1000);
}, 1000);
}
}

Expand Down
10 changes: 8 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,20 @@ function plugin_init_actualtime() {
}

$PLUGIN_HOOKS['post_item_form']['actualtime'] = ['PluginActualtimeTask', 'postForm'];
$PLUGIN_HOOKS['show_item_stats']['actualtime'] = ['Ticket'=> 'plugin_activetime_item_stats'];
$PLUGIN_HOOKS['pre_item_update']['actualtime'] = ['TicketTask'=>'plugin_activetime_item_update'];
$PLUGIN_HOOKS['show_item_stats']['actualtime'] = ['Ticket'=> 'plugin_actualtime_item_stats'];
$PLUGIN_HOOKS['pre_item_update']['actualtime'] = ['TicketTask'=>'plugin_actualtime_item_update'];
$PLUGIN_HOOKS['post_show_item']['actualtime'] = ['PluginActualtimeTask', 'postShowItem'];
$PLUGIN_HOOKS['add_javascript']['actualtime'] = 'js/actualtime.js';

if ($config->showTimerPopup()) {
// This hook is not needed if not showing popup
$PLUGIN_HOOKS['post_show_tab']['actualtime'] = ['PluginActualtimeTask', 'postShowTab'];
}

if ($config->autoOpenNew()) {
// This hook is not needed if not opening new tasks automatically
$PLUGIN_HOOKS['item_add']['actualtime'] = ['TicketTask'=>'plugin_actualtime_item_add'];
}

}
}
14 changes: 14 additions & 0 deletions tests/units/PluginActualtimeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ public function testShowTimerPopup() {
->isTrue();
}

public function testAutoOpenNew() {
$this
->given($this->newTestedInstance)
->boolean($this->testedInstance->autoOpenNew())
->isFalse();
}

public function testAutoOpenRunning() {
$this
->given($this->newTestedInstance)
->boolean($this->testedInstance->autoOpenRunning())
->isFalse();
}

public function testCanView() {
$this
->given($this->newTestedInstance)
Expand Down