noalyss Version-9
inplace_switch.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * PhpCompta is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with PhpCompta; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20// Copyright (2016) Author Dany De Bontridder <dany@alchimerys.be>
21
22/**
23 * @file
24 * @brief A switch let you switch between 2 values : 0 and 1, it is used to
25 * replace the check
26 */
27/**
28 * @class Inplace_Switch
29 * @brief A switch let you switch between 2 values : 0 and 1, it is used to
30 * replace the check
31 */
32
34{
35
36 /// The icon on
37 private $iconon;
38 /// The icon off
39 private $iconoff;
40 /// name of the widget, javascript id must be unique
41 private $name;
42 /// value
43 private $value;
44 /// Json object
45 private $json;
46 /// callback
47 private $callback;
48 /// Supplemental javascript command, execute after the ajax script
49 private $jscript;
50
51 function __construct($p_name, $p_value)
52 {
53 $this->name=$p_name;
54 $this->value=$p_value;
55 $this->iconon=Icon_Action::iconon(uniqid(), "");
56 $this->iconoff=Icon_Action::iconoff(uniqid(), "");
57 $this->json=json_encode(['name'=>$p_name,"value"=>$p_value], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
58 $this->callback="ajax.php";
59 $this->jscript="";
60 }
61
62 function input()
63 {
64 if ($this->value==1)
65 {
66 $icon=$this->iconon;
67 $color="green";
68 }
69 elseif ($this->value==0)
70 {
71 $icon=$this->iconoff;
72 $color="red";
73 }
74 else
75 {
76 throw new Exception(_("Invalide value"));
77 }
78
79 $r=sprintf('<span style="text-decoration: none;color:%s" class="inplace_edit icon" id="%s">', $color,$this->name);
80 $r.=$icon;
81 $r.= '</span>';
82 $r.=<<<EOF
83 <script>
84{$this->name}.onclick=function() {new Ajax.Updater({$this->name},'{$this->callback}',{method:"get",parameters:{$this->json},evalScripts:true} );
85 {$this->jscript}
86 }
87</script>
88EOF;
89 return $r;
90 }
91 public function get_jscript()
92 {
93 return $this->jscript;
94 }
95
96 public function set_jscript($jscript)
97 {
98 $this->jscript=$jscript;
99 }
100
101 public function get_json()
102 {
103 return $this->json;
104 }
105
106 public function get_callback()
107 {
108 return $this->callback;
109 }
110
111 public function set_json($json)
112 {
113 $this->json=$json;
114 }
115
116 public function set_callback($callback)
117 {
118 $this->callback=$callback;
119 }
120
121 public function get_iconon()
122 {
123 return $this->iconon;
124 }
125
126 public function get_iconoff()
127 {
128 return $this->iconoff;
129 }
130
131 public function get_name()
132 {
133 return $this->name;
134 }
135
136 public function get_value()
137 {
138 return $this->value;
139 }
140
141 public function set_iconon($iconon)
142 {
143 $this->iconon=$iconon;
144 }
145
146 public function set_iconoff($iconoff)
147 {
148 $this->iconoff=$iconoff;
149 }
150
151 public function set_name($name)
152 {
153 $this->name=$name;
154 }
155
156 public function set_value($value)
157 {
158 $this->value=$value;
159 }
160 /**
161 * Add json parameter to the current one, if there attribute already exists
162 * it will be overwritten
163 */
164 function add_json_param($p_attribute,$p_value) {
165 $x=json_decode($this->json,TRUE);
166 $x[$p_attribute]=$p_value;
167 $this->json=json_encode($x, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK);
168 }
169}
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$from_poste name
static iconoff($p_id, $p_javascript, $p_style="")
Display a icon OFF.
static iconon($p_id, $p_javascript, $p_style="")
Display a icon ON.
A switch let you switch between 2 values : 0 and 1, it is used to replace the check.
__construct($p_name, $p_value)
add_json_param($p_attribute, $p_value)
Add json parameter to the current one, if there attribute already exists it will be overwritten.
$jscript
Supplemental javascript command, execute after the ajax script.
$name
name of the widget, javascript id must be unique
if( $delta< 0) elseif( $delta==0)