extranet/scripts/build/getCatalog.php
2015-07-16 15:42:55 +00:00

149 lines
4.5 KiB
PHP

<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
'Metier' => __DIR__ . '/../../library/Metier',
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$catalogs = array(
'evenements' => array(
'sql' => 'tabEvenements.sql',
'table' => 'tabEvenements',
'method' => array('name'=>'getCatalogEvent', 'params'=> array( null , array('codEven', 'libEven', 'libEvenEn') ))
),
'naf5' => array(
'sql' => 'tabNaf5.sql',
'table' => 'tabNaf5tmp',
'method' => array('name'=>'getCatalogNaf5', 'params'=> array( null , array('codNaf5', 'libNaf5', 'libNaf5en', 'codNaf4', 'codNaf3', 'codNaf2', 'codNaf1') ))
),
'fctdir' => array(
'sql' => 'tabFctDir.sql',
'table' => 'tabFctDir',
'method' => array('name'=>'getCatalogFctDir', 'params'=> array( null , array('codeFct', 'libelle') ))
),
'fj' => array(
'sql' => 'tabFJur.sql',
'table' => 'tabFJur',
'method' => array('name'=>'getCatalogLegalForm', 'params'=> array( null , array('code', 'libelle', 'libelleEn') ))
),
'city' => array(
'sql' => 'tabVilles.sql',
'table' => 'tabVilles',
'method' => array('name'=>'getCatalogCity', 'params'=> array( null , array('codePostal', 'LIBGEO') ))
),
'dep' => array(
'sql' => 'tabDepartements.sql',
'table' => 'tabDepartements',
'method' => array('name'=>'getCatalogDepartements', 'params'=> array( null , array('numdep', 'libdep') ))
),
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Display this help",
'name=s' => "Name of catalog to load",
'update' => "Only new content",
'list' => "List item",
'login-s' => "Login for the webservice",
'password-s' => "Password "
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo $opts->getUsageMessage();
exit;
}
//Diplay list of catalog to load
if ( $opts->list ) {
foreach ($catalogs as $item => $options) {
echo $item."\n";
}
}
require_once 'Scores/WsScores.php';
function wsCall($name, $params, $login, $pass)
{
$ws = new WsScores($login, $pass);
return call_user_func_array(array($ws, $name), $params);
}
function createSql($filename, $db)
{
$sql = file_get_contents(realpath(dirname(__FILE__)).'/config/_sql/structure/'.$filename);
$db->query($sql);
}
function insertData($data, $table, $db)
{
$result = $db->insert($table, $data);
return $result;
}
$c = new Zend_Config($application->getOptions());
Zend_Registry::set('config', $c);
$db = Zend_Db::factory($c->profil->db->sdv1);
if ( array_key_exists($opts->name, $catalogs) )
{
$catalog = $catalogs[$opts->name];
$sql = "DROP TABLE IF EXISTS ".$catalog['table'].";";
$db->query($sql);
createSql($catalog['sql'], $db);
$encode = wsCall($catalog['method']['name'], $catalog['method']['params'], $opts->login, md5($opts->login.'|'.$opts->password));
$rows = json_decode($encode, true);
if (count($rows)>0) {
foreach ($rows as $row) {
insertData($row, $catalog['table'], $db);
}
}
}