59cb03c21d3b707c86535085e93e3364024cb3ab
[gosa.git] / gosa-plugins / glpi / admin / systems / services / glpi / class_glpiPrinterCartridges.inc
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(!session::is_set('glpiCartridgeRegex')){
28 session::set('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::get('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::set('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", msgPool::deleteInfo($val[$this->del]['name'],_("cartridge")));
93 return($smarty->fetch(get_template_path('remove_glpi.tpl',TRUE,dirname(__FILE__))));
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->SetPluginMode();
155 $divlist->SetEntriesPerPage(0);
156 $divlist->SetHeader(array(
157 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
158 array("string" => _("Cartridges"), "attach" => "style=''"),
159 array("string" => _("Action"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
161 /* Links for editing,adding.... */
162 $edit = "<a href='?plug=".$_GET['plug']."&act=edit_cartridge&id=%s'>%s</a>";
163 $editdel = "<a href='?plug=".$_GET['plug']."&act=edit_cartridge&id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
164 $editdel.= "<a href='?plug=".$_GET['plug']."&act=del_cartridge&id=%s'><img src='images/lists/trash.png' alt='"._("Delete")."' border=0></a>";
166 /* Checkbox for selection of some cartridges */
167 $useCartridge = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useCartridge_%s' %CHECKED%>";
169 /* Add cartridges */
170 $cart = $this->getCartridgeTypes();
172 /* Remove typically error possibilities */
173 $f = str_replace("/","\/",$filter);
174 $f = str_replace(".","\.",$f);
175 $f = str_replace("*",".*",$f);
177 /* Assign cartridges */
178 foreach($cart as $key=>$cartr){
180 /* Skip if filter doesn't match*/
181 if(!preg_match("/^".$f."$/i",$cartr['cartridgeName'])){
182 continue;
183 }
185 /* check if this cartridge is selected */
186 $chk = "";
187 if(isset($this->usedCartridges[$key])){
188 $chk = " checked ";
189 }
191 /* Add fields */
192 $field1 = array("string" => preg_replace("/%s/",($key),preg_replace("/%CHECKED%/",$chk,$useCartridge)),
193 "attach" => "style='text-align:center;width:20px;'");
194 $field2 = array("string" => sprintf($edit,($key),$cartr['cartridgeName']." [".$cartr['cartridgeTypeName']."]"),
195 "attach" => "style=''");
196 $field3 = array("string" => sprintf($editdel,($key),($key)),
197 "attach" => "style='width:60px;border-right:0px;text-align:right;'");
199 $divlist->AddEntry(array($field1,$field2,$field3));
201 }
203 /* Create list header */
204 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
205 " <input class='center' type='image' align='middle'
206 src='images/zip.png' title='"._("New monitor")."' alt='"._("M")."' name='newcartridge'> ".
207 "</div>";
209 /* Tell smarty some vars */
210 $filter = session::get('glpiCartridgeRegex');
211 $smarty->assign("devicehead", $listhead);
212 $smarty->assign("devices", $divlist->DrawList());
213 $smarty->assign("search_image", get_template_path('images/search.png'));
214 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
215 $smarty->assign("tree_image", get_template_path('images/tree.png'));
216 $smarty->assign("infoimage", get_template_path('images/info_small.png'));
217 $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
218 $smarty->assign("apply", apply_filter());
219 $smarty->assign("alphabet", generate_alphabet());
220 $smarty->assign("cartridge_regex", $filter);
222 $display.= $smarty->fetch(get_template_path('glpiPrinterCartridges.tpl',TRUE,dirname(__FILE__)));
223 return($display);
224 }
226 /* Save device to glpi database
227 * If this is a new device, create a new entry, else update this entry
228 */
229 function save()
230 {
231 $carts = $this->getCartridgeTypes();
232 $ret = array();
233 foreach($this->usedCartridges as $key){
234 $tmp =array();
235 $tmp['date_use'] = date("Y-m-d");
236 $tmp['ID'] = -1;
237 $tmp['type_ID'] = $carts[$key]['cartridgeID'];
238 $tmp['name'] = $carts[$key]['cartridgeName'];
239 $tmp['FK_glpi_printers']= -1;
240 $tmp['type_name'] = $carts[$key]['cartridgeTypeName'];
241 $ret[$key] = $tmp;
242 }
243 return($ret);
244 }
246 /* this only gets all already defined devices */
247 function reload()
248 {
249 $this->devices = $this->parent->handle->getDevices();
250 ksort($this->devices);
251 }
253 /* This funtions saves all POST variables.
254 The variable must be in the array $this->EditEntry
255 */
256 function save_object()
257 {
258 /* Checkbox handling
259 * Check which checkbox is selected
260 */
261 foreach($_POST as $name => $value){
262 if(preg_match("/wasOnPage/",$name)){
263 $id = preg_replace("/wasOnPage_/","",$name);
264 if(isset($_POST['useCartridge_'.$id])){
265 $this->usedCartridges[$id]=$id;
266 }else{
267 unset($this->usedCartridges[$id]);
268 }
269 }
270 }
271 }
273 /* This function cehck all created devices if you wan't to create device specific check
274 use >>if($attr['device_type']=="moboard")<< to create a device type depending check
275 */
276 function check()
277 {
278 /* Call common method to give check the hook */
279 $message= plugin::check();
281 return($message);
282 }
284 /* Return cartriges for our divlist */
285 function getCartridgeTypes()
286 {
287 $ret = array();
288 $ret = $this->parent->handle->getAvailableCartridgeTypes($this->PrinterType);
289 return($ret);
290 }
291 }
292 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
293 ?>