Code

672f324af87390353837d5facdfe1f89e7ff30ca
[gosa.git] / plugins / admin / systems / class_glpiPrinterCartridges.inc
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     }
35   
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     }
85   
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     }
91  
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       $tmp = $this->parent->handle->is_cartridgeTypeUsed($this->del);
108       if(count($tmp)){
110         $str = "";
111         foreach($tmp as $id => $name){
112           $str .= $name.", "; 
113         }
114         $str = preg_replace("/, $/","",$str);
116         print_red(sprintf(_("You can't delete this cartridge type, it is still in use by this printer(s) '%s'."),$str));
117       }else{
118         $val = ($this->parent->handle->getCartridgeTypeInformations($this->del));
119         $this->cur_dialog = new glpiPrinterCartridgesEdit($this->config,$this->dn,$this->PrinterType,$val[$this->del]); 
120         $this->cur_dialog->parent = $this->parent;
121         $this->cur_dialog->remove_from_parent();
122         $this->cur_dialog= false;
123       } 
124     }
126     /* Abort edit / add dialog */
127     if(isset($_POST['CancelCartridge'])){
128       $this->cur_dialog = false;
129     }
130   
131     /* Save changes if check is ok */
132     if(isset($_POST['SaveCartridge'])){
133       $this->cur_dialog->save_object();
134       if(count($this->cur_dialog->check())) {
135         foreach($this->cur_dialog->check() as $checks){
136           print_red($checks);
137         }
138       }else{
139         $this->cur_dialog->save();
140         $this->cur_dialog = false;
141       }
142     }
144     /* If we have a dialog open, display it */
145     if($this->cur_sub_dialog){
146       $this->cur_sub_dialog->parent = $this->parent;
147       $this->cur_sub_dialog->save_object();
148       return($this->cur_sub_dialog->execute());
149     }
150     /* If we have a dialog open, display it */
151     if($this->cur_dialog){
152       $this->cur_dialog->parent = $this->parent;
153       $this->cur_dialog->save_object();
154       return($this->cur_dialog->execute());
155     }
157     /* Create divlist */
158     $divlist = new divlist("glpi devices");
159     $divlist->SetPluginMode();
160     $divlist->SetEntriesPerPage(0);
161     $divlist->SetHeader(array(
162           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
163           array("string" => _("Cartridges"), "attach" => "style=''"),
164           array("string" => _("Action"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
166     /* Links for editing,adding.... */
167     $edit =     "<a href='?plug=".$_GET['plug']."&amp;act=edit_cartridge&amp;id=%s'>%s</a>";
168     $editdel =  "<a href='?plug=".$_GET['plug']."&amp;act=edit_cartridge&amp;id=%s'><img src='images/edit.png' alt='"._("Edit")."' border=0></a>";
169     $editdel.= "<a href='?plug=".$_GET['plug']."&amp;act=del_cartridge&amp;id=%s'><img src='images/edittrash.png' alt='"._("Delete")."' border=0></a>";
170    
171     /* Checkbox for selection of some cartridges */ 
172     $useCartridge = "<input type='hidden' name='wasOnPage_%s' value='%s'><input type='checkbox' value='%s' name='useCartridge_%s' %CHECKED%>";
174     /* Add cartridges */ 
175     $cart = $this->getCartridgeTypes();
177     /* Remove typically error possibilities */
178     $f = str_replace("/","\/",$filter);
179     $f = str_replace(".","\.",$f);
180     $f = str_replace("*",".*",$f);
182     /* Assign cartridges */
183     foreach($cart as $key=>$cartr){
185       /* Skip if filter doesn't match*/
186       if(!preg_match("/^".$f."$/i",$cartr['cartridgeName'])){
187         continue;
188       }
190       /* check if this cartridge is selected */
191       $chk = "";
192       if(isset($this->usedCartridges[$key])){
193         $chk = " checked ";
194       }
196       /* Add fields */
197       $field1 = array("string" => preg_replace("/%s/",($key),preg_replace("/%CHECKED%/",$chk,$useCartridge)), 
198                       "attach" => "style='text-align:center;width:20px;'");
199       $field2 = array("string" => sprintf($edit,($key),$cartr['cartridgeName']."&nbsp;[".$cartr['cartridgeTypeName']."]"), 
200                       "attach" => "style=''");
201       $field3 = array("string" => sprintf($editdel,($key),($key)), 
202                       "attach" => "style='width:60px;border-right:0px;text-align:right;'");
204       $divlist->AddEntry(array($field1,$field2,$field3));
205       
206     }
207    
208     /* Create list header */ 
209     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
210       " <input class='center' type='image' align='middle' 
211       src='images/zip.png'  title='"._("New monitor")."' alt='"._("M")."' name='newcartridge'>&nbsp;".
212       "</div>";
214     /* Tell smarty some vars */
215     $filter= $_SESSION['glpiCartridgeRegex'];
216     $smarty->assign("devicehead", $listhead);
217     $smarty->assign("devices", $divlist->DrawList());
218     $smarty->assign("search_image", get_template_path('images/search.png'));
219     $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
220     $smarty->assign("tree_image", get_template_path('images/tree.png'));
221     $smarty->assign("infoimage", get_template_path('images/info_small.png'));
222     $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
223     $smarty->assign("apply", apply_filter());
224     $smarty->assign("alphabet", generate_alphabet());
225     $smarty->assign("cartridge_regex", $filter);
227     $display.= $smarty->fetch(get_template_path('glpiPrinterCartridges.tpl', TRUE));
228     return($display);
229   }
231   /* Save device to glpi database 
232    * If this is a new device, create a new entry, else update this entry 
233    */
234   function save()
235   {
236     $carts = $this->getCartridgeTypes();
237     $ret = array();
238     foreach($this->usedCartridges as $key){
239       $tmp =array();
240       $tmp['date_use']        = date("Y-m-d");
241       $tmp['ID']              = -1;
242       $tmp['type_ID']         = $carts[$key]['cartridgeID'];
243       $tmp['name']            = $carts[$key]['cartridgeName'];
244       $tmp['FK_glpi_printers']= -1;
245       $tmp['type_name']       = $carts[$key]['cartridgeTypeName'];
246       $ret[$key] = $tmp;
247     }
248     return($ret);
249   }
251   /* this only gets all already defined devices */
252   function reload()
253   {
254     $this->devices = $this->parent->handle->getDevices();
255     ksort($this->devices);
256   }
258   /* This funtions saves all POST variables.
259      The variable must be in the array $this->EditEntry 
260   */
261   function save_object()
262   {
263     /* Checkbox handling 
264      * Check which checkbox is selected 
265      */
266     foreach($_POST as $name => $value){
267       if(preg_match("/wasOnPage/",$name)){
268         $id = preg_replace("/wasOnPage_/","",$name);
269         if(isset($_POST['useCartridge_'.$id])){
270           $this->usedCartridges[$id]=$id;
271         }else{
272           unset($this->usedCartridges[$id]);
273         }
274       }
275     }
276   }
278   /* This function cehck all created devices if you wan't to create device specific check 
279       use >>if($attr['device_type']=="moboard")<< to create a device type depending check
280    */
281   function check($attr)
282   {
283     /* Call common method to give check the hook */
284     $message= plugin::check();
286     return($message);
287   }
289   /* Return cartriges for our divlist */
290   function getCartridgeTypes()
291   {
292     $ret = array();
293     $ret = $this->parent->handle->getAvailableCartridgeTypes($this->PrinterType);
294     return($ret);
295   }
297 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
298 ?>