[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Noalyss-commit] [noalyss] 114/323: New : code for progress bar
From: |
Dany De Bontridder |
Subject: |
[Noalyss-commit] [noalyss] 114/323: New : code for progress bar |
Date: |
Wed, 14 Mar 2018 17:38:35 -0400 (EDT) |
sparkyx pushed a commit to branch master
in repository noalyss.
commit 29da5f05706dae57e5e52da7936d2fc9af3c2752
Author: Dany De Bontridder <address@hidden>
Date: Sat Feb 3 14:28:37 2018 +0100
New : code for progress bar
---
doc/developper/Doxyfile | 1 -
html/ajax_misc.php | 11 ++++
html/js/scripts.js | 67 +++++++++++++++++++++
html/style-light.css | 26 +++++++++
html/style-r692.css | 26 +++++++++
html/test.php | 2 +-
include/lib/progress_bar.class.php | 115 +++++++++++++++++++++++++++++++++++++
scenario/progress-bar.test.php | 65 +++++++++++++++++++++
sql/upgrade.sql | 10 ++++
9 files changed, 321 insertions(+), 2 deletions(-)
diff --git a/doc/developper/Doxyfile b/doc/developper/Doxyfile
index 63ef352..3571e04 100644
--- a/doc/developper/Doxyfile
+++ b/doc/developper/Doxyfile
@@ -673,7 +673,6 @@ EXCLUDE_SYMLINKS = NO
EXCLUDE_PATTERNS = a?.php \
b?.php \
?.php \
- test*.php \
calendar-*.js \
richtext.js \
builder.js \
diff --git a/html/ajax_misc.php b/html/ajax_misc.php
index 38e2332..c77c74c 100644
--- a/html/ajax_misc.php
+++ b/html/ajax_misc.php
@@ -46,6 +46,7 @@ require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
require_once NOALYSS_INCLUDE.'/class/user.class.php';
require_once NOALYSS_INCLUDE.'/lib/http_input.class.php';
require_once NOALYSS_INCLUDE.'/lib/icon_action.class.php';
+require_once NOALYSS_INCLUDE.'/lib/progress_bar.class.php';
$http=new HttpInput();
mb_internal_encoding("UTF-8");
@@ -86,6 +87,16 @@ else
$g_user = new User($cn);
$g_user->check(true);
}
+
+// For progress bar, for saving time , we check and answer directly
+if ($op == "progressBar") {
+ $task_id=$http->request("task_id");
+ $task=new Progress_Bar($task_id);
+ $task->answer();
+ return;
+}
+
+
$html = var_export($_REQUEST, true);
set_language();
if ( LOGINPUT)
diff --git a/html/js/scripts.js b/html/js/scripts.js
index 6334279..a212591 100644
--- a/html/js/scripts.js
+++ b/html/js/scripts.js
@@ -3394,3 +3394,70 @@ Periode.filter_exercice=function (p_table_id) {
}
};
+
+// keep track of progress bar
+var progressBar = [];
+// idx of progress bar
+var progressIdx = 0;
+
+/**
+ * Start the progress bar
+ * @param {string} p_taskid id to monitor
+ * @param {int} p_dossier
+ */
+function progress_bar_start(p_taskid)
+{
+ try {
+ progressIdx++;
+ // Create a div
+ add_div({id: "progressDiv" + progressIdx, cssclass: "progressbar",
html: '<span id="progressValue">0</span>'});
+ // Check status every sec.
+ progressBar[progressIdx] = setInterval(progress_bar_check.bind(null,
progressIdx, p_taskid), 1000);
+ } catch (e) {
+ console.error(e.message);
+ }
+}
+
+/**
+ * Check every second the status
+ * @param {integer} p_idx idx of progressbar
+ * @param {string} p_taskid id to monitor
+ */
+function progress_bar_check(p_idx, p_taskid)
+{
+ try {
+
+ new Ajax.Request("ajax_misc.php", {
+ parameters: {gDossier: 0, task_id: p_taskid,op:"progressBar"},
+ onSuccess: function (req) {
+ try
+ {
+ var answer=req.responseText.evalJSON();
+
+ var progressValue = $('progressValue');
+ var progress = parseFloat(progressValue.innerHTML);
+ if ( answer.value <= progress ) {
+ return;
+ }
+
+ progressValue.innerHTML = answer.value;
+ progressValue.setStyle("width:" + answer.value + "%");
+ if (answer.value== 100) {
+ clearInterval(progressBar[p_idx]);
+ progressValue.innerHTML="Success";
+
Effect.BlindUp("progressDiv"+progressIdx,{duration:1.0,scaleContent:false})
+ }
+ } catch (e) {
+ clearInterval(progressBar[p_idx]);
+
document.getElementById("progressValue").innerHTML=req.responseText;
+ console.error(e.message);
+ }
+ }
+ });
+ } catch (e) {
+ clearInterval(progressBar[p_idx]);
+ console.error(e.message);
+ }
+}
+
+
diff --git a/html/style-light.css b/html/style-light.css
index 8875abe..7e291e8 100644
--- a/html/style-light.css
+++ b/html/style-light.css
@@ -2200,4 +2200,30 @@ div.bxbutton .icon:hover
{
background-color: white;
color: blue;
+}
+/**
+ * progressBar
+ */
+div.progressbar {
+ width:300px;
+ height:30px;
+ position:fixed;
+ top:5px;
+ left:40%;
+ background-color: white;
+ color:blue;
+ z-index:800;
+ border-color: #596a72;
+ border-width: 1px;
+ border-style: solid;
+}
+#progressValue {
+ display:block;
+ width: 0px;
+ height: 100%;
+ margin:0px;
+ padding: 0px;
+ background-color:darkblue;
+ color:antiquewhite;
+ font-weight: bolder;
}
\ No newline at end of file
diff --git a/html/style-r692.css b/html/style-r692.css
index 5e2efd9..ad8b18d 100644
--- a/html/style-r692.css
+++ b/html/style-r692.css
@@ -2227,4 +2227,30 @@ div.bxbutton .icon:hover
{
background-color: white;
color: blue;
+}
+/**
+ * progressBar
+ */
+div.progressbar {
+ width:300px;
+ height:30px;
+ position:fixed;
+ top:5px;
+ left:40%;
+ background-color: white;
+ color:blue;
+ z-index:800;
+ border-color: #596a72;
+ border-width: 1px;
+ border-style: solid;
+}
+#progressValue {
+ display:block;
+ width: 0px;
+ height: 100%;
+ margin:0px;
+ padding: 0px;
+ background-color:darkblue;
+ color:antiquewhite;
+ font-weight: bolder;
}
\ No newline at end of file
diff --git a/html/test.php b/html/test.php
index d5a467e..e45ddc2 100644
--- a/html/test.php
+++ b/html/test.php
@@ -23,7 +23,7 @@
* It is only a quick and dirty testing. You should use a tool as PHPUNIT for
the unit testing
*
* - first do not forget to create the authorized_debug file in the html
folder
- * - secund the test must adapted to this page : if you do a post (or get)
from a test, you won't get any result
+ * - secund the test must be adapted to this page : if you do a post (or get)
from a test, you won't get any result
* if the $_REQUEST[test_select] is not set, so set it .
*/
diff --git a/include/lib/progress_bar.class.php
b/include/lib/progress_bar.class.php
new file mode 100644
index 0000000..04e6f24
--- /dev/null
+++ b/include/lib/progress_bar.class.php
@@ -0,0 +1,115 @@
+<?php
+
+/*
+ * Copyright (C) 2018 Dany De Bontridder <address@hidden>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+
+/**
+ * @file
+ * @brief Manage the progress bar and display it with javascript
+ *
+ */
+
+/**
+ * @brief Use one db for tracking progress bar value, the task id must be
unique
+ * and let you follow the progress of a task.
+ * how it works : when calling an ajax , you have to create the task id and
start
+ * the monitoring of it (js function = progress_bar_start).
+ * In your php script called by ajax you call Progress_Bar->set_value to show
+ * the progress. The timer created by progress_bar_start will check regularly
the
+ * progress in the db.
+ * The ajax parameter for following the task is task_id
+ *
+ address@hidden you have to use session_write_close(); in the ajax file ,
otherwise,
+ * the function progress_bar_check will be blocked and won't update the
progress
+ * bar
+ *
+ address@hidden progress_bar_start
+ address@hidden progress_bar_check
+ *
+ *
+ */
+class Progress_Bar
+{
+ private $db ; //!< database connexion
+ private $task_id ; //! task id (progress_bar.p_id)
+ private $value; //!< value of progress (between 0 & 100)
+ /**
+ * @example progress-bar.test.php test of this class
+ * @param type $p_task_id
+ */
+ function __construct($p_task_id)
+ {
+ $this->db=new Database();
+ $this->task_id=$p_task_id;
+ // Find value from db
+ $this->value = $this->db->exec_sql("select p_value from progress where
p_id=$1",
+ [$p_task_id]);
+
+ // if task doesn't exists, create it
+ if ( $this->db->size()==0)
+ {
+ $this->value=0;
+ $this->db->exec_sql("insert into progress(p_id,p_value) values
($1,0)",
+ [$p_task_id]);
+ }
+ }
+ /**
+ * Store the progress value into the db
+ * @param integer $p_value value of the progress between 0 & 100
+ address@hidden code 1005 - if p_value is not in between 0 & 100
+ */
+ function set_value($p_value)
+ {
+ if ( $p_value > 100 || $p_value < 0 ) {
+ throw new Exception("Invalid value",EXC_PARAM_VALUE);
+ }
+ $this->value=$p_value;
+ $this->db->start();
+ $this->db->exec_sql("update progress set p_value=$1 where p_id=$2",
+ [$this->value,$this->task_id]);
+ $this->db->commit();
+ }
+ /**
+ * Get the progress value from db
+ * @return integer between 0 & 100
+ */
+ function get_value()
+ {
+ $this->value = $this->db->get_value("select p_value from progress
where p_id=$1",
+ [$this->task_id]);
+
+ return $this->value;
+ }
+ /**
+ * Json answer of the task progressing
+ * if value is equal or greater than 100 , delete the row
+ * @return type
+ */
+ function answer()
+ {
+ $this->get_value();
+
+ header('Content-Type: application/json');
+ echo json_encode(["value"=>$this->value]);
+ if ($this->value>=100) {
+ $this->db->exec_sql("delete from progress where
p_id=$1",[$this->task_id])
+ }
+ return;
+ }
+}
diff --git a/scenario/progress-bar.test.php b/scenario/progress-bar.test.php
new file mode 100644
index 0000000..f7fdb68
--- /dev/null
+++ b/scenario/progress-bar.test.php
@@ -0,0 +1,65 @@
+<?php
+
+/*
+ * Copyright (C) 2018 Dany De Bontridder <address@hidden>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+/**
+ * @file
+ * @brief Example about Progress_Bar, the most important is the
+ * session_write_close in the ajax script to unblock the PHP script.
+ * Each time a step is reached , you use Progress_Bar->set_value
+ * @see Progress_Bar
+ * @see progress_bar_start
+ * @see progress_bar_check
+ */
+require_once NOALYSS_INCLUDE.'/lib/progress_bar.class.php';
+
+$ajax=$http->request("TestAjaxFile","string","no");
+
+// $ajax != no if we are in ajax mode
+if ( $ajax != "no")
+{
+ session_write_close();
+ $task=$http->request("task_id");
+ $progress=new Progress_Bar($task);
+ sleep(1);
+ $progress->set_value(10);
+ sleep(2);
+ $progress->set_value(20);
+ sleep(1);
+ $progress->set_value(90);
+ sleep(2);
+ $progress->set_value(91);
+ sleep(5);
+ $progress->set_value(95);
+ sleep(6);
+ $progress->set_value(100);
+ return;
+}
+?>
+
+<script>
+ function start_test()
+ {
+ var task_id='<?php echo uniqid()?>';
+ progress_bar_start(task_id);
+ new Ajax.Request("ajax_test.php",{
+ parameters:{"TestAjaxFile":"<?php echo __FILE__?>",gDossier:<?php
echo Dossier::id()?>,'task_id':task_id}
+ });
+ }
+</script>
+<button onclick="start_test()"> Start progress</button>
\ No newline at end of file
diff --git a/sql/upgrade.sql b/sql/upgrade.sql
index eebd134..21af50a 100644
--- a/sql/upgrade.sql
+++ b/sql/upgrade.sql
@@ -9,6 +9,14 @@ delete from theme where the_filestyle in
('style-mandarine.css','style-mobile.cs
update user_global_pref set parameter_value='style-classic7.css' where
parameter_value in ('style-mandarine.css','style-mobile.css');
-- add constraint
alter table jnt_use_dos add CONSTRAINT use_id_dos_id_uniq UNIQUE
(use_id,dos_id);
+-- create table to check progress
+create table progress
+(
+ p_id varchar(16) primary key,
+ p_value integer not null ,
+ p_created timestamp default now()
+);
+
*/
create sequence tmp_pcmn_id_seq;
ALTER TABLE tmp_pcmn ADD COLUMN id bigint;
@@ -107,3 +115,5 @@ ALTER TABLE tags ADD CONSTRAINT tags_check CHECK (t_actif
in ('N','Y')) ;
alter table tags alter t_actif set default 'Y';
COMMENT ON COLUMN tags.t_actif is 'Y if the tag is activate and can be used ';
+
+-- in repo
- [Noalyss-commit] [noalyss] 115/323: translate, (continued)
- [Noalyss-commit] [noalyss] 115/323: translate, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 119/323: Missing lib, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 118/323: Progress Bar : use GET , which is faster than POST, use 2 decimal in progress because if the step is < 1 , it doesn't work, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 117/323: Export PDF from ANC , add a progress_bar, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 16/323: Bug 1516 : réécriture du module période, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 111/323: History of analytic account the same way as the history for card or accounting, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 120/323: New LOGO, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 122/323: cosmetic menu level2, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 124/323: Fix : logo and new style by default, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 125/323: fix : managetable correct position, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 114/323: New : code for progress bar,
Dany De Bontridder <=
- [Noalyss-commit] [noalyss] 129/323: Small bug : new structure for table version, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 126/323: fix : typo default theme Classic7, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 121/323: use icon_action::icon_magnify instead of icon, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 128/323: Improve CSS, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 109/323: update account_repository (to review), Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 131/323: upgrade SQL : syntax on FK, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 127/323: Esthetic, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 130/323: upgrade SQL : issue with trigger on tmp_pcmn, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 132/323: set version, Dany De Bontridder, 2018/03/14
- [Noalyss-commit] [noalyss] 133/323: Fix bug for updating, Dany De Bontridder, 2018/03/14