This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/import/import.inc.php

320 lines
8.0 KiB
PHP
Raw Normal View History

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
2009-08-03 04:10:16 +00:00
* @subpackage Modules:Import
*/
2009-08-03 04:10:16 +00:00
/**
* The main AgileBill Import Class
*
* @package AgileBill
* @subpackage Modules:Import
*/
class import extends OSB_module {
protected $actions = array();
/**
* Test remote database connectivity
*/
protected function test() {
global $C_debug,$VAR;
# Connect to the remote DB
$dbr = &NewADOConnection($this->type);
$dbr->Connect($this->host,$this->user,$this->pass,$this->db);
if (! empty($dbr->_errorMsg))
$C_debug->alert(sprintf('Failed: %s',$dbr->_errorMsg));
else
$this->pre_test();
# Return to main import page
printf("<script type='text/javascript' language=javascript>setTimeout('document.location=\'?_page=import:import&plugin=%s\'',1500);</script>",$VAR['plugin']);
}
2009-08-03 04:10:16 +00:00
/**
* Record pre-setup tests
*/
protected function pre_test() {
global $C_debug,$VAR;
2009-08-03 04:10:16 +00:00
$C_debug->alert('Marked Done!');
2009-08-03 04:10:16 +00:00
$db = &DB();
2009-08-03 04:10:16 +00:00
# Insert the import record
$this->import_transaction($this->plugin,$VAR['action'],null,null,null,null,$db);
2009-08-03 04:10:16 +00:00
# Return to main import page
printf("<script type='text/javascript' language=javascript>setTimeout('document.location=\'?_page=import:import&plugin=%s\'',1500);</script>",$VAR['plugin']);
}
2009-08-03 04:10:16 +00:00
# Store import id
protected function import_transaction($plugin,$action,$ab_table,$ab_id,$remote_table,$remote_id,&$db) {
# Check that this record has not already been imported:
2009-08-03 04:10:16 +00:00
$sql = sqlSelect($db,'import','id',
sprintf('plugin = %s AND action = %s AND ab_table = %s AND ab_id = %s AND remote_table = %s AND remote_id = %s',
$db->qstr($plugin),
$db->qstr($action),
$db->qstr($ab_table),
$db->qstr($ab_id),
$db->qstr($remote_table),
$db->qstr($remote_id)
));
$rs = $db->Execute($sql);
# Check results
if ($rs === false || $rs->RecordCount() > 0) {
$db->FailTrans();
return false;
}
# Insert the record
2009-08-03 04:10:16 +00:00
$update = array(
'date_orig'=>time(),
'plugin'=>$plugin,
'action'=>$action,
'ab_table'=>$ab_table,
'ab_id'=>$ab_id,
'remote_table'=>$remote_table,
'remote_id'=>$remote_id);
2009-08-03 04:10:16 +00:00
$db->Execute(sqlInsert($db,'import',$update));
2009-08-03 04:10:16 +00:00
return true;
}
2009-08-03 04:10:16 +00:00
# Do an action for a specific plugin:
public function do_action($VAR) {
# Load the plugin
if (! is_file($file = sprintf('%simport/%s.php',PATH_PLUGINS,$VAR['plugin'])))
return false;
# New instance
include_once($file);
2009-08-03 04:10:16 +00:00
$import_plugin = new import_plugin;
2009-08-03 04:10:16 +00:00
# Call the required method
call_user_func(array($import_plugin,$VAR['action']),$VAR,$import_plugin);
}
2009-08-03 04:10:16 +00:00
# Do an action for a specific plugin:
public function undo_action($VAR) {
$db = &DB();
# Make sure this action is done...
2009-08-03 04:10:16 +00:00
$rs = $db->Execute(sqlSelect($db,'import','ab_table,ab_id',sprintf('plugin=::%s:: AND action=::%s::',$VAR['plugin'],$VAR['action'])));
2009-08-03 04:10:16 +00:00
if ($rs->RecordCount() == 0) {
echo 'There is nothing to undo!';
2009-08-03 04:10:16 +00:00
return;
}
2009-08-03 04:10:16 +00:00
while (! $rs->EOF) {
if (trim($rs->fields['ab_table']) && trim($rs->fields['ab_id']))
$db->Execute(sqlDelete($db,$rs->fields['ab_table'],sprintf('id=%s',$rs->fields['ab_id'])));
$rs->MoveNext();
}
2009-08-03 04:10:16 +00:00
# Delete the selected action
$db->Execute(sqlDelete($db,'import',sprintf('plugin=::%s:: AND action=::%s::',$VAR['plugin'],$VAR['action'])));
}
2009-08-03 04:10:16 +00:00
/**
* View import plugins
*/
public function view($VAR) {
# Load the plugin
if (! is_file($file = sprintf('%simport/%s.php',PATH_PLUGINS,$VAR['plugin'])))
return false;
$db = &DB();
include_once($file);
$import_plugin = new import_plugin;
2009-08-03 04:10:16 +00:00
# Loop through each action to determine its availibility status
$actions = $import_plugin->actions;
2009-08-03 04:10:16 +00:00
$done = false;
for ($i=0; $i<count($actions); $i++) {
# Are the dependencies met?
$depn = $actions[$i]['depn'];
if (is_array($depn)) {
for ($ii=0; $ii<count($depn); $ii++) {
if (empty($done[$depn[$ii]])) {
$actions[$i]['status'] = 'pending';
break;
}
}
2009-08-03 04:10:16 +00:00
if ($actions[$i]['status'] != 'pending')
$actions[$i]['status'] = 'ready';
2009-08-03 04:10:16 +00:00
} else {
$actions[$i]['status'] = 'ready';
2009-08-03 04:10:16 +00:00
}
2009-08-03 04:10:16 +00:00
# Passed dependencies, check if it has been run already or not:
if ($actions[$i]['status'] == 'ready') {
$rs = $db->Execute(sqlSelect($db,'import','id',sprintf('plugin=::%s:: AND action=::%s::',$import_plugin->name,$actions[$i]['name'])));
2009-08-03 04:10:16 +00:00
if ($rs->RecordCount() > 0) {
$actions[$i]['status'] = 'done';
$actions[$i]['records'] = $rs->RecordCount();
$done[$actions[$i]['name']] = true;
}
}
}
2009-08-03 04:10:16 +00:00
global $smarty;
$smarty->assign('name',$import_plugin->name);
$smarty->assign('instructions',$import_plugin->instructions);
$smarty->assign('import',$actions);
}
2009-08-03 04:10:16 +00:00
/**
* Search for available plugs
*
* @uses CORE_search
*/
public function search($VAR) {
# Read the contents of the directory
$count = 0;
2009-08-03 04:10:16 +00:00
$dir = opendir(PATH_PLUGINS.'import');
while ($file_name = readdir($dir))
if (! in_array($file_name,array('.','..')) && ! preg_match('/^_/',$file_name) && preg_match('/.php$/',$file_name))
$count++;
2009-08-03 04:10:16 +00:00
# Define the DB vars as a Smarty accessible block
global $smarty;
# create the search record:
2009-08-03 04:10:16 +00:00
if ($count > 0) {
# create the search record
2009-08-03 04:10:16 +00:00
include_once(PATH_CORE.'search.inc.php');
$search = new CORE_search;
2009-08-03 04:10:16 +00:00
$arr['module'] = $this->module;
$arr['sql'] = '';
$arr['limit'] = '999';
$arr['order_by'] = 'name';
$arr['results'] = $count;
$search->add($arr);
# define the search id and other parameters for Smarty
2009-08-03 04:10:16 +00:00
$smarty->assign('search_id',$search->id);
2009-08-03 04:10:16 +00:00
# page
$smarty->assign('page','1');
2009-08-03 04:10:16 +00:00
# limit
$smarty->assign('limit','999');
2009-08-03 04:10:16 +00:00
# order_by
$smarty->assign('order_by','name');
# define the result count
2009-08-03 04:10:16 +00:00
$smarty->assign('results',$count);
}
}
2009-08-03 04:10:16 +00:00
/**
* Show search results
*/
public function search_show($VAR) {
# Read the contents of the directory
$count = 0;
2009-08-03 04:10:16 +00:00
$dir = opendir(PATH_PLUGINS.'import');
while ($file_name = readdir($dir)) {
2009-08-03 04:10:16 +00:00
if (! in_array($file_name,array('.','..')) && ! preg_match('/^_/',$file_name) && preg_match('/.php$/',$file_name)) {
$result[$count]['name'] = preg_replace('/.php$/','',$file_name);
$result[$count]['id'] = $count;
$count++;
}
2009-08-03 04:10:16 +00:00
}
$class_name = true;
for ($i=0; $i<count($result); $i++) {
$smart[$i] = $result[$i];
if ($class_name) {
$smart[$i]['_C'] = 'row1';
2009-08-03 04:10:16 +00:00
$class_name = false;
} else {
$smart[$i]['_C'] = 'row2';
2009-08-03 04:10:16 +00:00
$class_name = true;
}
}
2009-08-03 04:10:16 +00:00
# Define the DB vars as a Smarty accessible block
global $smarty;
# define the results
2009-08-03 04:10:16 +00:00
$smarty->assign('search_show',$smart);
$smarty->assign('page',$VAR['page']);
$smarty->assign('order',$smarty_order);
$smarty->assign('sort',$smarty_sort);
$smarty->assign('limit',$search->limit);
$smarty->assign('search_id',$search->id);
2009-08-03 04:10:16 +00:00
$smarty->assign('results',$count);
# total pages
2009-08-03 04:10:16 +00:00
$smarty->assign('pages',1);
# current page
2009-08-03 04:10:16 +00:00
$smarty->assign('page',1);
# page array for menu
2009-08-03 04:10:16 +00:00
$smarty->assign('page_arr','');
}
/**
* Read a plugin map file, for some default values
*
* @uses CORE_xml;
*/
protected function read_map() {
# If there is a map file, open it
$pmap = array();
if (file_exists($mapfile = sprintf('%simport/%s_map.xml',PATH_PLUGINS,$this->plugin))) {
$C_xml = new CORE_xml;
$map = $C_xml->xml_to_array($mapfile);
if (isset($map['map']))
foreach ($map['map'] as $index => $details)
foreach ($map['map'][$index] as $values) {
$a = $values['id'];
unset($values['id']);
$pmap[$index][$a] = $values;
}
}
return $pmap;
}
}
2009-08-03 04:10:16 +00:00
?>