a6ff09895d18db89e4bffadff87b3a606550b387
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 function glpiPrinterCartridges ($config, $dn= NULL,$type)
20 {
21 plugin::plugin ($config, $dn);
23 /* Assign some basic settings */
24 $this->ui = get_userinfo();
25 if(!isset($_SESSION['glpiCartridgeRegex'])){
26 $_SESSION['glpiCartridgeRegex'] = "*";
27 }
29 /* Only display cartridges for this type of printer */
30 $this->PrinterType = $type;
31 }
33 function execute()
34 {
35 /* Call parent execute */
36 plugin::execute();
37 $display ="";
38 $smarty = get_smarty();
40 /* Check poted vars and do the requested job, but only once */
41 $only_once = true;
42 foreach($_POST as $name => $value){
44 /* We have to create a new cartridge */
45 if(preg_match("/^newcartridge/",$name)&&($only_once)){
46 echo "new Cartridge";
48 $only_once = false;
49 }
50 }
52 /* Edit cartridge */
53 if(isset($_GET['act'])&&$_GET['act']=="edit_cartridge"){
54 print_a($this->parent->handle->getCartridgeTypeInformations($_GET['id']));
55 echo "edit";
56 }
58 /* remove cartridge */
59 if(isset($_GET['act'])&&$_GET['act']=="del_cartridge"){
60 print_a($this->parent->handle->getCartridgeTypeInformations($_GET['id']));
61 echo "remove";
62 }
64 /* Create divlist */
65 $divlist = new divlist("glpi devices");
66 $divlist->SetEntriesPerPage(0);
67 $divlist->SetHeader(array(
68 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
69 array("string" => _("Cartridges"), "attach" => "style=''"),
70 array("string" => _("Action"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
72 /* Links for editing,adding.... */
73 $edit = "<a href='?plug=".$_GET['plug']."&act=edit_cartridge&id=%s'>%s</a>";
74 $editdel = "<a href='?plug=".$_GET['plug']."&act=edit_cartridge&id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
75 $editdel.= "<a href='?plug=".$_GET['plug']."&act=del_cartridge&id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
77 /* Checkbox for selection of some cartridges */
78 $useCartridge = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useCartridge_%s' %CHECKED%>";
80 /* Add cartridges */
81 $cart = $this->getCartridgeTypes();
82 foreach($cart as $key=>$cartr){
84 /* check if this cartridge is selected */
85 $chk = "";
86 if(isset($this->usedCartridges[$key])){
87 $chk = " checked ";
88 }
90 /* Add fields */
91 $field1 = array("string" => preg_replace("/%s/",($key),preg_replace("/%CHECKED%/",$chk,$useCartridge)), "attach" => "style='text-align:center;width:20px;'");
92 $field2 = array("string" => sprintf($edit,($key),$cartr['cartridgeName']." [".$cartr['cartridgeTypeName']."]"), "attach" => "style=''");
93 $field3 = array("string" => sprintf($editdel,($key),($key)),
94 "attach" => "style='width:60px;border-right:0px;text-align:right;'");
95 $divlist->AddEntry(array($field1,$field2,$field3));
97 }
99 /* Create list header */
100 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
101 " <input class='center' type='image' align='middle'
102 src='images/zip.png' title='"._("New monitor")."' alt='"._("M")."' name='newcartridge'> ".
103 "</div>";
105 /* Tell smarty some vars */
106 $filter= $_SESSION['glpiCartridgeRegex'];
107 $smarty->assign("devicehead", $listhead);
108 $smarty->assign("devices", $divlist->DrawList());
109 $smarty->assign("search_image", get_template_path('images/search.png'));
110 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
111 $smarty->assign("tree_image", get_template_path('images/tree.png'));
112 $smarty->assign("infoimage", get_template_path('images/info.png'));
113 $smarty->assign("launchimage", get_template_path('images/launch.png'));
114 $smarty->assign("apply", apply_filter());
115 $smarty->assign("alphabet", generate_alphabet());
116 $smarty->assign("cartridge_regex", $filter['device_regex']);
118 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridges.tpl', TRUE));
119 return($display);
120 }
122 /* Save device to glpi database
123 * If this is a new device, create a new entry, else update this entry
124 */
125 function save()
126 {
127 $carts = $this->getCartridgeTypes();
128 $ret = array();
129 foreach($this->usedCartridges as $key){
130 $tmp =array();
131 $tmp['date_use'] = date("Y-m-d");
132 $tmp['ID'] = -1;
133 $tmp['type_ID'] = $carts[$key]['cartridgeID'];
134 $tmp['name'] = $carts[$key]['cartridgeName'];
135 $tmp['FK_glpi_printers']= -1;
136 $tmp['type_name'] = $carts[$key]['cartridgeTypeName'];
137 $ret[$key] = $tmp;
138 }
139 return($ret);
140 }
142 /* this only gets all already defined devices */
143 function reload()
144 {
145 $this->devices = $this->parent->handle->getDevices();
146 ksort($this->devices);
147 }
149 /* This funtions saves all POST variables.
150 The variable must be in the array $this->EditEntry
151 */
152 function save_object()
153 {
154 /* Checkbox handling
155 * Check which checkbox is selected
156 */
157 foreach($_POST as $name => $value){
158 if(preg_match("/wasOnPage/",$name)){
159 $id = preg_replace("/wasOnPage_/","",$name);
160 if(isset($_POST['useCartridge_'.$id])){
161 $this->usedCartridges[$id]=$id;
162 }else{
163 unset($this->usedCartridges[$id]);
164 }
165 }
166 }
167 }
169 /* This function cehck all created devices if you wan't to create device specific check
170 use >>if($attr['device_type']=="moboard")<< to create a device type depending check
171 */
172 function check($attr)
173 {
174 $message = array();
175 return($message);
176 }
178 /* Return cartriges for our divlist */
179 function getCartridgeTypes()
180 {
181 $ret = array();
182 $ret = $this->parent->handle->getAvailableCartridgeTypes($this->PrinterType);
183 return($ret);
184 }
185 }
186 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
187 ?>