/pimcore/models/Tool/CustomReport/Config.php

https://github.com/timglabisch/pimcore · PHP · 478 lines · 239 code · 73 blank · 166 comment · 22 complexity · 6585b26c083db0934b88491067cb8a76 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.pimcore.org/license
  11. *
  12. * @category Pimcore
  13. * @package Tool
  14. * @copyright Copyright (c) 2009-2013 pimcore GmbH (http://www.pimcore.org)
  15. * @license http://www.pimcore.org/license New BSD License
  16. */
  17. class Tool_CustomReport_Config {
  18. /**
  19. * @var string
  20. */
  21. public $name = "";
  22. /**
  23. * @var string
  24. */
  25. public $sql = "";
  26. /**
  27. * @var string[]
  28. */
  29. public $dataSourceConfig = array();
  30. /**
  31. * @var array
  32. */
  33. public $columnConfiguration = array();
  34. /**
  35. * @var string
  36. */
  37. public $niceName = "";
  38. /**
  39. * @var string
  40. */
  41. public $group = "";
  42. /**
  43. * @var string
  44. */
  45. public $groupIconClass = "";
  46. /**
  47. * @var string
  48. */
  49. public $iconClass = "";
  50. /**
  51. * @var bool
  52. */
  53. public $menuShortcut;
  54. /**
  55. * @var string
  56. */
  57. public $chartType;
  58. /**
  59. * @var string
  60. */
  61. public $pieColumn;
  62. /**
  63. * @var string
  64. */
  65. public $pieLabelColumn;
  66. /**
  67. * @var string
  68. */
  69. public $xAxis;
  70. /**
  71. * @var string|array
  72. */
  73. public $yAxis;
  74. /**
  75. * @param $name
  76. * @return Tool_CustomReport_Config
  77. * @throws Exception
  78. */
  79. public static function getByName ($name) {
  80. $code = new self();
  81. $code->setName($name);
  82. if(!$code->load()) {
  83. throw new Exception("sql report definition : " . $name . " does not exist");
  84. }
  85. return $code;
  86. }
  87. /**
  88. * @static
  89. * @return string
  90. */
  91. public static function getWorkingDir () {
  92. $dir = PIMCORE_CONFIGURATION_DIRECTORY . "/sqlreport";
  93. if(!is_dir($dir)) {
  94. mkdir($dir);
  95. }
  96. return $dir;
  97. }
  98. /**
  99. * @return void
  100. */
  101. public function save () {
  102. $arrayConfig = object2array($this);
  103. $items = $arrayConfig["columnConfiguration"];
  104. $arrayConfig["columnConfiguration"] = array("columnConfiguration" => $items);
  105. if($arrayConfig["dataSourceConfig"]) {
  106. $configArray = array();
  107. foreach($arrayConfig["dataSourceConfig"] as $config) {
  108. $configArray[] = json_encode($config);
  109. }
  110. $arrayConfig["dataSourceConfig"] = array("dataSourceConfig" => $configArray);
  111. } else {
  112. $arrayConfig["dataSourceConfig"] = array("dataSourceConfig" => array());
  113. }
  114. $items = $arrayConfig["yAxis"];
  115. $arrayConfig["yAxis"] = array("yAxis" => $items);
  116. $config = new Zend_Config($arrayConfig);
  117. $writer = new Zend_Config_Writer_Xml(array(
  118. "config" => $config,
  119. "filename" => $this->getConfigFile()
  120. ));
  121. $writer->write();
  122. return true;
  123. }
  124. /**
  125. * @return void
  126. */
  127. public function load () {
  128. $configXml = new Zend_Config_Xml($this->getConfigFile());
  129. $configArray = $configXml->toArray();
  130. if(array_key_exists("columnConfiguration",$configArray) && is_array($configArray["columnConfiguration"]["columnConfiguration"])) {
  131. if(array_key_exists("method",$configArray["columnConfiguration"]["columnConfiguration"])) {
  132. $configArray["columnConfiguration"] = array($configArray["columnConfiguration"]["columnConfiguration"]);
  133. } else {
  134. $configArray["columnConfiguration"] = $configArray["columnConfiguration"]["columnConfiguration"];
  135. }
  136. } else {
  137. $configArray["columnConfiguration"] = array("columnConfiguration" => array());
  138. }
  139. if(array_key_exists("dataSourceConfig",$configArray) && is_array($configArray["dataSourceConfig"])) {
  140. $dataSourceConfig = array();
  141. foreach($configArray["dataSourceConfig"] as $c) {
  142. if($c) {
  143. $dataSourceConfig[] = json_decode($c);
  144. }
  145. }
  146. $configArray["dataSourceConfig"] = $dataSourceConfig;
  147. } else {
  148. $configArray["dataSourceConfig"] = array();
  149. }
  150. if(array_key_exists("yAxis",$configArray) && is_array($configArray["yAxis"])) {
  151. if(!is_array($configArray["yAxis"]["yAxis"])) {
  152. $configArray["yAxis"] = array($configArray["yAxis"]["yAxis"]);
  153. } else {
  154. $configArray["yAxis"] = $configArray["yAxis"]["yAxis"];
  155. }
  156. }
  157. // to preserve compatibility to older sql reports
  158. if($configArray["sql"] && empty($configArray["dataSourceConfig"])) {
  159. $legacy = new stdClass();
  160. $legacy->type = "sql";
  161. $legacy->sql = $configArray["sql"];
  162. $configArray["dataSourceConfig"][] = $legacy;
  163. }
  164. foreach ($configArray as $key => $value) {
  165. $setter = "set" . ucfirst($key);
  166. if(method_exists($this, $setter)) {
  167. $this->$setter($value);
  168. }
  169. }
  170. return true;
  171. }
  172. /**
  173. * @return array
  174. */
  175. public function getReportsList () {
  176. $dir = Tool_CustomReport_Config::getWorkingDir();
  177. $reports = array();
  178. $files = scandir($dir);
  179. foreach ($files as $file) {
  180. if(strpos($file, ".xml")) {
  181. $name = str_replace(".xml", "", $file);
  182. $reports[] = array(
  183. "id" => $name,
  184. "text" => $name
  185. );
  186. }
  187. }
  188. return $reports;
  189. }
  190. public function getAdapter($configuration, $fullConfig = null) {
  191. $type = $configuration->type ? ucfirst($configuration->type) : 'Sql';
  192. $adapter = "Tool_CustomReport_Adapter_{$type}";
  193. return new $adapter($configuration, $fullConfig);
  194. }
  195. /**
  196. * @return void
  197. */
  198. public function delete() {
  199. if(is_file($this->getConfigFile())) {
  200. unlink($this->getConfigFile());
  201. }
  202. }
  203. /**
  204. * @return string
  205. */
  206. protected function getConfigFile () {
  207. return self::getWorkingDir() . "/" . $this->getName() . ".xml";
  208. }
  209. /**
  210. * @param string $name
  211. */
  212. public function setName($name)
  213. {
  214. $this->name = $name;
  215. }
  216. /**
  217. * @return string
  218. */
  219. public function getName()
  220. {
  221. return $this->name;
  222. }
  223. /**
  224. * @param string $sql
  225. */
  226. public function setSql($sql)
  227. {
  228. $this->sql = $sql;
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getSql()
  234. {
  235. return $this->sql;
  236. }
  237. /**
  238. * @param array $columnConfiguration
  239. */
  240. public function setColumnConfiguration($columnConfiguration)
  241. {
  242. $this->columnConfiguration = $columnConfiguration;
  243. }
  244. /**
  245. * @return array
  246. */
  247. public function getColumnConfiguration()
  248. {
  249. return $this->columnConfiguration;
  250. }
  251. /**
  252. * @param string $group
  253. */
  254. public function setGroup($group)
  255. {
  256. $this->group = $group;
  257. }
  258. /**
  259. * @return string
  260. */
  261. public function getGroup()
  262. {
  263. return $this->group;
  264. }
  265. /**
  266. * @param string $groupIconClass
  267. */
  268. public function setGroupIconClass($groupIconClass)
  269. {
  270. $this->groupIconClass = $groupIconClass;
  271. }
  272. /**
  273. * @return string
  274. */
  275. public function getGroupIconClass()
  276. {
  277. return $this->groupIconClass;
  278. }
  279. /**
  280. * @param string $iconClass
  281. */
  282. public function setIconClass($iconClass)
  283. {
  284. $this->iconClass = $iconClass;
  285. }
  286. /**
  287. * @return string
  288. */
  289. public function getIconClass()
  290. {
  291. return $this->iconClass;
  292. }
  293. /**
  294. * @param string $niceName
  295. */
  296. public function setNiceName($niceName)
  297. {
  298. $this->niceName = $niceName;
  299. }
  300. /**
  301. * @return string
  302. */
  303. public function getNiceName()
  304. {
  305. return $this->niceName;
  306. }
  307. /**
  308. * @param boolean $menuShortcut
  309. */
  310. public function setMenuShortcut($menuShortcut)
  311. {
  312. $this->menuShortcut = (bool) $menuShortcut;
  313. }
  314. /**
  315. * @return boolean
  316. */
  317. public function getMenuShortcut()
  318. {
  319. return $this->menuShortcut;
  320. }
  321. /**
  322. * @param \string[] $dataSourceConfig
  323. */
  324. public function setDataSourceConfig($dataSourceConfig)
  325. {
  326. $this->dataSourceConfig = $dataSourceConfig;
  327. }
  328. /**
  329. * @return \string[]
  330. */
  331. public function getDataSourceConfig()
  332. {
  333. return $this->dataSourceConfig;
  334. }
  335. /**
  336. * @param string $chartType
  337. */
  338. public function setChartType($chartType)
  339. {
  340. $this->chartType = $chartType;
  341. }
  342. /**
  343. * @return string
  344. */
  345. public function getChartType()
  346. {
  347. return $this->chartType;
  348. }
  349. /**
  350. * @param string $pieColumn
  351. */
  352. public function setPieColumn($pieColumn)
  353. {
  354. $this->pieColumn = $pieColumn;
  355. }
  356. /**
  357. * @return string
  358. */
  359. public function getPieColumn()
  360. {
  361. return $this->pieColumn;
  362. }
  363. /**
  364. * @param string $xAxis
  365. */
  366. public function setXAxis($xAxis)
  367. {
  368. $this->xAxis = $xAxis;
  369. }
  370. /**
  371. * @return string
  372. */
  373. public function getXAxis()
  374. {
  375. return $this->xAxis;
  376. }
  377. /**
  378. * @param array|string $yAxis
  379. */
  380. public function setYAxis($yAxis)
  381. {
  382. $this->yAxis = $yAxis;
  383. }
  384. /**
  385. * @return array|string
  386. */
  387. public function getYAxis()
  388. {
  389. return $this->yAxis;
  390. }
  391. /**
  392. * @param string $pieLabelColumn
  393. */
  394. public function setPieLabelColumn($pieLabelColumn)
  395. {
  396. $this->pieLabelColumn = $pieLabelColumn;
  397. }
  398. /**
  399. * @return string
  400. */
  401. public function getPieLabelColumn()
  402. {
  403. return $this->pieLabelColumn;
  404. }
  405. }