PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/bitrix/modules/vote/lib/attach.php

https://bitbucket.org/alex_poluektov/itech_test
PHP | 330 lines | 265 code | 17 blank | 48 comment | 42 complexity | 239e1bd3232e448c9662b9717e9ba376 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage vote
  6. * @copyright 2001-2016 Bitrix
  7. */
  8. namespace Bitrix\Vote;
  9. use \Bitrix\Main\Entity;
  10. use \Bitrix\Main\Localization\Loc;
  11. use \Bitrix\Main\ArgumentException;
  12. use \Bitrix\Vote\Base\BaseObject;
  13. use \Bitrix\Vote\DBResult;
  14. use \Bitrix\Main\SystemException;
  15. Loc::loadMessages(__FILE__);
  16. /**
  17. * Class AttachTable
  18. * Fields:
  19. * <ul>
  20. * <li> ID int mandatory
  21. * <li> OBJECT_ID int,
  22. * <li> MODULE_ID string(32),
  23. * <li> ENTITY_TYPE string(100),
  24. * <li> ENTITY_ID int,
  25. * <li> CREATE_TIME datetime,
  26. * <li> CREATED_BY int
  27. * </ul>
  28. *
  29. */
  30. class AttachTable extends Entity\DataManager
  31. {
  32. /**
  33. * Returns DB table name for entity
  34. * @return string
  35. */
  36. public static function getTableName()
  37. {
  38. return 'b_vote_attached_object';
  39. }
  40. /**
  41. * Returns entity map definition.
  42. * @return array
  43. */
  44. public static function getMap()
  45. {
  46. return array(
  47. 'ID' => array(
  48. 'data_type' => 'integer',
  49. 'primary' => true,
  50. 'autocomplete' => true,
  51. 'title' => Loc::getMessage('V_TABLE_FIELD_ID'),
  52. ),
  53. 'OBJECT_ID' => array(
  54. 'data_type' => 'integer',
  55. 'title' => Loc::getMessage('V_TABLE_FIELD_OBJECT_ID'),
  56. ),
  57. 'MODULE_ID' => array(
  58. 'data_type' => 'string',
  59. 'size' => 32,
  60. 'title' => Loc::getMessage('V_TABLE_FIELD_MODULE_ID')
  61. ),
  62. 'ENTITY_TYPE' => array(
  63. 'data_type' => 'string',
  64. 'size' => 100,
  65. 'title' => Loc::getMessage('V_TABLE_FIELD_ENTITY_TYPE')
  66. ),
  67. 'ENTITY_ID' => array(
  68. 'data_type' => 'integer',
  69. 'title' => Loc::getMessage('V_TABLE_FIELD_OBJECT_ID'),
  70. ),
  71. 'CREATE_TIME' => array(
  72. 'data_type' => 'datetime',
  73. 'title' => Loc::getMessage('V_TABLE_FIELD_TIMESTAMP_X'),
  74. ),
  75. 'CREATED_BY' => array(
  76. 'data_type' => 'integer',
  77. 'title' => Loc::getMessage('V_TABLE_FIELD_AUTHOR_ID'),
  78. ),
  79. 'VOTE' => array(
  80. 'data_type' => '\Bitrix\Vote\VoteTable',
  81. 'reference' => array(
  82. '=this.OBJECT_ID' => 'ref.ID',
  83. ),
  84. 'join_type' => 'INNER',
  85. ),
  86. );
  87. }
  88. /**
  89. * @param array $parameters Array in terms ORM.
  90. * @return \Bitrix\Vote\DBResult
  91. */
  92. public static function getList(array $parameters = array())
  93. {
  94. return new DBResult(parent::getList($parameters));
  95. }
  96. /**
  97. * Removes group of attaches
  98. * @param array $filter Array in terms ORM.
  99. * @return bool
  100. * @throws \Bitrix\Main\ArgumentNullException
  101. */
  102. public static function deleteByFilter(array $filter)
  103. {
  104. if (!$filter)
  105. {
  106. throw new \Bitrix\Main\ArgumentNullException('filter');
  107. }
  108. $result = static::getList(array(
  109. 'select' => array('ID'),
  110. 'filter' => $filter,
  111. ));
  112. while($row = $result->fetch())
  113. {
  114. if(!empty($row['ID']))
  115. {
  116. $resultDelete = static::delete($row['ID']);
  117. if(!$resultDelete->isSuccess())
  118. {
  119. return false;
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. }
  126. class Attach extends BaseObject
  127. {
  128. public static $storage = array();
  129. /**
  130. * Return array where first key is attach array, second - vote array
  131. * @param integer $id Attach ID.
  132. * @return array|null
  133. */
  134. public static function getData($id)
  135. {
  136. $filter = array();
  137. if (is_array($id))
  138. {
  139. $filter = array_change_key_case($id, CASE_UPPER);
  140. $id = md5(serialize($filter));
  141. }
  142. else if (($id = intval($id)) && $id > 0)
  143. $filter["ID"] = $id;
  144. else
  145. return null;
  146. if (!array_key_exists($id, self::$storage))
  147. {
  148. self::$storage[$id] = null;
  149. $dbRes = AttachTable::getList(array(
  150. 'select' => array(
  151. 'O_' => "*",
  152. 'V_' => 'VOTE.*',
  153. 'V_LAMP' => 'VOTE.LAMP',
  154. 'Q_' => 'VOTE.QUESTION.*',
  155. 'A_' => 'VOTE.QUESTION.ANSWER',
  156. ),
  157. 'order' => array(
  158. 'VOTE.QUESTION.C_SORT' => 'ASC',
  159. 'VOTE.QUESTION.ID' => 'ASC',
  160. 'VOTE.QUESTION.ANSWER.C_SORT' => 'ASC',
  161. 'VOTE.QUESTION.ANSWER.ID' => 'ASC',
  162. ),
  163. 'filter' => $filter
  164. ));
  165. $attaches = array();
  166. $votes = array();
  167. while (($res = $dbRes->fetch()) && $res)
  168. {
  169. $attach = array();
  170. $vote = array();
  171. $question = array();
  172. $answer = array();
  173. foreach ($res as $key => $val)
  174. {
  175. if (strpos($key, "O_") === 0)
  176. $attach[substr($key, 2)] = $val;
  177. else if (strpos($key, "V_") === 0)
  178. $vote[substr($key, 2)] = $val;
  179. else if (strpos($key, "Q_") === 0)
  180. $question[substr($key, 2)] = $val;
  181. else if (strpos($key, "A_") === 0)
  182. $answer[substr($key, 2)] = $val;
  183. }
  184. if (!array_key_exists($attach["ID"], $attaches))
  185. $attaches[$attach["ID"]] = $attach;
  186. if (!array_key_exists($vote["ID"], $votes))
  187. $votes[$vote["ID"]] = array_merge($vote, array("QUESTIONS" => array()));
  188. $vote = $votes[$vote["ID"]];
  189. $questions = &$vote["QUESTIONS"];
  190. $qid = "".$question["ID"];
  191. if (!array_key_exists($qid, $questions))
  192. $questions[$qid] = array_merge($question, array("ANSWERS" => array()));
  193. if (!array_key_exists($qid, Question::$storage))
  194. Question::$storage[$qid] = $question;
  195. $answers = &$questions[$qid]["ANSWERS"];
  196. if (!empty($answer))
  197. {
  198. switch ($answer["FIELD_TYPE"])
  199. {
  200. case 1://checkbox
  201. $answer["FIELD_NAME"] = 'vote_checkbox_' . $qid;
  202. break;
  203. case 2://select
  204. $answer["FIELD_NAME"] = 'vote_dropdown_' . $qid;
  205. break;
  206. case 3://multiselect
  207. $answer["FIELD_NAME"] = 'vote_multiselect_' . $qid;
  208. break;
  209. case 4://text field
  210. $answer["FIELD_NAME"] = 'vote_field_' . $answer["ID"];
  211. break;
  212. case 5 :
  213. $answer["FIELD_NAME"] = 'vote_field_' . $answer["ID"];
  214. break;
  215. default: //radio
  216. $answer["FIELD_NAME"] = 'vote_radio_' . $qid;
  217. break;
  218. }
  219. $answer["~PERCENT"] = ($question["COUNTER"] > 0 ? $answer["COUNTER"] * 100 / $question["COUNTER"] : 0);
  220. $answer["PERCENT"] = round($answer["~PERCENT"], 2);
  221. if (!array_key_exists($answer["ID"], $answers))
  222. $answers[$answer["ID"]] = $answer;
  223. if (!array_key_exists($answer["ID"], Answer::$storage))
  224. Answer::$storage[$answer["ID"]] = $answer;
  225. }
  226. $votes[$vote["ID"]] = $vote;
  227. }
  228. foreach ($votes as $vote)
  229. Vote::$storage[$vote["ID"]] = $vote;
  230. foreach ($attaches as $attach)
  231. {
  232. self::$storage[$attach["ID"]] = array($attach, $votes[$attach["OBJECT_ID"]]);
  233. if (is_string($id))
  234. {
  235. self::$storage[$id] = (is_array(self::$storage[$id]) ? self::$storage[$id] : array());
  236. self::$storage[$id][$attach["ID"]] = array($attach, $votes[$attach["OBJECT_ID"]]);
  237. }
  238. }
  239. }
  240. return self::$storage[$id];
  241. }
  242. /**
  243. * Returns array of attaches linked to special entity
  244. * @param array $id Array("ENTITY_TYPE" => "blog", "ENTITY_ID" => 89);.
  245. * @return mixed
  246. */
  247. public static function getDataByEntity(array $id)
  248. {
  249. $id1 = md5(serialize($id));
  250. if (!array_key_exists($id1, self::$storage))
  251. {
  252. self::$storage[$id1] = array();
  253. $dbRes = AttachTable::getList(array(
  254. 'select' => array(
  255. 'O_' => "*",
  256. 'V_' => 'VOTE.*',
  257. 'V_LAMP' => 'VOTE.LAMP',
  258. 'Q_' => 'VOTE.QUESTION.*',
  259. 'A_' => 'VOTE.QUESTION.ANSWER',
  260. ),
  261. 'order' => array(
  262. 'VOTE.QUESTION.C_SORT' => 'ASC',
  263. 'VOTE.QUESTION.ID' => 'ASC',
  264. 'VOTE.QUESTION.ANSWER.C_SORT' => 'ASC',
  265. 'VOTE.QUESTION.ANSWER.ID' => 'ASC',
  266. ),
  267. 'filter' => array(
  268. 'ENTITY_TYPE' => $id['ENTITY_TYPE'],
  269. 'ENTITY_ID' => $id['ENTITY_ID']
  270. )
  271. ));
  272. if (($res = $dbRes->fetch()) && $res)
  273. {
  274. $attach = array();
  275. $vote = array();
  276. foreach ($res as $key => $val)
  277. if (strpos($key, "O_") === 0)
  278. $attach[substr($key, 2)] = $val;
  279. else if (strpos($key, "V_") === 0)
  280. $vote[substr($key, 2)] = $val;
  281. $vote["QUESTIONS"] = array();
  282. $questions = &$vote["QUESTIONS"];
  283. do
  284. {
  285. $question = array(); $answer = array();
  286. foreach ($res as $key => $val)
  287. {
  288. if (strpos($key, "Q_") === 0)
  289. $question[substr($key, 2)] = $val;
  290. else if (strpos($key, "A_") === 0)
  291. $answer[substr($key, 2)] = $val;
  292. }
  293. $qid = "".$question["ID"];
  294. if (!array_key_exists($qid, $questions))
  295. $questions[$qid] = array_merge($question, array("ANSWERS" => array()));
  296. if (!array_key_exists($qid, Question::$storage))
  297. Question::$storage[$qid] = $question;
  298. $answers = &$questions[$qid]["ANSWERS"];
  299. if (!empty($answer))
  300. {
  301. if (!array_key_exists($answer["ID"], $answers))
  302. $answers[$answer["ID"]] = $answer;
  303. if (!array_key_exists($answer["ID"], Answer::$storage))
  304. Answer::$storage[$answer["ID"]] = $answer;
  305. }
  306. } while (($res = $dbRes->fetch()) && $res);
  307. Vote::$storage[$vote["ID"]] = $vote;
  308. self::$storage[$id1] = array($attach, $vote);
  309. }
  310. }
  311. return self::$storage[$id1];
  312. }
  313. }