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