import_csv.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Link CSV import class definition
  5. * @package chamilo.link
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace Link;
  11. /**
  12. * Import a csv file into the course/session.
  13. *
  14. *
  15. *
  16. * @license /licence.txt
  17. * @author Laurent Opprecht <laurent@opprecht.info>
  18. */
  19. class ImportCsv
  20. {
  21. protected $c_id;
  22. protected $session_id;
  23. protected $path;
  24. protected $links_imported = 0;
  25. protected $links_skipped = 0;
  26. protected $update_existing_entries = false;
  27. public function __construct($c_id, $session_id, $path, $update_existing_entries = false)
  28. {
  29. $this->c_id = $c_id;
  30. $this->session_id = $session_id;
  31. $this->path = $path;
  32. $this->update_existing_entries = $update_existing_entries;
  33. }
  34. public function get_path()
  35. {
  36. return $this->path;
  37. }
  38. public function get_c_id()
  39. {
  40. return $this->c_id;
  41. }
  42. public function get_session_id()
  43. {
  44. return $this->session_id;
  45. }
  46. public function get_links_imported()
  47. {
  48. return $this->links_imported;
  49. }
  50. public function get_links_skipped()
  51. {
  52. return $this->links_skipped;
  53. }
  54. public function get_update_existing_entries()
  55. {
  56. return $this->update_existing_entries;
  57. }
  58. /**
  59. * Read file and returns an array filled up with its' content.
  60. *
  61. * @return array of objects
  62. */
  63. public function get_data()
  64. {
  65. $result = array();
  66. $path = $this->path;
  67. if (!is_readable($path)) {
  68. return array();
  69. }
  70. $items = \Import::csv_reader($path);
  71. foreach ($items as $item) {
  72. $item = (object) $item;
  73. $url = isset($item->url) ? trim($item->url) : '';
  74. $title = isset($item->title) ? trim($item->title) : '';
  75. $description = isset($item->description) ? trim($item->description) : '';
  76. $target = isset($item->target) ? trim($item->target) : '';
  77. $category_title = isset($item->category_title) ? trim($item->category_title) : '';
  78. $category_description = isset($item->category_description) ? trim($item->category_description) : '';
  79. if (empty($url)) {
  80. continue;
  81. }
  82. if ($category_title) {
  83. $category_title = \Security::remove_XSS($category_title);
  84. $category_description = \Security::remove_XSS($category_description);
  85. } else {
  86. $category_description = '';
  87. }
  88. $url = \Security::remove_XSS($url);
  89. $title = \Security::remove_XSS($title);
  90. $description = \Security::remove_XSS($description);
  91. $target = \Security::remove_XSS($target);
  92. $item->url = $url;
  93. $item->title = $title;
  94. $item->description = $description;
  95. $item->target = $target;
  96. $item->category_title = $category_title;
  97. $item->category_description = $category_description;
  98. $result[] = $item;
  99. }
  100. return $result;
  101. }
  102. public function run()
  103. {
  104. $path = $this->path;
  105. if (!is_readable($path)) {
  106. return false;
  107. }
  108. $this->links_imported = 0;
  109. $this->links_skipped = 0;
  110. $items = $this->get_data();
  111. foreach ($items as $item) {
  112. $url = $item->url;
  113. $title = $item->title;
  114. $description = $item->description;
  115. $target = $item->target;
  116. $category_title = $item->category_title;
  117. $category_description = $item->category_description;
  118. if ($category_title) {
  119. $category = $this->ensure_category($category_title, $category_description);
  120. }
  121. $link = $this->find_link_by_url($url);
  122. if ($link && $this->update_existing_entries == false) {
  123. $this->links_skipped++;
  124. continue;
  125. }
  126. if (empty($link)) {
  127. $link = new Link();
  128. $link->c_id = $this->c_id;
  129. $link->session_id = $this->session_id;
  130. $link->url = $url;
  131. }
  132. $link->title = $title;
  133. $link->description = $description;
  134. $link->target = $target;
  135. $link->category_id = $category ? $category->id : 0;
  136. $repo = LinkRepository::instance();
  137. $repo->save($link);
  138. $this->links_imported++;
  139. }
  140. }
  141. public function ensure_category($title, $description = '')
  142. {
  143. $c_id = $this->c_id;
  144. $session_id = $this->session_id;
  145. $repo = LinkCategoryRepository::instance();
  146. $result = $repo->find_one_by_course_and_name($c_id, $session_id, $title);
  147. if (empty($result)) {
  148. $result = new LinkCategory();
  149. $result->c_id = $c_id;
  150. $result->category_title = $title;
  151. $result->description = $description;
  152. $result->session_id = $session_id;
  153. $repo->save($result);
  154. }
  155. return $result;
  156. }
  157. public function find_link_by_url($url)
  158. {
  159. $c_id = $this->c_id;
  160. $session_id = $this->session_id;
  161. $repo = LinkRepository::instance();
  162. $link = $repo->find_one_by_course_and_url($c_id, $session_id, $url);
  163. return $link;
  164. }
  165. }