PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/landing/lib/internals/base.php

https://gitlab.com/alexprowars/bitrix
PHP | 185 lines | 129 code | 16 blank | 40 comment | 21 complexity | c3d37b645af5a9ba5b3fa0293de64deb MD5 | raw file
  1. <?php
  2. namespace Bitrix\Landing\Internals;
  3. use \Bitrix\Landing\Manager;
  4. class BaseTable
  5. {
  6. public static $internalClass = null;
  7. /**
  8. * Get internal class (must declarated in external class).
  9. * @return string
  10. */
  11. private static function getCallingClass()
  12. {
  13. if (static::$internalClass === null)
  14. {
  15. throw new \Bitrix\Main\SystemException(
  16. 'Variable static::$internalClass must be declarated in external class.'
  17. );
  18. }
  19. return '\\' . __NAMESPACE__ . '\\' . static::$internalClass;
  20. }
  21. /**
  22. * Get Map of table.
  23. * @return array
  24. */
  25. public static function getMap()
  26. {
  27. /** @var \Bitrix\Main\ORM\Data\DataManager $class */
  28. $class = self::getCallingClass();
  29. return $class::getMap();
  30. }
  31. /**
  32. * Create new record and return it new id.
  33. * @param array $fields Fields array.
  34. * @return \Bitrix\Main\ORM\Data\AddResult
  35. */
  36. public static function add($fields)
  37. {
  38. $uid = Manager::getUserId();
  39. $uid = $uid ? $uid : 1;
  40. $date = new \Bitrix\Main\Type\DateTime;
  41. $charValue = array(
  42. 'ACTIVE', 'PUBLIC', 'SITEMAP', 'FOLDER'
  43. );
  44. foreach ($charValue as $code)
  45. {
  46. if (isset($fields[$code]) && $fields[$code] != 'Y')
  47. {
  48. $fields[$code] = 'N';
  49. }
  50. }
  51. if (!isset($fields['CREATED_BY_ID']))
  52. {
  53. $fields['CREATED_BY_ID'] = $uid;
  54. }
  55. if (!isset($fields['MODIFIED_BY_ID']))
  56. {
  57. $fields['MODIFIED_BY_ID'] = $uid;
  58. }
  59. if (!isset($fields['DATE_CREATE']))
  60. {
  61. $fields['DATE_CREATE'] = $date;
  62. }
  63. if (!isset($fields['DATE_MODIFY']))
  64. {
  65. $fields['DATE_MODIFY'] = $date;
  66. }
  67. /** @var \Bitrix\Main\ORM\Data\DataManager $class */
  68. $class = self::getCallingClass();
  69. return $class::add($fields);
  70. }
  71. /**
  72. * Update record.
  73. * @param int $id Record key.
  74. * @param array $fields Fields array.
  75. * @return \Bitrix\Main\Result
  76. */
  77. public static function update($id, $fields = array())
  78. {
  79. $uid = Manager::getUserId();
  80. $date = new \Bitrix\Main\Type\DateTime;
  81. $charValue = array(
  82. 'ACTIVE', 'PUBLIC', 'SITEMAP', 'FOLDER'
  83. );
  84. foreach ($charValue as $code)
  85. {
  86. if (isset($fields[$code]) && $fields[$code] != 'Y')
  87. {
  88. $fields[$code] = 'N';
  89. }
  90. }
  91. if (isset($fields['ID']))
  92. {
  93. unset($fields['ID']);
  94. }
  95. if (!isset($fields['MODIFIED_BY_ID']))
  96. {
  97. $fields['MODIFIED_BY_ID'] = $uid;
  98. }
  99. else if (!$fields['MODIFIED_BY_ID'])
  100. {
  101. unset($fields['MODIFIED_BY_ID']);
  102. }
  103. if (!isset($fields['DATE_MODIFY']))
  104. {
  105. $fields['DATE_MODIFY'] = $date;
  106. }
  107. if (!$fields['DATE_MODIFY'])
  108. {
  109. unset($fields['DATE_MODIFY']);
  110. }
  111. /** @var \Bitrix\Main\ORM\Data\DataManager $class */
  112. $class = self::getCallingClass();
  113. return $class::update($id, $fields);
  114. }
  115. /**
  116. * Delete record.
  117. * @param int $id Record key.
  118. * @return \Bitrix\Main\Result
  119. */
  120. public static function delete($id)
  121. {
  122. /** @var \Bitrix\Main\ORM\Data\DataManager $class */
  123. $class = self::getCallingClass();
  124. \Bitrix\Landing\Debug::log(
  125. $class,
  126. 'id: ' . $id . '@' . print_r(\Bitrix\Main\Diag\Helper::getBackTrace(5), true),
  127. 'LANDING_ENTITY_DELETE'
  128. );
  129. return $class::delete($id);
  130. }
  131. /**
  132. * Get records of table.
  133. * @param array $params Params array like ORM style.
  134. * @return \Bitrix\Main\DB\Result
  135. */
  136. public static function getList($params = array())
  137. {
  138. $class = self::getCallingClass();
  139. if (method_exists($class, 'setAccessFilter'))
  140. {
  141. $params = $class::setAccessFilter($params);
  142. }
  143. /** @var \Bitrix\Main\ORM\Data\DataManager $class */
  144. return $class::getList($params);
  145. }
  146. /**
  147. * Register calllback for internal table.
  148. * @param string $code Type of callback.
  149. * @param callable $callback Callback.
  150. * @return void
  151. */
  152. public static function callback($code, $callback)
  153. {
  154. $class = self::getCallingClass();
  155. if (mb_substr(mb_strtolower($class), -5) == 'table')
  156. {
  157. $class = mb_substr($class, 0, -5);
  158. if ($class)
  159. {
  160. $eventManager = \Bitrix\Main\EventManager::getInstance();
  161. $eventManager->addEventHandler(
  162. 'landing',
  163. $class . '::' . $code,
  164. $callback
  165. );
  166. }
  167. }
  168. }
  169. }