Last active
January 20, 2017 14:15
-
-
Save Burick/edc0accbae6d236674a499a9632bbfe1 to your computer and use it in GitHub Desktop.
выбирает записи из таблицы FormIt и записывает в csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$index = $_SERVER[DOCUMENT_ROOT].'/index.php'; | |
// Подключаем | |
define('MODX_API_MODE', true); | |
//require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/index.php'; | |
require_once $index; | |
// Включаем обработку ошибок | |
$modx->getService('error','error.modError'); | |
$modx->setLogLevel(modX::LOG_LEVEL_INFO); | |
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML'); | |
/* Подключение Formit */ | |
$formitModelPath = MODX_CORE_PATH . 'components/formit/model/'; | |
$modx->addPackage('formit',$formitModelPath , 'modx_'); | |
$data = date('Ymd_Hi'); | |
$id_file = 'id.txt'; // файл в котором хранится последний взятый id | |
$data_file = $data.'_formit.csv'; // файл с выбранными данными | |
// получаем ID последней выбраной записи из файла | |
// если нет файла с последним ID выборки то выбираем все | |
if( file_exists($id_file) && $last_id = file($id_file)){ | |
$where = array( | |
'context_key' => 'web', | |
'id:>' => $last_id[0] | |
); | |
}else{ | |
$where = array( | |
'context_key' => 'web' | |
); | |
} | |
$q = $modx->newQuery('FormItForm'); | |
$q->where($where); | |
$q->select( array( | |
'id', | |
'values' | |
)); | |
$q->prepare(); | |
$sql = $q->toSQL(); | |
$query = $modx->prepare($sql); | |
$query->execute(); | |
$value_arr = array(); | |
if($result = $query->fetchAll(PDO::FETCH_ASSOC)){ | |
$data_array = array(); | |
foreach($result as $data){ | |
$id = $data['id']; | |
$value = $data['values']; | |
$value = json_decode($value); | |
$value_str = $id.';'; | |
foreach($value as $v){ | |
if($v){ | |
$value_str .= $v.';'; | |
}else{ | |
$value_str .= ' ;'; | |
} | |
} | |
// заменяем переносы строк на пробелы | |
$need = '/([\r\n])[\s]+/'; | |
$value_str = preg_replace($need, ' ', $value_str); | |
$value_str .= PHP_EOL; | |
$value_arr[] = $value_str; | |
} | |
}; | |
$bom = "\xEF\xBB\xBF"; | |
file_put_contents($data_file, $bom); | |
if($value_arr && file_put_contents($data_file, $value_arr, FILE_APPEND) ){ | |
// записываем последний взятый ID если что то брали | |
file_put_contents($id_file, $id); | |
} | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment