Code

Update goto import
[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 = 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 = split(";",$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     /* Import file 
63      */
64     if(isset($_POST['import']) && isset($_FILES['file'])) {
65       if(isset($_FILES['file']['tmp_name'])){
66         $str = file_get_contents($_FILES['file']['tmp_name']);
67         $this->parse_csv($str);
68       }
69     }
70     
71     /* Import started 
72      */
73     $confirmed = FALSE;
74     foreach($_POST as $name => $value){ 
75       if(preg_match("/^MSG_OK/",$name)){
76         $confirmed = TRUE;
77       }
78     }
79     if(isset($_POST['start_import']) || $confirmed){
80       $error = FALSE;
81       if(!$confirmed){
82         foreach($this->events as $event){
83           if(!empty($event['ERROR'])){
84             $error = TRUE;
85             break;
86           } 
87         }
88         if($error){
89           msg_dialog::display(_("Import"),
90               _("Selected entries will be skipped because of errors. Do you want to proceed?"),CONFIRM_DIALOG);
91         }
92       }
93       if(!$error){
95         $success = 0;
96         $fail = 0;
98         foreach($this->events as $key => $event){
99           if(!empty($event['ERROR'])){  
100             $fail ++;
101             continue;
102           }
104           /* Create event 
105            */
106           $class= $this->daemon_events['QUEUED'][$event['HEADER']];
107           $o_data = $this->daemon_events['BY_CLASS'][$class];
108           $object = new $class($this->config);
109           $object->add_targets(array($event['MAC']));
110           if($o_data['s_Schedule_Action'] == $event['HEADER']){
111             $object->set_type(SCHEDULED_EVENT);
112           }else{
113             $object->set_type(TRIGGERED_EVENT);
114           }
116           /* Update values like fqdn a.s.o 
117            */
118           foreach($this->csv_fields as $name){
119             if($name == "TIMESTAMP" && empty($event[$name])) continue;
120             $object->set_value($name,$event[$name]);
121           }
123           if(!$this->parent->o_queue->append($object)){
124             msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->parent->o_queue->get_error()),ERROR_DIALOG);
125             $fail ++;
126           }else{
127             unset($this->events[$key]);
128             $success ++;
129           }
130         }
131         msg_dialog::display(_("Import"),sprintf(_("Import complete: %s events successfully send, %s failed"),$success,$fail),INFO_DIALOG);
132         $this->import_successful = count($this->events) == 0;
133       }
134     }
137     /* Prepare output 
138      */
139     $evts = $this->events;
140     foreach($this->events as $id => $evt){
141       foreach($evt as $key => $val){
142         if(in_array($key,$this->csv_fields)){
143           $evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>"; 
144         }
145       }
146     }
147  
148     $smarty = get_smarty();
149     $smarty->assign("info",$evts);
150     $smarty->assign("count",count($evts));
151     return($smarty->fetch(get_template_path('goto_import_file.tpl', TRUE)));
152   }
155   public function check()
156   {
157     $message = plugin::check();
158     $this->check_fields();
159     return($message);
160   }
162   
163   private function check_fields()
164   {
165     foreach($this->events as $key => $event){
166       $this->events[$key]['ERROR'] = "";
167       if(empty($event['MAC']) || !tests::is_mac($event['MAC'])){
168         $this->events[$key]['ERROR'] .=  msgPool::invalid(_("MAC")).", ";
169       }
170       if(empty($event['HEADER']) || !isset($this->daemon_events['QUEUED'][$event['HEADER']])){
171         $this->events[$key]['ERROR'] .=  msgPool::invalid(_("Event")).", ";
172       }
173       $this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'],", ");
174     }
175   } 
176
178 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
179 ?>