1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2011 Artica Soluciones Tecnologicas
6// Please see http://pandorafms.org for full contribution list
7
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public License
10// as published by the Free Software Foundation; version 2
11
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17/**
18 * @package Include
19 * @subpackage Planned downtimes
20 */
21
22// Load global vars
23global $config;
24
25/**
26 * Include the usual functions
27 */
28require_once($config["homedir"] . "/include/functions_ui.php");
29// enterprise_include_once('include/functions_inventory.php');
30
31function planned_downtimes_check_dates ($type_execution = 'once', $type_periodicity = '', $datetime_from = false, $datetime_to = false, $periodically_time_from = false, $periodically_time_to = false, $periodically_day_from = false, $periodically_day_to = false) {
32	global $config;
33
34	$now = time();
35
36	$result = array(
37			'result' => false,
38			'message' => ''
39		);
40
41	if ($type_execution == 'once' && !$config["past_planned_downtimes"] && $datetime_from < $now) {
42		$result['message'] = ui_print_error_message(__('Not created. Error inserting data. Start time must be higher than the current time' ), '', true);
43	}
44	else if ($type_execution == 'once' && $datetime_from >= $datetime_to) {
45		$result['message'] = ui_print_error_message(__('Not created. Error inserting data') . ". " .__('The end date must be higher than the start date'), '', true);
46	}
47	else if ($type_execution == 'periodically'
48			&& (($type_periodicity == 'weekly' && $periodically_time_from >= $periodically_time_to)
49				|| ($type_periodicity == 'monthly' && $periodically_day_from == $periodically_day_to && $periodically_time_from >= $periodically_time_to))) {
50		$result['message'] = ui_print_error_message(__('Not created. Error inserting data') . ". " .__('The end time must be higher than the start time'), '', true);
51	}
52	else if ($type_execution == 'periodically' && $type_periodicity == 'monthly' && $periodically_day_from > $periodically_day_to) {
53		$result['message'] = ui_print_error_message(__('Not created. Error inserting data') . ". " .__('The end day must be higher than the start day'), '', true);
54	}
55	else {
56		$result['result'] = true;
57	}
58
59	return $result;
60}
61
62/**
63 * Update or create a planned downtime.
64 *
65 * @param array Values of the planned downtime.
66 * @param int Id of the planned downtime. Empty to create a new downtime.
67 *
68 * @return array Id of the updated/created planned downtime, result of the operation and result message.
69 */
70function planned_downtimes_update ($values, $downtime_id = 0, $check_dates = true) {
71	$result = array(
72			'id' => $downtime_id,
73			'result' => false,
74			'message' => ''
75		);
76
77	if ($check_dates) {
78		$dates_check = planned_downtimes_check_dates($values['type_execution'], $values['type_periodicity'],
79			$values['date_from'], $values['date_to'], $values['periodically_time_from'], $values['periodically_time_to'],
80			$values['periodically_day_from'], $values['periodically_day_to']);
81
82		if (!$dates_check['result']) {
83			$result['message'] = $dates_check['message'];
84
85			return $result;
86		}
87	}
88
89	$name_trimmed = trim(io_safe_output($values['name']));
90	if (!empty($name_trimmed)) {
91		$name_exists = (bool) db_get_value('id', 'tplanned_downtime', 'name', $values['name']);
92
93		if ($name_exists) {
94			$result['message'] = ui_print_error_message(__('Each planned downtime must have a different name'), '', true);
95
96			return $result;
97		}
98	}
99	else {
100		$result['message'] = ui_print_error_message(__('Planned downtime must have a name'), '', true);
101
102		return $result;
103	}
104
105
106	if (empty($downtime_id)) {
107		$res = db_process_sql_insert('tplanned_downtime', $values);
108
109		if ($res === false) {
110			$result['message'] = ui_print_error_message(__('Could not be created'), '', true);
111		}
112		else {
113			$result['message'] = ui_print_success_message(__('Successfully created'), '', true);
114			$result['result'] = true;
115			$result['id'] = $res;
116		}
117	}
118	else {
119		$res = db_process_sql_update('tplanned_downtime', $values, array('id' => $downtime_id));
120
121		if (empty($res)) {
122			$result['message'] = ui_print_error_message(__('Could not be updated'), '', true);
123		}
124		else {
125			$result['message'] = ui_print_success_message(__('Successfully updated'), '', true);
126			$result['result'] = true;
127		}
128	}
129
130
131	return $result;
132}
133
134/**
135 * Add new agents and modules to the planned downtime.
136 *
137 * @param int Id of the planned downtime.
138 * @param array IDs of the agents to add.
139 * @param bool Add all modules of the agents or not.
140 * @param array Names of the modules to add. Empty to add all the modules.
141 *
142 * @return array The status will be false id any insertion fails.
143 * The failed insertions will be added to bad_agents and bad_modules.
144 */
145function planned_downtimes_add_items ($downtime_id, $agents, $all_modules = true, $module_names = array()) {
146	global $config;
147
148	include_once($config['homedir'] . "/include/functions_modules.php");
149
150	$return = array(
151			'status' => true,
152			'bad_agents' => array(),
153			'bad_modules' => array()
154		);
155
156	if (empty($module_names)) {
157		$all_modules = true;
158	}
159	else {
160		//It is empty.
161		if ($module_names[0] == "0")
162			$all_modules = true;
163	}
164
165	if (empty($agents)) {
166		$agents = array();
167		$return['status'] = false;
168	}
169
170	foreach ($agents as $agent_id) {
171
172		$values = array(
173			'id_downtime' => $downtime_id,
174			'id_agent' => $agent_id,
175			'all_modules' => $all_modules
176		);
177
178		$result = db_process_sql_insert('tplanned_downtime_agents', $values);
179
180		if (empty($result)) {
181			$return['bad_agents'][] = $agent_id;
182		}
183		else if (!$all_modules) {
184			foreach ($module_names as $module_name) {
185
186				$module = modules_get_agentmodule_id(io_safe_input($module_name), $agent_id);
187				$result = false;
188
189				if ($module) {
190
191					$module_id = $module["id_agente_modulo"];
192
193					$values = array(
194							'id_downtime' => $downtime_id,
195							'id_agent' => $agent_id,
196							'id_agent_module' => $module_id
197						);
198					$result = db_process_sql_insert('tplanned_downtime_modules', $values);
199				}
200
201				if (!$result) {
202					$return['bad_modules'][] = $module_name;
203				}
204			}
205		}
206	}
207
208	return $return;
209}
210
211/**
212 * Delete the agents and modules asociated to a planned downtime.
213 *
214 * @param int Id of the planned downtime.
215 * @param int ID of the agent to delete.
216 *
217 * @return bool The result of the operation.
218 */
219function planned_downtimes_delete_items ($downtime_id, $agent_id) {
220	$filter = array(
221				'id_downtime' => $downtime_id,
222				'id_agent' => $agent_id
223			);
224	$downtime_agent_row = db_get_row('tplanned_downtime_agents', $filter);
225
226	$result = db_process_sql_delete('tplanned_downtime_agents', array('id' => $downtime_agent_row['id']));
227
228	if (!empty($result) && $downtime_agent_row['all_modules'] == 0) {
229		//Delete modules in downtime
230		$filter = array(
231				'id_downtime' => $downtime_id,
232				'id_agent' => $agent_id
233			);
234		db_process_sql_delete('tplanned_downtime_modules', $filter);
235	}
236
237	return $result;
238}
239
240/**
241 * Get the planned downtimes that don't comply the actual rules of dates
242 *
243 * @return array List of planned downtimes
244 */
245function planned_downtimes_get_malformed () {
246
247	$sql = "SELECT *
248			FROM tplanned_downtime
249			WHERE type_execution = 'periodically'
250				AND ((type_periodicity = 'monthly'
251						AND (periodically_day_from > periodically_day_to
252							OR (periodically_day_from = periodically_day_to
253								AND periodically_time_from >= periodically_time_to)))
254					OR (type_periodicity = 'weekly'
255						AND periodically_time_from >= periodically_time_to))";
256	$malformed_downtimes = db_get_all_rows_sql($sql);
257
258	if ($malformed_downtimes === false) {
259		return false;
260	}
261
262	$malformed_downtimes_aux = array();
263	foreach ($malformed_downtimes as $malformed_downtime) {
264		$malformed_downtimes_aux[$malformed_downtime['id']] = $malformed_downtime;
265	}
266	$malformed_downtimes = $malformed_downtimes_aux;
267
268	return $malformed_downtimes;
269}
270
271/**
272 * Create new downtimes with the correct format and delete the malformed
273 *
274 * @param array List with the malformed downtimes
275 *
276 * @return bool The result of the migration
277 */
278function planned_downtimes_migrate_malformed_downtimes ($malformed_downtimes = array()) {
279	global $config;
280
281	$migration_result = array(
282			'status' => true,
283			'bad_downtimes' => array()
284		);
285
286	if (empty($malformed_downtimes))
287		$malformed_downtimes = planned_downtimes_get_malformed();
288
289	foreach ($malformed_downtimes as $key => $downtime) {
290		$downtime_type = $downtime['type_downtime'];
291		$downtime_execution = $downtime['type_execution'];
292		$downtime_periodicity = $downtime['type_periodicity'];
293
294		$downtime_time_from = $downtime['periodically_time_from'];
295		$downtime_time_to = $downtime['periodically_time_to'];
296
297		if ($downtime_execution == 'periodically') {
298			if ($downtime_periodicity == 'monthly') {
299				$downtime_day_from = $downtime['periodically_day_from'];
300				$downtime_day_to = $downtime['periodically_day_to'];
301
302				if (($downtime_day_from > $downtime_day_to) || ($downtime_day_from == $downtime_day_to && $downtime_time_from > $downtime_time_to)) {
303					$values_first = array(
304							'name' => $downtime['name'] . "&nbsp;[1/2]",
305							'description' => $downtime['description'],
306							'executed' => $downtime['executed'],
307							'id_group' => $downtime['id_group'],
308							'only_alerts' => $downtime['only_alerts'],
309							'periodically_time_from' => $downtime_time_from,
310							'periodically_time_to' => "23:59:59",
311							'periodically_day_from' => $downtime_day_from,
312							'periodically_day_to' => 31,
313							'type_downtime' => $downtime_type,
314							'type_execution' => $downtime_execution,
315							'type_periodicity' => $downtime_periodicity,
316							'id_user' => $config['id_user']
317						);
318					$values_second = $values_first;
319					$values_second['name'] = $downtime['name'] . "&nbsp;[2/2]";
320					$values_second['periodically_day_from'] = 1;
321					$values_second['periodically_time_from'] = "00:00:00";
322					$values_second['periodically_day_to'] = $downtime_day_to;
323					$values_second['periodically_time_to'] = $downtime_time_to;
324
325					$result_first = planned_downtimes_update($values_first, 0, false);
326
327					if ($result_first['result']) {
328						$result_second = planned_downtimes_update($values_second, 0, false);
329
330						if (!$result_second['result']) {
331							db_process_sql_delete('tplanned_downtime', array('id' => $result_first['id']));
332						}
333					}
334
335					if ($result_first['result'] && $result_second['result']) {
336						$result_copy = planned_downtimes_migrate_malformed_downtimes_copy_items($downtime['id'], $result_first['id'], $result_second['id']);
337
338						if (!$result_copy) {
339							$migration_result['status'] = false;
340							$migration_result['bad_downtimes'][] = $downtime['id'];
341						}
342					}
343					else {
344						$migration_result['status'] = false;
345						$migration_result['bad_downtimes'][] = $downtime['id'];
346					}
347				}
348				else if ($downtime_day_from == $downtime_day_to && $downtime_time_from == $downtime_time_to) {
349
350					$utimestamp = strtotime("1-1-1970 $downtime_time_to");
351					$time = date('H:i:s', $utimestamp + 1);
352
353					if ($time != '00:00:00') {
354						$values = array('periodically_time_to' => $time);
355					}
356					else {
357						$utimestamp = strtotime("1-1-1970 $downtime_time_from");
358						$time = date('H:i:s', $utimestamp - 1);
359						$values = array('periodically_time_from' => $time);
360					}
361
362					$filter = array('id' => $downtime['id']);
363					$result_update = db_process_sql_update('tplanned_downtime', $values, $filter);
364
365					if (!$result_update) {
366						$migration_result['status'] = false;
367						$migration_result['bad_downtimes'][] = $downtime['id'];
368					}
369				}
370			}
371			else if ($downtime_periodicity == 'weekly') {
372				if ($downtime_time_from > $downtime_time_to) {
373					$days = array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
374
375					$values_first = array(
376							'name' => $downtime['name'] . "&nbsp;[1/2]",
377							'description' => $downtime['description'],
378							'executed' => $downtime['executed'],
379							'id_group' => $downtime['id_group'],
380							'only_alerts' => $downtime['only_alerts'],
381							'monday' => $downtime['monday'],
382							'tuesday' => $downtime['tuesday'],
383							'wednesday' => $downtime['wednesday'],
384							'thursday' => $downtime['thursday'],
385							'friday' => $downtime['friday'],
386							'saturday' => $downtime['saturday'],
387							'sunday' => $downtime['sunday'],
388							'periodically_time_from' => $downtime_time_from,
389							'periodically_time_to' => "23:59:59",
390							'type_downtime' => $downtime_type,
391							'type_execution' => $downtime_execution,
392							'type_periodicity' => $downtime_periodicity,
393							'id_user' => $config['id_user']
394						);
395
396					$values_second = $values_first;
397					$values_second['name'] = $downtime['name'] . "&nbsp;[2/2]";
398					$values_second['periodically_time_from'] = "00:00:00";
399					$values_second['periodically_time_to'] = $downtime_time_to;
400					$values_second['tuesday'] = $downtime['monday'] ? 1 : 0;
401					$values_second['wednesday'] = $downtime['tuesday'] ? 1 : 0;
402					$values_second['thursday'] = $downtime['wednesday'] ? 1 : 0;
403					$values_second['friday'] = $downtime['thursday'] ? 1 : 0;
404					$values_second['saturday'] = $downtime['friday'] ? 1 : 0;
405					$values_second['sunday'] = $downtime['saturday'] ? 1 : 0;
406					$values_second['monday'] = $downtime['sunday'] ? 1 : 0;
407
408					$result_first = planned_downtimes_update($values_first, 0, false);
409
410					if ($result_first['result']) {
411						$result_second = planned_downtimes_update($values_second, 0, false);
412
413						if (!$result_second['result']) {
414							db_process_sql_delete('tplanned_downtime', array('id' => $result_first['id']));
415						}
416					}
417
418					if ($result_first['result'] && $result_second['result']) {
419						$result_copy = planned_downtimes_migrate_malformed_downtimes_copy_items($downtime['id'], $result_first['id'], $result_second['id']);
420
421						if (!$result_copy) {
422							$migration_result['status'] = false;
423							$migration_result['bad_downtimes'][] = $downtime['id'];
424						}
425					}
426					else {
427						$migration_result['status'] = false;
428						$migration_result['bad_downtimes'][] = $downtime['id'];
429					}
430				}
431				else if ($downtime_time_from == $downtime_time_to) {
432					$utimestamp = strtotime("1-1-1970 $downtime_time_to");
433					$time = date('H:i:s', $utimestamp + 1);
434
435					if ($time != '00:00:00') {
436						$values = array('periodically_time_to' => $time);
437					}
438					else {
439						$utimestamp = strtotime("1-1-1970 $downtime_time_from");
440						$time = date('H:i:s', $utimestamp - 1);
441						$values = array('periodically_time_from' => $time);
442					}
443
444					$filter = array('id' => $downtime['id']);
445					$result_update = db_process_sql_update('tplanned_downtime', $values, $filter);
446
447					if (!$result_update) {
448						$migration_result['status'] = false;
449						$migration_result['bad_downtimes'][] = $downtime['id'];
450					}
451				}
452			}
453		}
454	}
455
456	return $migration_result;
457}
458
459/**
460 * Aux function to copy the items of the selected downtime to the new downtimes.
461 * Deletes the new downtimes if the items are not copied.
462 */
463function planned_downtimes_migrate_malformed_downtimes_copy_items ($original_downtime_id, $new_downtime_first_id, $new_downtime_second_id) {
464	$sql = "SELECT *
465			FROM tplanned_downtime_agents
466			WHERE id_downtime = " . $original_downtime_id;
467	$downtime_agents_rows = db_get_all_rows_sql($sql);
468	$sql = "SELECT *
469			FROM tplanned_downtime_modules
470			WHERE id_downtime = " . $original_downtime_id;
471	$downtime_modules_rows = db_get_all_rows_sql($sql);
472
473	if (!empty($downtime_agents_rows)) {
474		$result_agents = true;
475
476		foreach ($downtime_agents_rows as $downtime_agents_row) {
477			$values_agent = array(
478					'id_agent' => $downtime_agents_row['id_agent'],
479					'all_modules' => $downtime_agents_row['all_modules']
480				);
481			$values_agent['id_downtime'] = $new_downtime_first_id;
482			$result_agent_first = db_process_sql_insert('tplanned_downtime_agents', $values_agent);
483			$values_agent['id_downtime'] = $new_downtime_second_id;
484			$result_agent_second = db_process_sql_insert('tplanned_downtime_agents', $values_agent);
485
486			if (empty($result_agent_first) || empty($result_agent_second)) {
487				$result_agents = false;
488				db_process_sql_delete('tplanned_downtime', array('id' => $new_downtime_first_id));
489				db_process_sql_delete('tplanned_downtime', array('id' => $new_downtime_second_id));
490				break;
491			}
492		}
493
494		if ($result_agents) {
495			if (!empty($downtime_modules_rows)) {
496				foreach ($downtime_modules_rows as $downtime_modules_row) {
497					$values_module = array(
498							'id_agent' => $downtime_modules_row['id_agent'],
499							'id_agent_module' => $downtime_modules_row['id_agent_module']
500						);
501					$values_module['id_downtime'] = $new_downtime_first_id;
502					$result_module_first = db_process_sql_insert('tplanned_downtime_modules', $values_module);
503					$values_module['id_downtime'] = $new_downtime_second_id;
504					$result_module_second = db_process_sql_insert('tplanned_downtime_modules', $values_module);
505
506					if (empty($result_module_first) || empty($result_module_second)) {
507						db_process_sql_delete('tplanned_downtime', array('id' => $new_downtime_first_id));
508						db_process_sql_delete('tplanned_downtime', array('id' => $new_downtime_second_id));
509						break;
510					}
511				}
512			}
513		}
514	}
515
516	// The new downtimes are created
517	$new_planned_downtimes_exists = (bool) db_get_value('id', 'tplanned_downtime', 'id', $new_downtime_first_id)
518		&& (bool) db_get_value('id', 'tplanned_downtime', 'id', $new_downtime_second_id);
519
520	if ($new_planned_downtimes_exists) {
521		// Delete the migrated downtime and its items
522		db_process_sql_delete('tplanned_downtime', array('id' => $original_downtime_id));
523	}
524
525	return $new_planned_downtimes_exists;
526}
527
528/**
529 * Stop a planned downtime.
530 *
531 * @param array Planned downtime data.
532 *
533 * @return mixes False on error or an array with the result and a message of the operation.
534 */
535function planned_downtimes_stop ($downtime) {
536	$result = false;
537	$message = '';
538
539	if (empty($downtime))
540		return false;
541
542	$id_downtime = $downtime['id'];
543
544	switch ($downtime['type_execution']) {
545		case 'once':
546			$values = array(
547					'executed' => 0,
548					'date_to' => time()
549				);
550
551			$result = db_process_sql_update('tplanned_downtime',
552				$values, array ('id' => $id_downtime));
553			break;
554		case 'periodically':
555			return false;
556			break;
557	}
558
559	$message .= ui_print_result_message($result,
560		__('Succesful stopped the Downtime'),
561		__('Unsuccesful stopped the Downtime'),
562		true);
563
564	if ($result) {
565		events_create_event ("Manual stop downtime  ".
566			$downtime['name'] . " (" . $downtime['id'] . ") by " .
567			$config['id_user'], 0, 0, EVENT_STATUS_NEW, $config["id_user"],
568			"system", 1);
569		db_pandora_audit("Planned Downtime management",
570			"Manual stop downtime " . $downtime['name'] . " (ID " . $downtime['id'] . ")",
571			false, true);
572
573		//Reenabled the Agents or Modules or alerts...depends of type
574		switch ($downtime['type_downtime']) {
575			case 'quiet':
576				$agents = db_get_all_rows_filter(
577					'tplanned_downtime_agents',
578					array('id_downtime' => $id_downtime));
579				if (empty($agents))
580					$agents = array();
581
582				$count = 0;
583				foreach ($agents as $agent) {
584					if ($agent['all_modules']) {
585						$result = db_process_sql_update('tagente',
586							array('quiet' => 0),
587							array('id_agente' => $agent['id_agent']));
588
589						if ($result)
590							$count++;
591					}
592					else {
593						$modules = db_get_all_rows_filter(
594							'tplanned_downtime_modules',
595							array('id_agent' => $agent['id_agent'],
596								'id_downtime' => $id_downtime));
597						if (empty($modules))
598							$modules = array();
599
600						foreach ($modules as $module) {
601							$result = db_process_sql_update(
602								'tagente_modulo',
603								array('quiet' => 0),
604								array('id_agente_modulo' =>
605									$module['id_agent_module']));
606
607							if ($result)
608								$count++;
609						}
610					}
611				}
612				break;
613			case 'disable_agents':
614				$agents = db_get_all_rows_filter(
615					'tplanned_downtime_agents',
616					array('id_downtime' => $id_downtime));
617				if (empty($agents))
618					$agents = array();
619
620				$count = 0;
621				foreach ($agents as $agent) {
622					$result = db_process_sql_update('tagente',
623						array('disabled' => 0),
624						array('id_agente' => $agent['id_agent']));
625
626					if ($result)
627						$count++;
628				}
629				break;
630			case 'disable_agents_alerts':
631				$agents = db_get_all_rows_filter(
632					'tplanned_downtime_agents',
633					array('id_downtime' => $id_downtime));
634				if (empty($agents))
635					$agents = array();
636
637				$count = 0;
638				foreach ($agents as $agent) {
639					$modules = db_get_all_rows_filter(
640						'tagente_modulo',
641						array('id_agente' => $agent['id_agent']));
642					if (empty($modules))
643						$modules = array();
644
645					foreach ($modules as $module) {
646						$result = db_process_sql_update(
647							'talert_template_modules',
648							array('disabled' => 0),
649							array('id_agent_module' =>
650								$module['id_agente_modulo']));
651
652						if ($result)
653							$count++;
654					}
655				}
656				break;
657		}
658
659		$message .= ui_print_info_message(
660			sprintf(__('Enabled %s elements from the downtime'), $count), true);
661	}
662
663	return array('result' => $result, 'message' => $message);
664}
665
666function planned_downtimes_created ($values) {
667	global $config;
668
669	$check = (bool) db_get_value ('name', 'tplanned_downtime', 'name', $values['name']);
670
671	$datetime_from = strtotime ($values['once_date_from'] . ' ' . $values['once_time_from']);
672	$datetime_to = strtotime ($values['once_date_to'] . ' ' . $values['once_time_to']);
673	$now = time();
674	$result = false;
675
676	if ($values['type_execution'] == 'once' && !$config["past_planned_downtimes"] && $values['date_from'] < $now) {
677		return array('return' => false,
678			'message' => __('Not created. Error inserting data. Start time must be higher than the current time'));
679	}
680	else if ($values['type_execution'] == 'once' && $values['date_from'] >= $values['date_to']) {
681		return array('return' => false,
682			'message' => __('Not created. Error inserting data') . ". "
683				. __('The end date must be higher than the start date'));
684	}
685	else if ($values['type_execution'] == 'once' && $values['date_to'] <= $now) {
686		return array('return' => false,
687			'message' => __('Not created. Error inserting data') . ". "
688				. __('The end date must be higher than the current time'));
689	}
690	else if ($values['type_execution'] == 'periodically'
691			&& (($values['type_periodicity'] == 'weekly' && $values['periodically_time_from'] >= $values['periodically_time_to'])
692				|| ($values['type_periodicity'] == 'monthly' && $values['periodically_day_from'] ==
693					$values['periodically_day_to'] && $values['periodically_time_from'] >= $values['periodically_time_to']))) {
694		return array('return' => false,
695			'message' => __('Not created. Error inserting data') . ". "
696				. __('The end time must be higher than the start time'));
697	}
698	else if ($values['type_execution'] == 'periodically' &&
699				$values['type_periodicity'] == 'monthly' &&
700					$values['periodically_day_from'] > $values['periodically_day_to']) {
701		return array('return' => false,
702			'message' => __('Not created. Error inserting data') . ". "
703				. __('The end day must be higher than the start day'));
704	}
705	else {
706		if (trim(io_safe_output($values['name'])) != '') {
707			if (!$check) {
708				if ($config["dbtype"] == 'oracle') {
709					$values['periodically_time_from'] = '1970/01/01 ' . $values['periodically_time_from'];
710					$values['periodically_time_to'] = '1970/01/01 ' . $values['periodically_time_to'];
711				}
712
713				$result = db_process_sql_insert('tplanned_downtime', $values);
714			}
715			else {
716				return array('return' => false,
717					'message' => __('Each planned downtime must have a different name'));
718			}
719		}
720		else {
721			return array('return' => false,
722				'message' => __('Planned downtime must have a name'));
723		}
724
725		if ($result === false) {
726			return array('return' => false,
727				'message' => __('Could not be created'));
728		}
729		else {
730			return array('return' => $result,
731				'message' => __('Successfully created'));
732		}
733	}
734
735	return $return;
736}
737
738
739function all_planned_downtimes ($filter) {
740
741	$fields = 'id, name, description, date_from, date_to, id_group, monday,
742		tuesday, wednesday, thursday, friday, saturday, sunday,
743		periodically_time_from, periodically_time_to, periodically_day_from,
744		periodically_day_to, type_downtime, type_execution, type_periodicity, id_user';
745
746	$result = db_get_all_rows_filter('tplanned_downtime',$filter, $fields);
747
748	return $result;
749}
750
751function planned_downtimes_items ($filter) {
752
753	$downtime_agents = db_get_all_rows_filter('tplanned_downtime_agents',$filter, 'id_agent,id_downtime,all_modules');
754	$downtime = db_get_row_filter('tplanned_downtime',array('id' => $filter['id_downtime']), 'type_downtime');
755
756	foreach ( $downtime_agents as $key => $data ) {
757		$return = $data;
758		$modules = array();
759		if ($downtime['type_downtime'] === 'quiet') {
760			if (!$data['all_modules']) {
761				$second_filter = array(
762					'id_agent' => $data['id_agent'],
763					'id_downtime' => $data['id_downtime']);
764
765				$downtime_modules = db_get_all_rows_filter('tplanned_downtime_modules',$second_filter, 'id_agent_module');
766				if ( $downtime_modules ) {
767					foreach ( $downtime_modules as $data2 ) {
768						$modules[] = $data2['id_agent_module'];
769					}
770					$return['modules'] = implode(',', $modules);
771				}
772			}
773		}
774	}
775
776	return $return;
777}
778
779function delete_planned_downtimes ($filter) {
780
781
782	$downtime_execute = db_get_row_filter('tplanned_downtime',array('id' => $filter['id_downtime']), 'execute');
783
784	if ( $downtime_execute )
785		$return = __("This planned downtime are executed now. Can't delete in this moment.");
786	else {
787		$delete = db_process_sql_delete ('tplanned_downtime',
788					array('id'=>$filter['id_downtime']));
789		if ($delete)
790			$return = __("Deleted this planned downtime successfully.");
791		else
792			$return = __("Problems for deleted this planned downtime.");
793	}
794
795	return $return;
796}
797?>