1 <?php
3 class glpiPrinterCartridges extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
7 var $attributes = array();
8 var $objectclasses = array("whatever");
10 var $parent = NULL; // Contains parent class, to access glpi db handle
11 var $usedCartridges = array(); // IDs of used cartridges for this printer
12 var $PrinterType = 0; // Specifies which cartridge types are available
14 var $cur_dialog = false;
15 var $cur_sub_dialog = false;
17 var $editManufacturer = false;
18 var $del = 0;
21 function glpiPrinterCartridges ($config,$dn,$type)
22 {
23 plugin::plugin ($config, $dn);
25 /* Assign some basic settings */
26 $this->ui = get_userinfo();
27 if(!isset($_SESSION['glpiCartridgeRegex'])){
28 $_SESSION['glpiCartridgeRegex'] = "*";
29 }
31 /* Only display cartridges for this type of printer */
32 $this->PrinterType = $type;
33 }
35 function execute()
36 {
37 /* Call parent execute */
38 plugin::execute();
39 $display ="";
40 $smarty = get_smarty();
41 $filter = $_SESSION['glpiCartridgeRegex'];
43 /* Filter settings, remove double* */
44 if(isset($_GET['search'])){
45 $filter = preg_replace("/\*\**/","*",$_GET['search']."*");
46 }elseif(isset($_POST['cartridge_regex'])){
47 $filter = preg_replace("/\*\**/","*",$_POST['cartridge_regex']);
48 }
49 if(empty($filter)) {
50 $filter = "*";
51 }
52 $_SESSION['glpiCartridgeRegex']= $filter;
54 /* Open dialog which allows to edit the manufacturers
55 */
56 if(isset($_POST['edit_manufacturer_cartridges'])){
57 $this->cur_sub_dialog = new glpiManufacturer($this->config,$this->dn);
58 $this->dialog = true;
59 $this->editManufacturer =true;
60 }
62 /* Close manufacturer editing dialog
63 */
64 if((isset($_POST['close_edit_manufacturer']))&&($this->editManufacturer)){
65 $this->dialog = false;
66 $this->cur_sub_dialog = false;
67 $this->editManufacturer=false;
68 }
70 /* Check poted vars and do the requested job, but only once */
71 $only_once = true;
72 foreach($_POST as $name => $value){
74 /* We have to create a new cartridge */
75 if(preg_match("/^newcartridge/",$name)&&($only_once)){
76 $this->cur_dialog = new glpiPrinterCartridgesEdit($this->config,$this->dn,$this->PrinterType);
77 $only_once = false;
78 }
79 }
81 /* Edit cartridge */
82 if(isset($_GET['act'])&&$_GET['act']=="edit_cartridge"){
83 $val = ($this->parent->handle->getCartridgeTypeInformations($_GET['id']));
84 $this->cur_dialog = new glpiPrinterCartridgesEdit($this->config,$this->dn,$this->PrinterType,$val[$_GET['id']]);
85 }
87 /* remove cartridge */
88 if(isset($_GET['act'])&&$_GET['act']=="del_cartridge"){
89 /* remove attach from db */
90 $this->del = $_GET['id'];
91 $val = ($this->parent->handle->getCartridgeTypeInformations($_GET['id']));
92 $smarty->assign("warning", sprintf(_("You're about to delete the glpi cartridge type '%s'."), $val[$this->del]['name']));
93 return($smarty->fetch(get_template_path('remove_glpi.tpl', TRUE)));
94 }
96 if(isset($_POST['delete_cancel'])){
97 $this->del = false;
98 }
100 /* Delete this entry */
101 if(isset($_POST['delete_glpi_confirm'])&&($this->del)) {
102 $tmp = $this->parent->handle->is_cartridgeTypeUsed($this->del);
103 if(count($tmp)){
105 $str = "";
106 foreach($tmp as $id => $name){
107 $str .= $name.", ";
108 }
109 $str = preg_replace("/, $/","",$str);
111 print_red(sprintf(_("You can't delete this cartridge type, it is still in use by this printer(s) '%s'."),$str));
112 }else{
113 $val = ($this->parent->handle->getCartridgeTypeInformations($this->del));
114 $this->cur_dialog = new glpiPrinterCartridgesEdit($this->config,$this->dn,$this->PrinterType,$val[$this->del]);
115 $this->cur_dialog->parent = $this->parent;
116 $this->cur_dialog->remove_from_parent();
117 $this->cur_dialog= false;
118 }
119 }
121 /* Abort edit / add dialog */
122 if(isset($_POST['CancelCartridge'])){
123 $this->cur_dialog = false;
124 }
126 /* Save changes if check is ok */
127 if(isset($_POST['SaveCartridge'])){
128 $this->cur_dialog->save_object();
129 if(count($this->cur_dialog->check())) {
130 foreach($this->cur_dialog->check() as $checks){
131 print_red($checks);
132 }
133 }else{
134 $this->cur_dialog->save();
135 $this->cur_dialog = false;
136 }
137 }
139 /* If we have a dialog open, display it */
140 if($this->cur_sub_dialog){
141 $this->cur_sub_dialog->parent = $this->parent;
142 $this->cur_sub_dialog->save_object();
143 return($this->cur_sub_dialog->execute());
144 }
145 /* If we have a dialog open, display it */
146 if($this->cur_dialog){
147 $this->cur_dialog->parent = $this->parent;
148 $this->cur_dialog->save_object();
149 return($this->cur_dialog->execute());
150 }
152 /* Create divlist */
153 $divlist = new divlist("glpi devices");
154 $divlist->SetEntriesPerPage(0);
155 $divlist->SetHeader(array(
156 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
157 array("string" => _("Cartridges"), "attach" => "style=''"),
158 array("string" => _("Action"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
160 /* Links for editing,adding.... */
161 $edit = "<a href='?plug=".$_GET['plug']."&act=edit_cartridge&id=%s'>%s</a>";
162 $editdel = "<a href='?plug=".$_GET['plug']."&act=edit_cartridge&id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
163 $editdel.= "<a href='?plug=".$_GET['plug']."&act=del_cartridge&id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
165 /* Checkbox for selection of some cartridges */
166 $useCartridge = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useCartridge_%s' %CHECKED%>";
168 /* Add cartridges */
169 $cart = $this->getCartridgeTypes();
171 /* Remove typically error possibilities */
172 $f = str_replace("/","\/",$filter);
173 $f = str_replace(".","\.",$f);
174 $f = str_replace("*",".*",$f);
176 /* Assign cartridges */
177 foreach($cart as $key=>$cartr){
179 /* Skip if filter doesn't match*/
180 if(!preg_match("/^".$f."$/i",$cartr['cartridgeName'])){
181 continue;
182 }
184 /* check if this cartridge is selected */
185 $chk = "";
186 if(isset($this->usedCartridges[$key])){
187 $chk = " checked ";
188 }
190 /* Add fields */
191 $field1 = array("string" => preg_replace("/%s/",($key),preg_replace("/%CHECKED%/",$chk,$useCartridge)),
192 "attach" => "style='text-align:center;width:20px;'");
193 $field2 = array("string" => sprintf($edit,($key),$cartr['cartridgeName']." [".$cartr['cartridgeTypeName']."]"),
194 "attach" => "style=''");
195 $field3 = array("string" => sprintf($editdel,($key),($key)),
196 "attach" => "style='width:60px;border-right:0px;text-align:right;'");
198 $divlist->AddEntry(array($field1,$field2,$field3));
200 }
202 /* Create list header */
203 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
204 " <input class='center' type='image' align='middle'
205 src='images/zip.png' title='"._("New monitor")."' alt='"._("M")."' name='newcartridge'> ".
206 "</div>";
208 /* Tell smarty some vars */
209 $filter= $_SESSION['glpiCartridgeRegex'];
210 $smarty->assign("devicehead", $listhead);
211 $smarty->assign("devices", $divlist->DrawList());
212 $smarty->assign("search_image", get_template_path('images/search.png'));
213 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
214 $smarty->assign("tree_image", get_template_path('images/tree.png'));
215 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
216 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
217 $smarty->assign("apply", apply_filter());
218 $smarty->assign("alphabet", generate_alphabet());
219 $smarty->assign("cartridge_regex", $filter);
221 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridges.tpl', TRUE));
222 return($display);
223 }
225 /* Save device to glpi database
226 * If this is a new device, create a new entry, else update this entry
227 */
228 function save()
229 {
230 $carts = $this->getCartridgeTypes();
231 $ret = array();
232 foreach($this->usedCartridges as $key){
233 $tmp =array();
234 $tmp['date_use'] = date("Y-m-d");
235 $tmp['ID'] = -1;
236 $tmp['type_ID'] = $carts[$key]['cartridgeID'];
237 $tmp['name'] = $carts[$key]['cartridgeName'];
238 $tmp['FK_glpi_printers']= -1;
239 $tmp['type_name'] = $carts[$key]['cartridgeTypeName'];
240 $ret[$key] = $tmp;
241 }
242 return($ret);
243 }
245 /* this only gets all already defined devices */
246 function reload()
247 {
248 $this->devices = $this->parent->handle->getDevices();
249 ksort($this->devices);
250 }
252 /* This funtions saves all POST variables.
253 The variable must be in the array $this->EditEntry
254 */
255 function save_object()
256 {
257 /* Checkbox handling
258 * Check which checkbox is selected
259 */
260 foreach($_POST as $name => $value){
261 if(preg_match("/wasOnPage/",$name)){
262 $id = preg_replace("/wasOnPage_/","",$name);
263 if(isset($_POST['useCartridge_'.$id])){
264 $this->usedCartridges[$id]=$id;
265 }else{
266 unset($this->usedCartridges[$id]);
267 }
268 }
269 }
270 }
272 /* This function cehck all created devices if you wan't to create device specific check
273 use >>if($attr['device_type']=="moboard")<< to create a device type depending check
274 */
275 function check($attr)
276 {
277 /* Call common method to give check the hook */
278 $message= plugin::check();
280 return($message);
281 }
283 /* Return cartriges for our divlist */
284 function getCartridgeTypes()
285 {
286 $ret = array();
287 $ret = $this->parent->handle->getAvailableCartridgeTypes($this->PrinterType);
288 return($ret);
289 }
290 }
291 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
292 ?>