/models/Tool/CustomReport/Config.php

https://github.com/pimcore/pimcore · PHP · 586 lines · 274 code · 84 blank · 228 comment · 10 complexity · 1eaf2d283063f60ce0b6a38ae0e50014 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\Tool\CustomReport;
  15. use Pimcore\Model;
  16. /**
  17. * @internal
  18. *
  19. * @method bool isWriteable()
  20. * @method string getWriteTarget()
  21. * @method void delete()
  22. * @method void save()
  23. */
  24. class Config extends Model\AbstractModel implements \JsonSerializable
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $name = '';
  30. /**
  31. * @var string
  32. */
  33. protected $sql = '';
  34. /**
  35. * @var array
  36. */
  37. protected $dataSourceConfig = [];
  38. /**
  39. * @var array
  40. */
  41. protected $columnConfiguration = [];
  42. /**
  43. * @var string
  44. */
  45. protected $niceName = '';
  46. /**
  47. * @var string
  48. */
  49. protected $group = '';
  50. /**
  51. * @var string
  52. */
  53. protected $groupIconClass = '';
  54. /**
  55. * @var string
  56. */
  57. protected $iconClass = '';
  58. /**
  59. * @var bool
  60. */
  61. protected $menuShortcut = true;
  62. /**
  63. * @var string
  64. */
  65. protected $reportClass = '';
  66. /**
  67. * @var string
  68. */
  69. protected $chartType = '';
  70. /**
  71. * @var string
  72. */
  73. protected $pieColumn = '';
  74. /**
  75. * @var string
  76. */
  77. protected $pieLabelColumn = '';
  78. /**
  79. * @var string
  80. */
  81. protected $xAxis = '';
  82. /**
  83. * @var string|array
  84. */
  85. protected $yAxis = [];
  86. /**
  87. * @var int|null
  88. */
  89. protected $modificationDate;
  90. /**
  91. * @var int|null
  92. */
  93. protected $creationDate;
  94. /**
  95. * @var bool
  96. */
  97. protected $shareGlobally = true;
  98. /**
  99. * @var string[]
  100. */
  101. protected $sharedUserNames = [];
  102. /**
  103. * @var string[]
  104. */
  105. protected $sharedRoleNames = [];
  106. /**
  107. * @param string $name
  108. *
  109. * @return null|Config
  110. *
  111. * @throws \Exception
  112. */
  113. public static function getByName($name)
  114. {
  115. try {
  116. $report = new self();
  117. /** @var Model\Tool\CustomReport\Config\Dao $dao */
  118. $dao = $report->getDao();
  119. $dao->getByName($name);
  120. return $report;
  121. } catch (Model\Exception\NotFoundException $e) {
  122. return null;
  123. }
  124. }
  125. /**
  126. * @param Model\User|null $user
  127. *
  128. * @return array
  129. */
  130. public static function getReportsList(Model\User $user = null)
  131. {
  132. $reports = [];
  133. $list = new Config\Listing();
  134. if ($user) {
  135. $items = $list->getDao()->loadForGivenUser($user);
  136. } else {
  137. $items = $list->getDao()->loadList();
  138. }
  139. foreach ($items as $item) {
  140. $reports[] = [
  141. 'id' => $item->getName(),
  142. 'text' => $item->getName(),
  143. 'cls' => 'pimcore_treenode_disabled',
  144. 'writeable' => $item->isWriteable(),
  145. ];
  146. }
  147. return $reports;
  148. }
  149. /**
  150. * @param \stdClass $configuration
  151. * @param Config|null $fullConfig
  152. *
  153. * @deprecated Use ServiceLocator with id 'pimcore.custom_report.adapter.factories' to determine the factory for the adapter instead
  154. *
  155. * @return Model\Tool\CustomReport\Adapter\CustomReportAdapterInterface
  156. */
  157. public static function getAdapter($configuration, $fullConfig = null)
  158. {
  159. $type = $configuration->type ? $configuration->type : 'sql';
  160. $serviceLocator = \Pimcore::getContainer()->get('pimcore.custom_report.adapter.factories');
  161. if (!$serviceLocator->has($type)) {
  162. throw new \RuntimeException(sprintf('Could not find Custom Report Adapter with type %s', $type));
  163. }
  164. /** @var Model\Tool\CustomReport\Adapter\CustomReportAdapterFactoryInterface $factory */
  165. $factory = $serviceLocator->get($type);
  166. return $factory->create($configuration, $fullConfig);
  167. }
  168. /**
  169. * @param string $name
  170. */
  171. public function setName($name)
  172. {
  173. $this->name = $name;
  174. }
  175. /**
  176. * @return string
  177. */
  178. public function getName()
  179. {
  180. return $this->name;
  181. }
  182. /**
  183. * @param string $sql
  184. */
  185. public function setSql($sql)
  186. {
  187. $this->sql = $sql;
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function getSql()
  193. {
  194. return $this->sql;
  195. }
  196. /**
  197. * @param array $columnConfiguration
  198. */
  199. public function setColumnConfiguration($columnConfiguration)
  200. {
  201. $this->columnConfiguration = $columnConfiguration;
  202. }
  203. /**
  204. * @return array
  205. */
  206. public function getColumnConfiguration()
  207. {
  208. return $this->columnConfiguration;
  209. }
  210. /**
  211. * @param string $group
  212. */
  213. public function setGroup($group)
  214. {
  215. $this->group = $group;
  216. }
  217. /**
  218. * @return string
  219. */
  220. public function getGroup()
  221. {
  222. return $this->group;
  223. }
  224. /**
  225. * @param string $groupIconClass
  226. */
  227. public function setGroupIconClass($groupIconClass)
  228. {
  229. $this->groupIconClass = $groupIconClass;
  230. }
  231. /**
  232. * @return string
  233. */
  234. public function getGroupIconClass()
  235. {
  236. return $this->groupIconClass;
  237. }
  238. /**
  239. * @param string $iconClass
  240. */
  241. public function setIconClass($iconClass)
  242. {
  243. $this->iconClass = $iconClass;
  244. }
  245. /**
  246. * @return string
  247. */
  248. public function getIconClass()
  249. {
  250. return $this->iconClass;
  251. }
  252. /**
  253. * @param string $niceName
  254. */
  255. public function setNiceName($niceName)
  256. {
  257. $this->niceName = $niceName;
  258. }
  259. /**
  260. * @return string
  261. */
  262. public function getNiceName()
  263. {
  264. return $this->niceName;
  265. }
  266. /**
  267. * @param bool $menuShortcut
  268. */
  269. public function setMenuShortcut($menuShortcut)
  270. {
  271. $this->menuShortcut = (bool) $menuShortcut;
  272. }
  273. /**
  274. * @return bool
  275. */
  276. public function getMenuShortcut()
  277. {
  278. return $this->menuShortcut;
  279. }
  280. /**
  281. * @param array $dataSourceConfig
  282. */
  283. public function setDataSourceConfig($dataSourceConfig)
  284. {
  285. $this->dataSourceConfig = $dataSourceConfig;
  286. }
  287. /**
  288. * @return \stdClass|null
  289. */
  290. public function getDataSourceConfig()
  291. {
  292. if (is_array($this->dataSourceConfig) && isset($this->dataSourceConfig[0])) {
  293. $dataSourceConfig = new \stdClass();
  294. $dataSourceConfigArray = $this->dataSourceConfig[0];
  295. foreach ($dataSourceConfigArray as $key => $value) {
  296. $dataSourceConfig->$key = $value;
  297. }
  298. return $dataSourceConfig;
  299. }
  300. return null;
  301. }
  302. /**
  303. * @param string $chartType
  304. */
  305. public function setChartType($chartType)
  306. {
  307. $this->chartType = $chartType;
  308. }
  309. /**
  310. * @return string
  311. */
  312. public function getChartType()
  313. {
  314. return $this->chartType;
  315. }
  316. /**
  317. * @param string $pieColumn
  318. */
  319. public function setPieColumn($pieColumn)
  320. {
  321. $this->pieColumn = $pieColumn;
  322. }
  323. /**
  324. * @return string
  325. */
  326. public function getPieColumn()
  327. {
  328. return $this->pieColumn;
  329. }
  330. /**
  331. * @param string $xAxis
  332. */
  333. public function setXAxis($xAxis)
  334. {
  335. $this->xAxis = $xAxis;
  336. }
  337. /**
  338. * @return string
  339. */
  340. public function getXAxis()
  341. {
  342. return $this->xAxis;
  343. }
  344. /**
  345. * @param array|string $yAxis
  346. */
  347. public function setYAxis($yAxis)
  348. {
  349. $this->yAxis = $yAxis;
  350. }
  351. /**
  352. * @return array|string
  353. */
  354. public function getYAxis()
  355. {
  356. return $this->yAxis;
  357. }
  358. /**
  359. * @param string $pieLabelColumn
  360. */
  361. public function setPieLabelColumn($pieLabelColumn)
  362. {
  363. $this->pieLabelColumn = $pieLabelColumn;
  364. }
  365. /**
  366. * @return string
  367. */
  368. public function getPieLabelColumn()
  369. {
  370. return $this->pieLabelColumn;
  371. }
  372. /**
  373. * @return int|null
  374. */
  375. public function getModificationDate()
  376. {
  377. return $this->modificationDate;
  378. }
  379. /**
  380. * @param int $modificationDate
  381. */
  382. public function setModificationDate($modificationDate)
  383. {
  384. $this->modificationDate = $modificationDate;
  385. }
  386. /**
  387. * @return int|null
  388. */
  389. public function getCreationDate()
  390. {
  391. return $this->creationDate;
  392. }
  393. /**
  394. * @param int $creationDate
  395. */
  396. public function setCreationDate($creationDate)
  397. {
  398. $this->creationDate = $creationDate;
  399. }
  400. /**
  401. * @return string
  402. */
  403. public function getReportClass()
  404. {
  405. return $this->reportClass;
  406. }
  407. /**
  408. * @param string $reportClass
  409. */
  410. public function setReportClass($reportClass)
  411. {
  412. $this->reportClass = $reportClass;
  413. }
  414. /**
  415. * @return bool
  416. */
  417. public function getShareGlobally()
  418. {
  419. return $this->shareGlobally;
  420. }
  421. /**
  422. * @param bool $shareGlobally
  423. */
  424. public function setShareGlobally($shareGlobally): void
  425. {
  426. $this->shareGlobally = $shareGlobally;
  427. }
  428. /**
  429. * @return int[]
  430. */
  431. public function getSharedUserIds()
  432. {
  433. $sharedUserIds = [];
  434. if ($this->sharedUserNames) {
  435. foreach ($this->sharedUserNames as $username) {
  436. $user = Model\User::getByName($username);
  437. if ($user) {
  438. $sharedUserIds[] = $user->getId();
  439. }
  440. }
  441. }
  442. return $sharedUserIds;
  443. }
  444. /**
  445. * @return int[]
  446. */
  447. public function getSharedRoleIds()
  448. {
  449. $sharedRoleIds = [];
  450. if ($this->sharedRoleNames) {
  451. foreach ($this->sharedRoleNames as $name) {
  452. $role = Model\User\Role::getByName($name);
  453. if ($role) {
  454. $sharedRoleIds[] = $role->getId();
  455. }
  456. }
  457. }
  458. return $sharedRoleIds;
  459. }
  460. /**
  461. * @return string[]
  462. */
  463. public function getSharedUserNames()
  464. {
  465. return $this->sharedUserNames;
  466. }
  467. /**
  468. * @param string[] $sharedUserNames
  469. */
  470. public function setSharedUserNames($sharedUserNames): void
  471. {
  472. $this->sharedUserNames = $sharedUserNames;
  473. }
  474. /**
  475. * @return string[]
  476. */
  477. public function getSharedRoleNames()
  478. {
  479. return $this->sharedRoleNames;
  480. }
  481. /**
  482. * @param string[] $sharedRoleNames
  483. */
  484. public function setSharedRoleNames($sharedRoleNames): void
  485. {
  486. $this->sharedRoleNames = $sharedRoleNames;
  487. }
  488. public function jsonSerialize(): array
  489. {
  490. $data = $this->getObjectVars();
  491. $data['sharedUserIds'] = $this->getSharedUserIds();
  492. $data['sharedRoleIds'] = $this->getSharedRoleIds();
  493. return $data;
  494. }
  495. public function __clone()
  496. {
  497. if ($this->dao) {
  498. $this->dao = clone $this->dao;
  499. $this->dao->setModel($this);
  500. }
  501. }
  502. }