extranet/scripts/build/getCatalog.php

111 lines
2.9 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(),
)));
/** Zend_Application */
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') ))
)
);
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 ".$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);
}
}
}