fork download
  1. <?php
  2.  
  3. class Announcement {
  4.  
  5. private $id;
  6. private $text;
  7. private $attachedFile;
  8. private $expiredDate;
  9.  
  10. public function __construct($text, DateTime $expiredDate = null, AttachedFile $attachedFile = null, $id = null) {
  11.  
  12. if (trim(strip_tags($text)) === '' && $attachedFile === null) {
  13.  
  14. throw new Exception('Announcement can\'t have no text and no file');
  15.  
  16. }
  17.  
  18. if ($expiredDate !== null && (int)$expiredDate->format('U') > pow(2, 31) - 1) {
  19.  
  20. throw new Exception('Too big expired date');
  21.  
  22. }
  23.  
  24. if ($id !== null && !ctype_digit((string)$id)) {
  25.  
  26. throw new Exception('Announcement id must be numeric');
  27.  
  28. }
  29.  
  30. $this->id = $id;
  31. $this->text = trim($text);
  32. $this->attachedFile = $attachedFile;
  33. $this->expiredDate = $expiredDate;
  34.  
  35.  
  36. }
  37.  
  38. public function putToDatabase() {
  39.  
  40. $mysqli = new mysqli(DATABASE_SERVER, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
  41.  
  42.  
  43. throw new Exception('Can\'t connect to database: ' . mysqli_connect_error());
  44.  
  45. }
  46.  
  47. if (!$mysqli->set_charset('utf8')) {
  48.  
  49. $mysqli->close();
  50. throw new Exception('Can\'t set utf8 to mysqli');
  51.  
  52. }
  53.  
  54. $sqlExpdate = $this->haveExpiredDate() ? 'FROM_UNIXTIME(' . $this->expiredDate->format('U') . ')' : 'NULL';
  55.  
  56. if ($this->id === null) {
  57.  
  58. if ($this->attachedFile !== null) {
  59.  
  60. $query = 'INSERT INTO announcements (ann_text, ann_file, ann_expdate) VALUES ("' . $mysqli->escape_string($this->text) . '", "' . $mysqli->escape_string($this->attachedFile->getFilepath()) . '", ' . $sqlExpdate . ')';
  61.  
  62. } else {
  63.  
  64. $query = 'INSERT INTO announcements (ann_text, ann_expdate) VALUES ("' . $mysqli->escape_string($this->text) . '", ' . $sqlExpdate . ')';
  65.  
  66. }
  67.  
  68. if (!$mysqli->query($query)) {
  69.  
  70. $mysqli->close();
  71. throw new Exception('Unsuccessful INSERT query');
  72.  
  73. }
  74.  
  75. if ($mysqli->affected_rows === 1) {
  76.  
  77. $this->id = $mysqli->insert_id;
  78.  
  79. $mysqli->close();
  80.  
  81. return true;
  82.  
  83. } else {
  84.  
  85. $mysqli->close();
  86. throw new Exception('No affected rows (or more than 1) after succesful INSERT query');
  87.  
  88. }
  89.  
  90. }
  91.  
  92. else {
  93.  
  94. if ($this->attachedFile !== null) {
  95.  
  96. $query = 'UPDATE announcements SET ann_text = "' . $mysqli->escape_string($this->text) . '", ann_file = "' . $mysqli->escape_string($this->attachedFile->getFilepath()) . '", ann_expdate = ' . $sqlExpdate . ' WHERE ann_id = ' . $this->id;
  97.  
  98. } else {
  99.  
  100. $query = 'UPDATE announcements SET ann_text = "' . $mysqli->escape_string($this->text) . '", ann_expdate = ' . $sqlExpdate . ' WHERE ann_id = ' . $this->id;
  101.  
  102. }
  103.  
  104. if (!$mysqli->query($query)) {
  105.  
  106. $mysqli->close();
  107. throw new Exception('Unsuccessful UPDATE query');
  108.  
  109. }
  110.  
  111. // закомментировано потому, что update без изменений не дает affected_rows
  112. // if ($mysqli->affected_rows === 1)
  113.  
  114. $mysqli->close();
  115.  
  116. return true;
  117.  
  118. }
  119.  
  120. }
  121.  
  122. const ACTION_REMOVE = 1;
  123. const ACTION_RESTORE = 2;
  124.  
  125. public function deleteMe() {
  126.  
  127. self::toggleAnnouncement($this->id, self::ACTION_REMOVE);
  128.  
  129. }
  130.  
  131. public static function deleteAnnouncement($id) {
  132.  
  133. self::toggleAnnouncement($id, self::ACTION_REMOVE);
  134.  
  135. }
  136.  
  137. public static function restoreAnnouncement($id) {
  138.  
  139. self::toggleAnnouncement($id, self::ACTION_RESTORE);
  140.  
  141. }
  142.  
  143. public static function toggleAnnouncement($id, $concreteAction = null) {
  144.  
  145. if (!ctype_digit($id)) {
  146.  
  147. throw new Exception('Announcement id must be numeric');
  148.  
  149. }
  150.  
  151. $mysqli = new mysqli(DATABASE_SERVER, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
  152.  
  153.  
  154. throw new Exception('Can\'t connect to database: ' . mysqli_connect_error());
  155.  
  156. }
  157.  
  158. if (!$mysqli->set_charset('utf8')) {
  159.  
  160. $mysqli->close();
  161. throw new Exception('Can\'t set utf8 to mysqli');
  162.  
  163. }
  164.  
  165. if (!($result = $mysqli->query('SELECT ann_hidden FROM announcements WHERE ann_id = ' . $id)) || $result->num_rows < 1) {
  166.  
  167. $mysqli->close();
  168. throw new Exception('Can\'t find announcement with id ' . $id);
  169.  
  170. }
  171.  
  172. $row = $result->fetch_assoc();
  173. $result->free();
  174.  
  175. if ($row['ann_hidden'] === '1' && $concreteAction === self::ACTION_REMOVE) {
  176.  
  177. // Уже удалено
  178. return false;
  179.  
  180. } elseif ($row['ann_hidden'] === '0' && $concreteAction === self::ACTION_RESTORE) {
  181.  
  182. // Уже восстановлено
  183. return false;
  184.  
  185. }
  186.  
  187. $toggleHidden = $row['ann_hidden'] === '0' ? '1' : '0';
  188.  
  189. if (!$mysqli->query('UPDATE announcements SET ann_hidden = ' . $toggleHidden . ' WHERE ann_id = ' . $id) || $mysqli->affected_rows < 1) {
  190.  
  191. $mysqli->close();
  192. throw new Exception('Unsuccessful UPDATE query');
  193.  
  194. }
  195.  
  196. // Здесь будет помещение в корзину
  197.  
  198. $mysqli->close();
  199.  
  200. return true;
  201.  
  202. }
  203.  
  204. public function getHumanReadableExpiredDate() {
  205.  
  206. return $this->expiredDate->format('j') . ' ' . $GLOBALS['MONTHS_ARRAY'][(int)$this->expiredDate->format('n')] . (($this->expiredDate->format('Y') !== date('Y')) ? ' ' . $this->expiredDate->format('Y') : '');
  207.  
  208. }
  209.  
  210. public function getId() {
  211.  
  212. return $this->id;
  213.  
  214. }
  215.  
  216. public function getText() {
  217.  
  218. return $this->text;
  219.  
  220. }
  221.  
  222. public function getFile() {
  223.  
  224. return $this->attachedFile;
  225.  
  226. }
  227.  
  228. public function haveText() {
  229.  
  230. return !empty($this->text);
  231.  
  232. }
  233.  
  234. public function haveAttachedFile() {
  235.  
  236. return !is_null($this->attachedFile);
  237.  
  238. }
  239.  
  240. public function haveExpiredDate() {
  241.  
  242. return !is_null($this->expiredDate);
  243.  
  244. }
  245.  
  246. public function getExpiredDate() {
  247.  
  248. return $this->expiredDate;
  249.  
  250. }
  251.  
  252. public static function separateActualAndNonactual(array $anns) {
  253.  
  254. $actual = array();
  255. $nonactual = array();
  256.  
  257. foreach ($anns as $ann) if ($ann instanceof Announcement) {
  258.  
  259. if (!$ann->haveExpiredDate() || (int)$ann->getExpiredDate()->format('U') > time()) {
  260.  
  261. $actual[] = $ann;
  262.  
  263. } else {
  264.  
  265. $nonactual[] = $ann;
  266.  
  267. }
  268.  
  269. }
  270.  
  271. return array('actual' => $actual, 'nonactual' => $nonactual);
  272.  
  273. }
  274.  
  275. public static function getAnnouncementsFromDatabase($onlyActual = true) {
  276.  
  277. $anns = array();
  278.  
  279. $mysqli = new mysqli(DATABASE_SERVER, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
  280.  
  281.  
  282. throw new Exception('Can\'t connect to database: ' . mysqli_connect_error());
  283.  
  284. }
  285.  
  286. if (!$mysqli->set_charset('utf8')) {
  287.  
  288. $mysqli->close();
  289. throw new Exception('Can\'t set utf8 to mysqli');
  290.  
  291. }
  292.  
  293. if (!($result = $mysqli->query('SELECT * FROM announcements WHERE ann_hidden != 1' . ($onlyActual ? ' AND (ann_expdate > NOW() OR ann_expdate IS NULL)' : '') . ' ORDER BY ann_id DESC'))) {
  294.  
  295. $mysqli->close();
  296. throw new Exception('Unsuccessful SELECT query');
  297.  
  298. }
  299.  
  300. while ($row = $result->fetch_assoc()) {
  301.  
  302. try {
  303.  
  304. $attachedFile = ($row['ann_file'] !== null) ? new AttachedFile($row['ann_file']) : null;
  305. $expdate = ($row['ann_expdate'] !== null) ? new DateTime($row['ann_expdate']) : null;
  306.  
  307. $anns[] = new Announcement($row['ann_text'], $expdate, $attachedFile, $row['ann_id']);
  308.  
  309. } catch (Exception $e) {
  310.  
  311. $result->free();
  312. $mysqli->close();
  313.  
  314. throw $e;
  315.  
  316. }
  317.  
  318. }
  319.  
  320. $result->free();
  321. $mysqli->close();
  322.  
  323. return $anns;
  324.  
  325. }
  326.  
  327. }
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Standard output is empty