Some more cleanup - remove unused functions, formatting and some IDE warnings

This commit is contained in:
nathangray 2016-05-17 09:03:30 -06:00
parent 82737f47e1
commit 2675da8e54

View File

@ -29,16 +29,19 @@ class importexport_schedule_ui
protected static $template; protected static $template;
public function __construct() { public function __construct()
{
$this->template = new Etemplate(); $this->template = new Etemplate();
} }
public function index($content = array()) { public function index($content = array())
{
if(is_array($content['scheduled'])) if(is_array($content['scheduled']))
{ {
foreach($content['scheduled'] as $row) foreach($content['scheduled'] as $row)
{ {
if($row['delete']) { if($row['delete'])
{
$key = urldecode(key($row['delete'])); $key = urldecode(key($row['delete']));
ExecMethod('phpgwapi.asyncservice.cancel_timer', $key); ExecMethod('phpgwapi.asyncservice.cancel_timer', $key);
} }
@ -46,8 +49,10 @@ class importexport_schedule_ui
} }
$async_list = ExecMethod('phpgwapi.asyncservice.read', 'importexport%'); $async_list = ExecMethod('phpgwapi.asyncservice.read', 'importexport%');
$data = array(); $data = array();
if(is_array($async_list)) { if(is_array($async_list))
foreach($async_list as $id => $async) { {
foreach($async_list as $id => $async)
{
foreach(array('errors', 'warnings', 'result') as $messages) foreach(array('errors', 'warnings', 'result') as $messages)
{ {
if(is_array($async['data'][$messages])) if(is_array($async['data'][$messages]))
@ -63,7 +68,8 @@ class importexport_schedule_ui
$async['data'][$messages] = $list; $async['data'][$messages] = $list;
} }
} }
if(is_numeric($async['data']['record_count'])) { if(is_numeric($async['data']['record_count']))
{
$async['data']['record_count'] = lang('%1 records processed', $async['data']['record_count']); $async['data']['record_count'] = lang('%1 records processed', $async['data']['record_count']);
} }
$data['scheduled'][] = array_merge($async['data'], array( $data['scheduled'][] = array_merge($async['data'], array(
@ -82,7 +88,8 @@ class importexport_schedule_ui
$this->template->exec('importexport.importexport_schedule_ui.index', $data, $sel_options); $this->template->exec('importexport.importexport_schedule_ui.index', $data, $sel_options);
} }
public function edit($content = array()) { public function edit($content = array())
{
$id = $_GET['id'] ? urldecode($_GET['id']) : $content['id']; $id = $_GET['id'] ? urldecode($_GET['id']) : $content['id'];
$definition_id = $_GET['definition']; $definition_id = $_GET['definition'];
$async = new Api\Asyncservice(); $async = new Api\Asyncservice();
@ -92,7 +99,8 @@ class importexport_schedule_ui
$data = $content; $data = $content;
// Deal with incoming // Deal with incoming
if($content['save'] && self::check_target($content) === true) { if($content['save'] && self::check_target($content) === true)
{
unset($content['save']); unset($content['save']);
$async->cancel_timer($id); $async->cancel_timer($id);
$id = self::generate_id($content); $id = self::generate_id($content);
@ -102,7 +110,8 @@ class importexport_schedule_ui
unset($content['schedule']); unset($content['schedule']);
// Remove any left blank // Remove any left blank
foreach($schedule as $key => &$value) { foreach($schedule as $key => &$value)
{
if($value == '') unset($schedule[$key]); if($value == '') unset($schedule[$key]);
} }
$result = $async->set_timer( $result = $async->set_timer(
@ -111,35 +120,50 @@ class importexport_schedule_ui
'importexport.importexport_schedule_ui.exec', 'importexport.importexport_schedule_ui.exec',
$content $content
); );
if($result) { if($result)
{
Framework::refresh_opener('', 'admin',$id,'update','admin'); Framework::refresh_opener('', 'admin',$id,'update','admin');
Framework::window_close(); Framework::window_close();
} else { }
else
{
$data['message'] = lang('Unable to schedule'); $data['message'] = lang('Unable to schedule');
unset($id); unset($id);
} }
} }
if($id) { if($id)
{
$preserve['id'] = $id; $preserve['id'] = $id;
$async = $async->read($id); $async = $async->read($id);
if(is_array($async[$id]['data'])) { if(is_array($async[$id]['data']))
{
$data += $async[$id]['data']; $data += $async[$id]['data'];
$data['schedule'] = $async[$id]['times']; $data['schedule'] = $async[$id]['times'];
unset($data['times']); unset($data['times']);
// Async sometimes changes minutes to an array - show user what they typed // Async sometimes changes minutes to an array - show user what they typed
if(is_array($data['schedule']['min'])) $data['schedule']['min'] = $data['min']; if(is_array($data['schedule']['min']))
} else { {
$data['schedule']['min'] = $data['min'];
}
}
else
{
$data['message'] = lang('Schedule not found'); $data['message'] = lang('Schedule not found');
} }
} else { }
else
{
$data['type'] = $content['type'] ? $content['type'] : 'import'; $data['type'] = $content['type'] ? $content['type'] : 'import';
if((int)$definition_id) { if((int)$definition_id)
{
$bo = new importexport_definitions_bo(); $bo = new importexport_definitions_bo();
$definition = $bo->read($definition_id); $definition = $bo->read($definition_id);
if($definition['definition_id']) { if($definition['definition_id'])
{
$data['type'] = $definition['type']; $data['type'] = $definition['type'];
$data['appname'] = $definition['application']; $data['appname'] = $definition['application'];
$data['plugin'] = $definition['plugin']; $data['plugin'] = $definition['plugin'];
@ -148,9 +172,13 @@ class importexport_schedule_ui
} }
} }
if($data['target'] && $data['type']) { if($data['target'] && $data['type'])
{
$file_check = self::check_target($data); $file_check = self::check_target($data);
if($file_check !== true) $data['message'] .= ($data['message'] ? "\n" . $file_check : $file_check); if($file_check !== true)
{
$data['message'] .= ($data['message'] ? "\n" . $file_check : $file_check);
}
} }
$data['no_delete_files'] = $data['type'] != 'import'; $data['no_delete_files'] = $data['type'] != 'import';
@ -169,7 +197,8 @@ class importexport_schedule_ui
/** /**
* Get options for select boxes * Get options for select boxes
*/ */
public static function get_select_options(Array $data) { public static function get_select_options(Array $data)
{
$options = array( $options = array(
'type' => array( 'type' => array(
'import' => lang('import'), 'import' => lang('import'),
@ -178,15 +207,22 @@ class importexport_schedule_ui
); );
(array)$apps = importexport_helper_functions::get_apps($data['type'] ? $data['type'] : 'all'); (array)$apps = importexport_helper_functions::get_apps($data['type'] ? $data['type'] : 'all');
if(count($apps)) { if(count($apps))
{
$options['appname'] = array('' => lang('Select one')) + array_combine($apps,$apps); $options['appname'] = array('' => lang('Select one')) + array_combine($apps,$apps);
} }
$plugins = importexport_helper_functions::get_plugins($data['appname'] ? $data['appname'] : 'all', $data['type']); $plugins = importexport_helper_functions::get_plugins($data['appname'] ? $data['appname'] : 'all', $data['type']);
if(is_array($plugins)) { if(is_array($plugins))
foreach($plugins as $types) { {
if(!is_array($types[$data['type']])) continue; foreach($plugins as $types)
foreach($types[$data['type']] as $key => $title) { {
if(!is_array($types[$data['type']]))
{
continue;
}
foreach($types[$data['type']] as $key => $title)
{
$options['plugin'][$key] = $title; $options['plugin'][$key] = $title;
} }
} }
@ -200,15 +236,20 @@ class importexport_schedule_ui
$query['application'] = $data['application']; $query['application'] = $data['application'];
$query['plugin'] = $data['plugin']; $query['plugin'] = $data['plugin'];
$definitions = new importexport_definitions_bo($query); $definitions = new importexport_definitions_bo($query);
foreach ((array)$definitions->get_definitions() as $identifier) { foreach ((array)$definitions->get_definitions() as $identifier)
try { {
try
{
$definition = new importexport_definition($identifier); $definition = new importexport_definition($identifier);
} catch (Exception $e) { }
catch (Exception $e)
{
unset($e); unset($e);
// permission error // permission error
continue; continue;
} }
if (($title = $definition->get_title())) { if (($title = $definition->get_title()))
{
$options['definition'][$title] = $title; $options['definition'][$title] = $title;
} }
unset($definition); unset($definition);
@ -221,7 +262,8 @@ class importexport_schedule_ui
/** /**
* Generate a async key * Generate a async key
*/ */
public static function generate_id($data) { public static function generate_id($data)
{
$query = array( $query = array(
'name' => $data['definition'] 'name' => $data['definition']
@ -234,66 +276,6 @@ class importexport_schedule_ui
return $id; return $id;
} }
/**
* Get plugins via ajax
*/
public function ajax_get_plugins($type, $appname, &$response = null) {
// todo nathan: xajaxResponse needs to be replaced with Api\Json\Response, assign('exec[plugin]' is not eT2, is that code still used?
// please remove of not, or fix it
if($response) {
$return = false;
} else {
$response = new xajaxResponse();
}
$options = self::get_select_options(array('type' => $type, 'appname'=>$appname));
$response->addScript("clear_options('exec[plugin]');");
$response->addScript("selectbox_add_option('exec[plugin]','".lang('Select...')."', '',false);");
if(is_array($options['plugin'])) {
foreach ($options['plugin'] as $value => $title) {
$response->addScript("selectbox_add_option('exec[plugin]','$title', '$value',false);");
}
}
if(count($options['plugin']) == 1) {
$this->ajax_get_definitions($appname, $value, $response);
$response->assign('exec[plugin]','value',$value);
} else {
$response->addScript("xajax_doXMLHTTP('importexport.importexport_schedule_ui.ajax_get_definitions', '$appname', document.getElementById('exec[plugin]').value);");
}
// Trigger chosen
$response->addScript("\$j(document.getElementById('exec[plugin]')).trigger('liszt:updated');");
return $response->getXML();
}
/**
* Get definitions via ajax
*/
public function ajax_get_definitions($appname, $plugin, &$response = null) {
// todo nathan: xajaxResponse needs to be replaced with Api\Json\Response, assign('exec[plugin]' is not eT2, is that code still used?
// please remove of not, or fix it
$options = self::get_select_options(array('appname'=>$appname, 'plugin'=>$plugin));
if($response) {
$return = false;
} else {
$response = new xajaxResponse();
}
$response->addScript("clear_options('exec[definition]');");
$response->addScript("selectbox_add_option('exec[definition]','".lang('Select...')."', '',false);");
if(is_array($options['definition'])) {
foreach ($options['definition'] as $value => $title) {
$response->addScript("selectbox_add_option('exec[definition]','$title', '$value',false);");
}
}
if(count($options['definition']) == 1) {
$response->assign('exec[definition]','value',$value);
} else {
$response->addScript("document.getElementById('exec[definition]').value = ''");
}
// Trigger chosen
$response->addScript("\$j(document.getElementById('exec[definition]')).trigger('liszt:updated');");
return $response->getXML();
}
/** /**
* Check that the target is valid for the type (readable or writable) * Check that the target is valid for the type (readable or writable)
* and that they're not trying to write directly to the filesystem * and that they're not trying to write directly to the filesystem
@ -302,9 +284,11 @@ class importexport_schedule_ui
*/ */
public static function check_target(Array $data) { public static function check_target(Array $data) {
$scheme = parse_url($data['target'], PHP_URL_SCHEME); $scheme = parse_url($data['target'], PHP_URL_SCHEME);
if($scheme == '' || $scheme == 'file') { if($scheme == '' || $scheme == 'file')
{
return 'Direct file access not allowed'; return 'Direct file access not allowed';
} }
if($scheme == Vfs::SCHEME && !in_array(Vfs::SCHEME, stream_get_wrappers())) { if($scheme == Vfs::SCHEME && !in_array(Vfs::SCHEME, stream_get_wrappers())) {
stream_wrapper_register(Vfs::SCHEME, 'vfs_stream_wrapper', STREAM_IS_URL); stream_wrapper_register(Vfs::SCHEME, 'vfs_stream_wrapper', STREAM_IS_URL);
} }
@ -335,7 +319,8 @@ class importexport_schedule_ui
} }
return lang('%1 is not readable',$data['target']); return lang('%1 is not readable',$data['target']);
} }
elseif ($data['type'] == 'export' && !self::is__writable($data['target'])) { elseif ($data['type'] == 'export' && !self::is__writable($data['target']))
{
return lang('%1 is not writable',$data['target']); return lang('%1 is not writable',$data['target']);
} }
@ -348,21 +333,33 @@ class importexport_schedule_ui
* *
* @param path Path to check * @param path Path to check
*/ */
private static function is__writable($path) { private static function is__writable($path)
if ($path{strlen($path)-1}=='/') // recursively return a temporary file path {
if ($path{strlen($path)-1}=='/')
{
// recursively return a temporary file path
return self::is__writable($path.uniqid(mt_rand()).'.tmp'); return self::is__writable($path.uniqid(mt_rand()).'.tmp');
}
else if (is_dir($path)) else if (is_dir($path))
{
return self::is__writable($path.'/'.uniqid(mt_rand()).'.tmp'); return self::is__writable($path.'/'.uniqid(mt_rand()).'.tmp');
}
// check tmp file for read/write capabilities // check tmp file for read/write capabilities
$rm = file_exists($path); $rm = file_exists($path);
$f = @fopen($path, 'a'); $f = @fopen($path, 'a');
if ($f===false) if ($f===false)
{
return false; return false;
}
fclose($f); fclose($f);
if (!$rm) if (!$rm)
{
@unlink($path); @unlink($path);
}
return true; return true;
} }
@ -402,7 +399,8 @@ class importexport_schedule_ui
// check file // check file
$file_check = self::check_target($data); $file_check = self::check_target($data);
if($file_check !== true) { if($file_check !== true)
{
$data['errors'] = array($file_check=>''); $data['errors'] = array($file_check=>'');
// Update job with results // Update job with results
self::update_job($data); self::update_job($data);
@ -413,7 +411,8 @@ class importexport_schedule_ui
} }
$definition = new importexport_definition($data['definition']); $definition = new importexport_definition($data['definition']);
if( $definition->get_identifier() < 1 ) { if( $definition->get_identifier() < 1 )
{
$data['errors'] = array('Definition not found!'); $data['errors'] = array('Definition not found!');
// Update job with results // Update job with results
self::update_job($data); self::update_job($data);
@ -434,7 +433,10 @@ class importexport_schedule_ui
$targets = array(); $targets = array();
foreach(scandir($data['target']) as $target) foreach(scandir($data['target']) as $target)
{ {
if ($target == '.' || $target == '..') continue; if ($target == '.' || $target == '..')
{
continue;
}
$target = $data['target'].(substr($data['target'],-1) == '/' ? '' : '/').$target; $target = $data['target'].(substr($data['target'],-1) == '/' ? '' : '/').$target;
// Check modification time, make sure it's not currently being written // Check modification time, make sure it's not currently being written
@ -463,7 +465,6 @@ class importexport_schedule_ui
$targets = array($data['target']); $targets = array($data['target']);
} }
if($type == 'export') if($type == 'export')
{ {
// Set to export all or filter, if set // Set to export all or filter, if set
@ -476,7 +477,10 @@ class importexport_schedule_ui
foreach($definition->filter as $field => $value) foreach($definition->filter as $field => $value)
{ {
// Handle multiple values // Handle multiple values
if(!is_array($value) && strpos($value,',') !== false) $value = explode(',',$value); if(!is_array($value) && strpos($value,',') !== false)
{
$value = explode(',',$value);
}
$filters[$field] = $value; $filters[$field] = $value;
@ -503,12 +507,16 @@ class importexport_schedule_ui
self::update_job($data, true); self::update_job($data, true);
$resource = null; $resource = null;
try { try
if (($resource = @fopen( $target, $data['type'] == 'import' ? 'rb' : 'wb' ))) { {
if (($resource = @fopen( $target, $data['type'] == 'import' ? 'rb' : 'wb' )))
{
$result = $po->$type( $resource, $definition ); $result = $po->$type( $resource, $definition );
fclose($resource); fclose($resource);
} else { }
else
{
error_log('importexport_schedule: ' . date('c') . ": File $target not readable! \n"); error_log('importexport_schedule: ' . date('c') . ": File $target not readable! \n");
$data['errors'][$target][] = lang('%1 is not readable',$target); $data['errors'][$target][] = lang('%1 is not readable',$target);
} }
@ -520,33 +528,42 @@ class importexport_schedule_ui
} }
if(method_exists($po, 'get_warnings') && $po->get_warnings()) { if(method_exists($po, 'get_warnings') && $po->get_warnings())
{
$buffer = 'importexport_schedule: ' . date('c') . ": Import warnings:\n#\tWarning\n"; $buffer = 'importexport_schedule: ' . date('c') . ": Import warnings:\n#\tWarning\n";
foreach($po->get_warnings() as $record => $msg) { foreach($po->get_warnings() as $record => $msg)
{
$data['warnings'][$target][] = "#$record: $msg"; $data['warnings'][$target][] = "#$record: $msg";
$buffer += "$record\t$msg\n"; $buffer += "$record\t$msg\n";
} }
error_log($buffer); error_log($buffer);
} }
if(method_exists($po, 'get_errors') && $po->get_errors()) { if(method_exists($po, 'get_errors') && $po->get_errors())
{
$buffer = 'importexport_schedule: ' . date('c') . ": Import errors:\n#\tError\n"; $buffer = 'importexport_schedule: ' . date('c') . ": Import errors:\n#\tError\n";
foreach($po->get_errors() as $record => $error) { foreach($po->get_errors() as $record => $error)
{
$data['errors'][$target][] = "#$record: $error"; $data['errors'][$target][] = "#$record: $error";
$buffer += "$record\t$error\n"; $buffer += "$record\t$error\n";
} }
error_log($buffer); error_log($buffer);
} }
if($po instanceof importexport_iface_import_plugin) { if($po instanceof importexport_iface_import_plugin)
if(is_numeric($result)) { {
if(is_numeric($result))
{
$data['record_count'] += $result; $data['record_count'] += $result;
$data['result'][$target][] = lang('%1 records processed', $result); $data['result'][$target][] = lang('%1 records processed', $result);
} }
$data['result'][$target] = array(); $data['result'][$target] = array();
foreach($po->get_results() as $action => $count) { foreach($po->get_results() as $action => $count)
{
$data['result'][$target][] = lang($action) . ": $count"; $data['result'][$target][] = lang($action) . ": $count";
} }
} else { }
else
{
if($result instanceof importexport_iface_export_record) if($result instanceof importexport_iface_export_record)
{ {
$data['record_count'] += $result->get_num_of_records(); $data['record_count'] += $result->get_num_of_records();
@ -595,12 +612,15 @@ class importexport_schedule_ui
* Update the async job with current status, and send a notification * Update the async job with current status, and send a notification
* to user if there were any errors. * to user if there were any errors.
*/ */
private static function update_job($data, $no_notification = false) { private static function update_job($data, $no_notification = false)
{
$id = self::generate_id($data); $id = self::generate_id($data);
$async = new Api\Asyncservice(); $async = new Api\Asyncservice();
$jobs = $async->read($id); $jobs = $async->read($id);
$job = $jobs[$id]; $job = $jobs[$id];
if(is_array($job)) {
if(is_array($job))
{
$async->cancel_timer($id); $async->cancel_timer($id);
$result = $async->set_timer( $result = $async->set_timer(
$async['times'], $async['times'],
@ -609,7 +629,10 @@ class importexport_schedule_ui
$data $data
); );
} }
if($no_notification) return $result; if($no_notification)
{
return $result;
}
// Send notification to user // Send notification to user
if($data['warnings'] || $data['errors']) if($data['warnings'] || $data['errors'])