Code

Updated buttons
[gosa.git] / gosa-plugins / goto / addons / goto / class_goto_import_file.inc
1 <?php
3 class goto_import_file extends plugin
4 {
6   var $events     = array();
7   var $csv_fields = array();
8   var $import_successful = FALSE; // Indicates that we have successfully imported everything.
10   public function __construct($config,&$parent)
11   {
12     plugin::plugin($config,NULL);
13     $this->parent = $parent;
14     $this->daemon_events  = DaemonEvent::get_event_types( SYSTEM_EVENT | HIDDEN_EVENT);
16     $this->csv_fields = array(
17         "0"=>"TIMESTAMP","1" => "MAC",  "2" => "HEADER", "3" => "OGROUP",
18         "4" => "BASE", "5" => "FQDN",   "6" => "IP",     "7" => "DHCP");
19   }
22   private function parse_csv($str)
23   {
24     
25     /* Some file checks 
26      */
27     $lines = preg_split("/\n/",$str);
28     if(empty($str) || !count($lines)){
29       msg_dialog::display(_("Import"), msgPool::incorrectUpload(_("file is empty")),ERROR_DIALOG);
30       return;
31     }
33     /* Reset current events 
34      */
35     $this->events = array(); 
37     /* Parse each line of the given file
38      */
39     foreach($lines as $line){
41       // Skip empty lines.
42       if(empty($line)) continue;
44       /* Load values from file 
45        */
46       $fields = explode(";",$line);
47       $event = array();
48       foreach($this->csv_fields as $key => $val) {
49         $event[$val] = "";
50         if(isset($fields[$key])){
51           $event[$val] = $fields[$key];
52         }
53       }
54       $this->events[] = $event;
55     }
56     $this->check_fields();
57   }
60   public function execute()
61   {
62     plugin::execute();
64     /* Import file 
65      */
66     if(isset($_POST['import']) && isset($_FILES['file'])) {
67       if(isset($_FILES['file']['tmp_name'])){
68         $str = file_get_contents($_FILES['file']['tmp_name']);
69         $this->parse_csv($str);
70       }
71     }
72     
73     /* Import started 
74      */
75     $confirmed = FALSE;
76     foreach($_POST as $name => $value){ 
77       if(preg_match("/^MSG_OK/",$name)){
78         $confirmed = TRUE;
79       }
80     }
81     if(isset($_POST['start_import']) || $confirmed){
82       $error = FALSE;
83       if(!$confirmed){
84         foreach($this->events as $event){
85           if(!empty($event['ERROR'])){
86             $error = TRUE;
87             break;
88           } 
89         }
90         if($error){
91           msg_dialog::display(_("Import"),
92               _("Selected entries will be skipped because of errors. Do you want to proceed?"),CONFIRM_DIALOG);
93         }
94       }
95       if(!$error){
97         $success = 0;
98         $fail = 0;
100         foreach($this->events as $key => $event){
101           if(!empty($event['ERROR'])){  
102             $fail ++;
103             continue;
104           }
106           /* Create event 
107            */
108           $class= $this->daemon_events['QUEUED'][$event['HEADER']];
109           $o_data = $this->daemon_events['BY_CLASS'][$class];
110           $object = new $class($this->config);
111           $object->add_targets(array($event['MAC']));
112           if($o_data['s_Schedule_Action'] == $event['HEADER']){
113             $object->set_type(SCHEDULED_EVENT);
114           }else{
115             $object->set_type(TRIGGERED_EVENT);
116           }
118           /* Update values like fqdn a.s.o 
119            */
120           foreach($this->csv_fields as $name){
121             if($name == "TIMESTAMP" && empty($event[$name])) continue;
122             $object->set_value($name,$event[$name]);
123           }
125           if(!$this->parent->o_queue->append($object)){
126             msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->parent->o_queue->get_error()),ERROR_DIALOG);
127             $fail ++;
128           }else{
129             unset($this->events[$key]);
130             $success ++;
131           }
132         }
133         msg_dialog::display(_("Import"),sprintf(_("Import complete: %s events successfully send, %s failed"),$success,$fail),INFO_DIALOG);
134         $this->import_successful = count($this->events) == 0;
135       }
136     }
139     /* Prepare output 
140      */
141     $evts = $this->events;
142     foreach($this->events as $id => $evt){
143       foreach($evt as $key => $val){
144         if(in_array($key,$this->csv_fields)){
145           $evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>"; 
146         }
147       }
148     }
149  
150     $smarty = get_smarty();
151     $smarty->assign("info",$evts);
152     $smarty->assign("count",count($evts));
153     return($smarty->fetch(get_template_path('goto_import_file.tpl', TRUE)));
154   }
157   public function check()
158   {
159     $message = plugin::check();
160     $this->check_fields();
161     return($message);
162   }
164   
165   private function check_fields()
166   {
167     foreach($this->events as $key => $event){
168       $this->events[$key]['ERROR'] = "";
169       if(empty($event['MAC']) || !tests::is_mac($event['MAC'])){
170         $this->events[$key]['ERROR'] .=  msgPool::invalid(_("MAC")).", ";
171       }
172       if(empty($event['HEADER']) || !isset($this->daemon_events['QUEUED'][$event['HEADER']])){
173         $this->events[$key]['ERROR'] .=  msgPool::invalid(_("Event")).", ";
174       }
175       $this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'],", ");
176     }
177   } 
178
180 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
181 ?>