/lib/Recipe/Language/Importer.php

https://github.com/Basti-sama/Bengine · PHP · 292 lines · 172 code · 19 blank · 101 comment · 20 complexity · cb1430411e4329b270afa21b730bf432 MD5 · raw file

  1. <?php
  2. /**
  3. * This class imports language files into the database.
  4. *
  5. * @package Recipe 1.2
  6. * @author Sebastian Noll
  7. * @copyright Copyright (c) 2009, Sebastian Noll
  8. * @license Proprietary
  9. * @version $Id: Importer.php 8 2010-10-17 20:55:04Z secretchampion $
  10. */
  11. class Recipe_Language_Importer
  12. {
  13. /**
  14. * Holds data to import.
  15. *
  16. * @var mixed
  17. */
  18. protected $importData = array();
  19. /**
  20. * Language id.
  21. *
  22. * @var integer
  23. */
  24. protected $langId = 0;
  25. /**
  26. * Phrase group id.
  27. *
  28. * @var integer
  29. */
  30. protected $groupId = 0;
  31. /**
  32. * Constructor.
  33. *
  34. * @param mixed $data File name / File content / Array
  35. * @param mixed $lang Language code or id.
  36. * @param mixed $group
  37. * @throws Recipe_Exception_Generic
  38. */
  39. public function __construct($data, $lang, $group)
  40. {
  41. // Find out which language we got
  42. if(is_numeric($lang))
  43. {
  44. try { $this->getFromLangFromId($lang); }
  45. catch(Recipe_Exception_Generic $e) { $e->printError(); }
  46. }
  47. else
  48. {
  49. try { $this->getFromLangCode($lang); }
  50. catch(Recipe_Exception_Generic $e) { $e->printError(); }
  51. }
  52. // Find out which phrase group we got
  53. if(is_numeric($group))
  54. {
  55. try { $this->getGroupFromId($group); }
  56. catch(Recipe_Exception_Generic $e) { $e->printError(); }
  57. }
  58. else
  59. {
  60. try { $this->getFromGroupName($group); }
  61. catch(Recipe_Exception_Generic $e) { $e->printError(); }
  62. }
  63. // Check which type the import data is
  64. if(is_array($data))
  65. {
  66. $this->importData = $data;
  67. }
  68. else if(file_exists($data))
  69. {
  70. try { $this->getDataFromFile($data); }
  71. catch(Recipe_Exception_Generic $e) { $e->printError(); }
  72. }
  73. else
  74. {
  75. throw new Recipe_Exception_Generic("Unkown data supplyed for importer.");
  76. }
  77. // Start import
  78. try { $this->import(); }
  79. catch(Recipe_Exception_Generic $e) { $e->printError(); }
  80. return;
  81. }
  82. /**
  83. * Actual import function.
  84. *
  85. * @throws Recipe_Exception_Generic
  86. * @return Recipe_Language_Importer
  87. */
  88. protected function import()
  89. {
  90. // Check for valid import data
  91. if(!is_array($this->importData))
  92. {
  93. throw new Recipe_Exception_Generic("Supplied data for importer is not an array.");
  94. }
  95. // Importing ...
  96. foreach($this->importData as $key => $value)
  97. {
  98. $where = Core::getDB()->quoteInto("languageid = ? ", $this->langId);
  99. $where .= Core::getDB()->quoteInto("AND phrasegroupid = ? ", $this->groupId);
  100. $where .= Core::getDB()->quoteInto("AND title = ?", $key);
  101. $stmt = Core::getQuery()->select("phrases", array("phraseid"), "", $where);
  102. $row = $stmt->fetchRow();
  103. if(!empty($row["phraseid"]))
  104. {
  105. // Phrase already exists >> update
  106. $where = Core::getDB()->quoteInto("phraseid = ? ", $key);
  107. Core::getQuery()->update("phrases", array("content" => $value), $where);
  108. }
  109. else
  110. {
  111. // Phrase does not exist >> insert
  112. Core::getQuery()->insert("phrases", array("languageid" => $this->langId, "phrasegroupid" => $this->groupId, "title" => $key, "content" => $value));
  113. }
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Loads import data from a file.
  119. *
  120. * @param string $file Path to file
  121. * @throws Recipe_Exception_Generic
  122. * @return Recipe_Language_Importer
  123. */
  124. protected function getDataFromFile($file)
  125. {
  126. if(!file_exists($file))
  127. {
  128. throw new Recipe_Exception_Generic("Could not found import file. Make sure the path is correct or try absolute path. Given path: ".$file);
  129. }
  130. // If import file is PHP we can require it
  131. if(preg_match("/^.+\.php$/i", $file))
  132. {
  133. $item = array();
  134. require_once($file);
  135. $this->importData = $item;
  136. return $this;
  137. }
  138. // Get the file's contents
  139. $data = file_get_contents($file);
  140. $this->extractFromText($data);
  141. return $this;
  142. }
  143. /**
  144. * Extract the contents of an import file into an array.
  145. *
  146. * @param string $text Text content
  147. *
  148. * @return Recipe_Language_Importer
  149. */
  150. protected function extractFromText($text)
  151. {
  152. // First of all extract rows
  153. $row = explode("\r\n", $text);
  154. // Extract phrase group from phrase
  155. $size = count($row);
  156. for($i = 0; $i < $size; $i++)
  157. {
  158. $cell = explode("=>", $row[$i]);
  159. $this->importData[$cell[0]] = $cell[1];
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Checks the given language id for validation.
  165. *
  166. * @param integer $lang Language id
  167. * @throws Recipe_Exception_Generic
  168. * @return Recipe_Language_Importer
  169. */
  170. protected function getFromLangFromId($lang)
  171. {
  172. $result = Core::getQuery()->select("languages", array("languageid"), "", Core::getDB()->quoteInto("languageid = ?", $lang));
  173. if($result->rowCount())
  174. {
  175. $this->langId = $lang;
  176. return $this;
  177. }
  178. throw new Recipe_Exception_Generic("The given language id does not exist. Please create language first before import data.");
  179. }
  180. /**
  181. * Gets the language id for given language code.
  182. *
  183. * @param string $lang Language code
  184. * @param mixed $createData
  185. * @throws Recipe_Exception_Generic
  186. * @return Recipe_Language_Importer
  187. */
  188. protected function getFromLangCode($lang, $createData = null)
  189. {
  190. $result = Core::getQuery()->select("languages", array("languageid"), "", Core::getDB()->quoteInto("langcode = ?", $lang));
  191. $row = $result->fetchRow();
  192. $result->closeCursor();
  193. if(!$row)
  194. {
  195. if(is_array($createData))
  196. {
  197. $spec = array_combine(array("langcode", "title"), $createData);
  198. Core::getQuery()->insert("languages", $spec);
  199. $this->langId = Core::getDB()->lastInsertId();
  200. return $this;
  201. }
  202. else
  203. {
  204. throw new Recipe_Exception_Generic("The given language code does not exist. Please create language first before import data.");
  205. }
  206. }
  207. $this->langId = $row["languageid"];
  208. return $this;
  209. }
  210. /**
  211. * Checks the given phrase group id for validation.
  212. *
  213. * @param string $group Group id
  214. * @throws Recipe_Exception_Generic
  215. * @return Recipe_Language_Importer
  216. */
  217. protected function getGroupFromId($group)
  218. {
  219. $result = Core::getQuery()->select("phrasesgroups", array("phrasegroupid"), "", Core::getDB()->quoteInto("phrasegroupid = ?", $group));
  220. if($result->rowCount() > 0)
  221. {
  222. $this->groupId = $group;
  223. return $this;
  224. }
  225. throw new Recipe_Exception_Generic("The given phrase group does not exist. Please create group first before import data.");
  226. }
  227. /**
  228. * Gets the phrase group id for given group name.
  229. *
  230. * @param string $group Group name
  231. *
  232. * @return Recipe_Language_Importer
  233. */
  234. protected function getFromGroupName($group)
  235. {
  236. $result = Core::getQuery()->select("phrasesgroups", array("phrasegroupid"), "", Core::getDB()->quoteInto("title = ?", $group));
  237. $row = $result->fetchRow();
  238. $result->closeCursor();
  239. if(!$row)
  240. {
  241. // Create unknown group
  242. Core::getQuery()->insert("phrasesgroups", array("title" => $group));
  243. $this->groupId = Core::getDB()->lastInsertId();
  244. return $this;
  245. }
  246. $this->groupId = $row["phrasegroupid"];
  247. return $this;
  248. }
  249. /**
  250. * Sets the default language.
  251. *
  252. * @param mixed $langId Language code or id
  253. *
  254. * @return Recipe_Language_Importer
  255. */
  256. public function setDefaultLanguage($langId)
  257. {
  258. if(!is_numeric($langId))
  259. {
  260. $result = Core::getQuery()->select("languages", array("languageid"), "", Core::getDB()->quoteInto("langcode = ?", $langId), "", "1");
  261. if($row = $result->fetchRow())
  262. {
  263. $langId = $row["languageid"];
  264. }
  265. }
  266. if(is_numeric($langId))
  267. {
  268. Core::getConfig()->set("defaultlanguage", $langId);
  269. }
  270. return $this;
  271. }
  272. }
  273. ?>