import_csv.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?php
  2. if (PHP_SAPI !='cli') {
  3. die('Run this script through the command line or comment this line in the code');
  4. }
  5. //$_SERVER['SERVER_NAME'] = '';
  6. //$_SERVER['HTTP_HOST'] = '';
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. require_once api_get_path(LIBRARY_PATH).'log.class.php';
  9. /**
  10. * Class ImportCsv
  11. */
  12. class ImportCsv
  13. {
  14. private $logger;
  15. private $dumpValues;
  16. public $test;
  17. public $defaultLanguage = 'dutch';
  18. public $extraFieldIdNameList = array(
  19. 'session' => 'external_session_id',
  20. 'course' => 'external_course_id',
  21. 'user' => 'external_user_id',
  22. );
  23. public $defaultAdminId = 1;
  24. public $defaultSessionVisibility = 1;
  25. /**
  26. * When creating a user the expiration date is set to registration date + this value
  27. * @var int number of years
  28. */
  29. public $expirationDateInUserCreation = 1;
  30. /**
  31. * When updating a user the expiration date is set to update date + this value
  32. * @var int number of years
  33. */
  34. public $expirationDateInUserUpdate = 1;
  35. public $daysCoachAccessBeforeBeginning = 30;
  36. public $daysCoachAccessAfterBeginning = 60;
  37. public $conditions;
  38. /**
  39. * @param Logger $logger
  40. */
  41. public function __construct($logger, $conditions)
  42. {
  43. $this->logger = $logger;
  44. $this->conditions = $conditions;
  45. }
  46. /**
  47. * @param bool $dump
  48. */
  49. function setDumpValues($dump)
  50. {
  51. $this->dumpValues = $dump;
  52. }
  53. /**
  54. * @return mixed
  55. */
  56. function getDumpValues()
  57. {
  58. return $this->dumpValues;
  59. }
  60. /**
  61. * Runs the import process
  62. */
  63. public function run()
  64. {
  65. $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
  66. if (!is_dir($path)) {
  67. echo "The folder! $path does not exits";
  68. exit;
  69. }
  70. if ($this->getDumpValues()) {
  71. $this->dumpDatabaseTables();
  72. }
  73. echo "Starting with reading the files: ".PHP_EOL.PHP_EOL;
  74. $files = scandir($path);
  75. $fileToProcess = array();
  76. if (!empty($files)) {
  77. foreach ($files as $file) {
  78. $fileInfo = pathinfo($file);
  79. if ($fileInfo['extension'] == 'csv') {
  80. // teachers_yyyymmdd.csv, courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
  81. $parts = explode('_', $fileInfo['filename']);
  82. $method = 'import'.ucwords($parts[1]);
  83. if (method_exists($this, $method)) {
  84. $fileToProcess[$parts[1]][] = array(
  85. 'method' => $method,
  86. 'file' => $path.$fileInfo['basename']
  87. );
  88. //$this->$method($path.$fileInfo['basename']);
  89. } else {
  90. echo "Error - This file '$file' can't be processed.".PHP_EOL;
  91. echo "The file have to has this format:".PHP_EOL;
  92. echo "prefix_students_ddmmyyyy.csv, prefix_teachers_ddmmyyyy.csv, prefix_courses_ddmmyyyy.csv, prefix_sessions_ddmmyyyy.csv ".PHP_EOL;
  93. exit;
  94. }
  95. }
  96. }
  97. if (empty($fileToProcess)) {
  98. echo 'Error - no files to process.';
  99. exit;
  100. }
  101. $sections = array('students', 'teachers', 'courses', 'sessions');
  102. $this->prepareImport();
  103. foreach ($sections as $section) {
  104. $this->logger->addInfo("-- Import $section --");
  105. if (isset($fileToProcess[$section]) && !empty($fileToProcess[$section])) {
  106. $files = $fileToProcess[$section];
  107. foreach ($files as $fileInfo) {
  108. $method = $fileInfo['method'];
  109. $file = $fileInfo['file'];
  110. echo 'Reading file: '.$file.PHP_EOL;
  111. $this->logger->addInfo("Reading file: $file");
  112. $this->$method($file);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. private function prepareImport()
  119. {
  120. // Create user extra field: extra_external_user_id
  121. UserManager::create_extra_field($this->extraFieldIdNameList['user'], 1, 'External user id', null);
  122. // Create course extra field: extra_external_course_id
  123. CourseManager::create_course_extra_field($this->extraFieldIdNameList['course'], 1, 'External course id');
  124. // Create session extra field extra_external_session_id
  125. SessionManager::create_session_extra_field($this->extraFieldIdNameList['session'], 1, 'External session id');
  126. }
  127. /**
  128. * @param string $file
  129. */
  130. function moveFile($file)
  131. {
  132. $moved = str_replace('incoming', 'treated', $file);
  133. if ($this->test) {
  134. $result = 1;
  135. } else {
  136. $result = rename($file, $moved);
  137. }
  138. if ($result) {
  139. $this->logger->addInfo("Moving file to the treated folder: $file");
  140. } else {
  141. $this->logger->addError("Error - Cant move file to the treated folder: $file");
  142. }
  143. }
  144. /**
  145. * @param array $row
  146. *
  147. * @return array
  148. */
  149. private function cleanUserRow($row)
  150. {
  151. $row['lastname'] = $row['LastName'];
  152. $row['firstname'] = $row['FirstName'];
  153. $row['email'] = $row['Email'];
  154. $row['username'] = $row['UserName'];
  155. $row['password'] = $row['Password'];
  156. $row['auth_source'] = $row['AuthSource'];
  157. $row['official_code'] = $row['OfficialCode'];
  158. $row['phone'] = $row['PhoneNumber'];
  159. if (isset($row['StudentID'])) {
  160. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['StudentID'];
  161. }
  162. if (isset($row['TeacherID'])) {
  163. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['TeacherID'];
  164. }
  165. //$row['lastname'] = Status
  166. return $row;
  167. }
  168. /**
  169. * @param array $row
  170. * @return array
  171. */
  172. private function cleanCourseRow($row)
  173. {
  174. $row['title'] = $row['Title'];
  175. $row['course_code'] = $row['Code'];
  176. $row['course_category'] = $row['CourseCategory'];
  177. $row['email'] = $row['Teacher'];
  178. $row['language'] = $row['Language'];
  179. $row['teachers'] = array();
  180. if (isset($row['Teacher']) && !empty($row['Teacher'])) {
  181. $userInfo = api_get_user_info_from_username($row['Teacher']);
  182. if (!empty($userInfo)) {
  183. $row['teachers'] = $userInfo['user_id'];
  184. }
  185. }
  186. if (isset($row['CourseID'])) {
  187. $row['extra_'.$this->extraFieldIdNameList['course']] = $row['CourseID'];
  188. }
  189. return $row;
  190. }
  191. /**
  192. * File to import
  193. * @param string $file
  194. */
  195. private function importTeachers($file)
  196. {
  197. $data = Import::csv_to_array($file);
  198. /* Unique identifier: official-code username.
  199. Email address and password should never get updated. *ok
  200. The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
  201. A slight edit of these fields should be taken into account. ???
  202. Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
  203. We’ll handle that manually if applicable.
  204. No delete!
  205. */
  206. $language = $this->defaultLanguage;
  207. if (!empty($data)) {
  208. $this->logger->addInfo(count($data)." records found.");
  209. foreach ($data as $row) {
  210. $row = $this->cleanUserRow($row);
  211. $user_id = UserManager::get_user_id_from_original_id($row['extra_'.$this->extraFieldIdNameList['user']], $this->extraFieldIdNameList['user']);
  212. $userInfo = array();
  213. $userInfoByOfficialCode = null;
  214. if (!empty($user_id)) {
  215. $userInfo = api_get_user_info($user_id);
  216. //$userInfo = api_get_user_info_from_username($row['username']);
  217. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  218. }
  219. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  220. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  221. // Create user
  222. $userId = UserManager::create_user(
  223. $row['firstname'],
  224. $row['lastname'],
  225. COURSEMANAGER,
  226. $row['email'],
  227. $row['username'],
  228. $row['password'],
  229. $row['official_code'],
  230. $language, //$row['language'],
  231. $row['phone'],
  232. null, //$row['picture'], //picture
  233. PLATFORM_AUTH_SOURCE, // ?
  234. $expirationDate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  235. 1, //active
  236. 0,
  237. null, // extra
  238. null, //$encrypt_method = '',
  239. false //$send_mail = false
  240. );
  241. if ($userId) {
  242. foreach ($row as $key => $value) {
  243. if (substr($key, 0, 6) == 'extra_') { //an extra field
  244. UserManager::update_extra_field_value($userId, substr($key, 6), $value);
  245. }
  246. }
  247. $this->logger->addInfo("Teachers - User created: ".$row['username']);
  248. } else {
  249. $this->logger->addError("Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  250. }
  251. } else {
  252. if (empty($userInfo)) {
  253. $this->logger->addError("Teachers - Can't update user :".$row['username']);
  254. continue;
  255. }
  256. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  257. // Update user
  258. $result = UserManager::update_user(
  259. $userInfo['user_id'],
  260. $row['firstname'], // <<-- changed
  261. $row['lastname'], // <<-- changed
  262. $userInfo['username'],
  263. null, //$password = null,
  264. $auth_source = null,
  265. $userInfo['email'],
  266. COURSEMANAGER,
  267. $userInfo['official_code'],
  268. $userInfo['phone'],
  269. $userInfo['picture_uri'],
  270. $expirationDate,
  271. $userInfo['active'],
  272. null, //$creator_id = null,
  273. 0, //$hr_dept_id = 0,
  274. null, // $extra = null,
  275. null, //$language = 'english',
  276. null, //$encrypt_method = '',
  277. false, //$send_email = false,
  278. 0 //$reset_password = 0
  279. );
  280. if ($result) {
  281. foreach ($row as $key => $value) {
  282. if (substr($key, 0, 6) == 'extra_') { //an extra field
  283. UserManager::update_extra_field_value($userInfo['user_id'], substr($key, 6), $value);
  284. }
  285. }
  286. $this->logger->addInfo("Teachers - User updated: ".$row['username']);
  287. } else {
  288. $this->logger->addError("Teachers - User not updated: ".$row['username']);
  289. }
  290. }
  291. }
  292. }
  293. $this->moveFile($file);
  294. }
  295. /**
  296. * @param string $file
  297. */
  298. private function importStudents($file)
  299. {
  300. $data = Import::csv_to_array($file);
  301. /*
  302. * Another users import.
  303. Unique identifier: official code and username . ok
  304. Password should never get updated. ok
  305. If an update should need to occur (because it changed in the .csv), we’ll want that logged. We will handle this manually in that case.
  306. All other fields should be updateable, though passwords should of course not get updated. ok
  307. If a user gets deleted (not there anymore),
  308. He should be set inactive one year after the current date. So I presume you’ll just update the expiration date. We want to grant access to courses up to a year after deletion.
  309. */
  310. if (!empty($data)) {
  311. $language = $this->defaultLanguage;
  312. $this->logger->addInfo(count($data)." records found.");
  313. foreach ($data as $row) {
  314. $row = $this->cleanUserRow($row);
  315. //$userInfo = api_get_user_info_from_username($row['username']);
  316. $user_id = UserManager::get_user_id_from_original_id($row['extra_'.$this->extraFieldIdNameList['user']], $this->extraFieldIdNameList['user']);
  317. $userInfo = array();
  318. $userInfoByOfficialCode = null;
  319. if (!empty($user_id)) {
  320. $userInfo = api_get_user_info($user_id);
  321. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  322. }
  323. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  324. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  325. // Create user
  326. $result = UserManager::create_user(
  327. $row['firstname'],
  328. $row['lastname'],
  329. STUDENT,
  330. $row['email'],
  331. $row['username'],
  332. $row['password'],
  333. $row['official_code'],
  334. $language, //$row['language'],
  335. $row['phone'],
  336. null, //$row['picture'], //picture
  337. PLATFORM_AUTH_SOURCE, // ?
  338. $expirationDate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  339. 1, //active
  340. 0,
  341. null, // extra
  342. null, //$encrypt_method = '',
  343. false //$send_mail = false
  344. );
  345. if ($result) {
  346. foreach ($row as $key => $value) {
  347. if (substr($key, 0, 6) == 'extra_') { //an extra field
  348. UserManager::update_extra_field_value($result, substr($key, 6), $value);
  349. }
  350. }
  351. $this->logger->addInfo("Students - User created: ".$row['username']);
  352. } else {
  353. $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  354. }
  355. } else {
  356. if (empty($userInfo)) {
  357. $this->logger->addError("Students - Can't update user :".$row['username']);
  358. continue;
  359. }
  360. if ($row['action'] == 'delete') {
  361. // INactive one year later
  362. $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + 365*24*60*60));
  363. }
  364. if (isset($this->conditions['importStudents'])) {
  365. if (isset($this->conditions['importStudents']['update']) && isset($this->conditions['importStudents']['update']['avoid'])) {
  366. $avoidUsersWithEmail = $this->conditions['importStudents']['update']['avoid']['email'];
  367. if ($userInfo['email'] != $row['email'] && in_array($row['email'], $avoidUsersWithEmail)) {
  368. $this->logger->addInfo("Students - User skipped: ".$row['username']." because the avoid conditions (email).");
  369. continue;
  370. }
  371. $avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
  372. if ($userInfo['password'] != api_get_encrypted_password($row['password']) && in_array($row['password'], $avoidUsersWithPassword)) {
  373. $this->logger->addInfo("Students - User skipped: ".$row['username']." because the avoid conditions (password).");
  374. continue;
  375. }
  376. }
  377. }
  378. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  379. // Update user
  380. $result = UserManager::update_user(
  381. $userInfo['user_id'],
  382. $row['firstname'], // <<-- changed
  383. $row['lastname'], // <<-- changed
  384. $row['username'], // <<-- changed
  385. null, //$password = null,
  386. $auth_source = null,
  387. $userInfo['email'],
  388. STUDENT,
  389. $userInfo['official_code'],
  390. $userInfo['phone'],
  391. $userInfo['picture_uri'],
  392. $expirationDate,
  393. $userInfo['active'],
  394. null, //$creator_id = null,
  395. 0, //$hr_dept_id = 0,
  396. null, // $extra = null,
  397. null, //$language = 'english',
  398. null, //$encrypt_method = '',
  399. false, //$send_email = false,
  400. 0 //$reset_password = 0
  401. );
  402. if ($result) {
  403. if ($row['username'] != $userInfo['username']) {
  404. $this->logger->addInfo("Students - Username was changes from '".$userInfo['username']."' to '".$row['username']."' ");
  405. }
  406. foreach ($row as $key => $value) {
  407. if (substr($key, 0, 6) == 'extra_') { //an extra field
  408. UserManager::update_extra_field_value($userInfo['user_id'], substr($key, 6), $value);
  409. }
  410. }
  411. $this->logger->addInfo("Students - User updated: ".$row['username']);
  412. } else {
  413. $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  414. }
  415. }
  416. }
  417. }
  418. $this->moveFile($file);
  419. }
  420. /**
  421. * @param string $file
  422. */
  423. private function importCourses($file)
  424. {
  425. $data = Import::csv_to_array($file);
  426. //$language = $this->defaultLanguage;
  427. if (!empty($data)) {
  428. $this->logger->addInfo(count($data)." records found.");
  429. foreach ($data as $row) {
  430. $row = $this->cleanCourseRow($row);
  431. $courseCode = CourseManager::get_course_id_from_original_id($row['extra_'.$this->extraFieldIdNameList['course']], $this->extraFieldIdNameList['course']);
  432. //$courseInfo = api_get_course_info($row['course_code']);
  433. $courseInfo = api_get_course_info($courseCode);
  434. if (empty($courseInfo)) {
  435. // Create
  436. $params = array();
  437. $params['title'] = $row['title'];
  438. $params['exemplary_content'] = false;
  439. $params['wanted_code'] = $row['course_code'];
  440. $params['course_category'] = $row['course_category'];
  441. $params['course_language'] = $row['language'];
  442. $params['teachers'] = $row['teachers'];
  443. $courseInfo = CourseManager::create_course($params);
  444. if (!empty($courseInfo)) {
  445. CourseManager::update_course_extra_field_value($courseInfo['code'], 'external_course_id', $row['extra_'.$this->extraFieldIdNameList['course']]);
  446. $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
  447. } else {
  448. $this->logger->addError("Courses - Can't create course:".$row['title']);
  449. }
  450. } else {
  451. // Update
  452. $params = array(
  453. 'title' => $row['title'],
  454. );
  455. $result = CourseManager::update_attributes($courseInfo['real_id'], $params);
  456. //CourseManager::updateTeachers($courseInfo['id'], $row['teachers']);
  457. if ($result) {
  458. $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
  459. } else {
  460. $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
  461. }
  462. }
  463. }
  464. }
  465. $this->moveFile($file);
  466. }
  467. /**
  468. * @param string $file
  469. */
  470. private function importSessions($file)
  471. {
  472. $avoid = null;
  473. if (isset($this->conditions['importSessions']) && isset($this->conditions['importSessions']['update'])) {
  474. $avoid = $this->conditions['importSessions']['update'];
  475. }
  476. $result = SessionManager::importCSV(
  477. $file,
  478. true,
  479. $this->defaultAdminId,
  480. $this->logger,
  481. array('SessionID' => 'extra_'.$this->extraFieldIdNameList['session']),
  482. $this->extraFieldIdNameList['session'],
  483. $this->daysCoachAccessBeforeBeginning,
  484. $this->daysCoachAccessAfterBeginning,
  485. $this->defaultSessionVisibility,
  486. $avoid
  487. );
  488. if (!empty($result['error_message'])) {
  489. $this->logger->addError($result['error_message']);
  490. }
  491. $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
  492. $this->moveFile($file);
  493. }
  494. private function dumpDatabaseTables()
  495. {
  496. echo 'Dumping tables'.PHP_EOL;
  497. // User
  498. $table = Database::get_main_table(TABLE_MAIN_USER);
  499. $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
  500. //$sql = "DELETE FROM $table WHERE username NOT IN ('admin') AND lastname <> 'Anonymous' ";
  501. $sql = "DELETE FROM $table WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
  502. Database::query($sql);
  503. echo $sql.PHP_EOL;
  504. // Course
  505. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  506. $sql = "DELETE FROM $table";
  507. Database::query($sql);
  508. echo $sql.PHP_EOL;
  509. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  510. $sql = "DELETE FROM $table";
  511. Database::query($sql);
  512. echo $sql.PHP_EOL;
  513. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  514. $sql = "DELETE FROM $table";
  515. Database::query($sql);
  516. echo $sql.PHP_EOL;
  517. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  518. $sql = "DELETE FROM $table";
  519. Database::query($sql);
  520. echo $sql.PHP_EOL;
  521. // Sessions
  522. $table = Database::get_main_table(TABLE_MAIN_SESSION);
  523. $sql = "DELETE FROM $table";
  524. Database::query($sql);
  525. echo $sql.PHP_EOL;
  526. $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  527. $sql = "DELETE FROM $table";
  528. Database::query($sql);
  529. echo $sql.PHP_EOL;
  530. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  531. $sql = "DELETE FROM $table";
  532. Database::query($sql);
  533. echo $sql.PHP_EOL;
  534. $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  535. $sql = "DELETE FROM $table";
  536. Database::query($sql);
  537. echo $sql.PHP_EOL;
  538. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  539. $sql = "DELETE FROM $table";
  540. Database::query($sql);
  541. echo $sql.PHP_EOL;
  542. // Extra fields
  543. $table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  544. $sql = "DELETE FROM $table";
  545. Database::query($sql);
  546. echo $sql.PHP_EOL;
  547. $table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  548. $sql = "DELETE FROM $table";
  549. Database::query($sql);
  550. echo $sql.PHP_EOL;
  551. $table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  552. $sql = "DELETE FROM $table";
  553. Database::query($sql);
  554. echo $sql.PHP_EOL;
  555. }
  556. }
  557. use Monolog\Logger;
  558. use Monolog\Handler\StreamHandler;
  559. use Monolog\Handler\NativeMailerHandler;
  560. use Monolog\Handler\RotatingFileHandler;
  561. use Monolog\Handler\BufferHandler;
  562. $logger = new Logger('cron');
  563. $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
  564. $minLevel = Logger::DEBUG;
  565. if (!is_array($emails)) {
  566. $emails = array($emails);
  567. }
  568. $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
  569. $from = api_get_setting('emailAdministrator');
  570. if (!empty($emails)) {
  571. foreach ($emails as $email) {
  572. $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
  573. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  574. }
  575. }
  576. $stream = new StreamHandler(api_get_path(SYS_ARCHIVE_PATH).'import_csv.log', $minLevel);
  577. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  578. $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
  579. $import = new ImportCsv($logger, $_configuration['cron_import_csv_conditions']);
  580. if (isset($_configuration['default_admin_user_id_for_cron'])) {
  581. $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
  582. }
  583. // @todo in production disable the dump option
  584. $dump = false;
  585. if (isset($argv[1]) && $argv[1] = '--dump') {
  586. $dump = true;
  587. }
  588. $import->setDumpValues($dump);
  589. // Do not moves the files to treated
  590. $import->test = true;
  591. $import->run();