Code

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