PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/server/application/modules/admin/models/Headlines.php

http://display-ui.googlecode.com/
PHP | 155 lines | 91 code | 0 blank | 64 comment | 11 complexity | 60530729f2fe52cb5c7bdc8d034f9548 MD5 | raw file
  1. <?php
  2. /**
  3. * Headlines model for control panel
  4. *
  5. * Copyright 2009 Frederick Ding<br />
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. *
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * or the full licensing terms for this project at
  12. * http://code.google.com/p/display-ui/wiki/License
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * @author Frederick
  21. * @license http://code.google.com/p/display-ui/wiki/License Apache License 2.0
  22. * @version $Id: Headlines.php 437 2011-06-21 06:23:04Z frederickjding $
  23. */
  24. /**
  25. * Provides logic and data for managing headlines
  26. */
  27. class Admin_Model_Headlines extends Default_Model_DatabaseAbstract
  28. {
  29. /**
  30. * Fetch an array of headlines from the database table
  31. * @param int|string $_admin
  32. * @return array
  33. */
  34. public function fetchHeadlines ($_admin)
  35. {
  36. if (! is_null($this->db)) {
  37. // Fetch headlines for clients to which the active user has access
  38. $select = $this->db->select()
  39. ->from(array(
  40. 'h' => 'dui_headlines'),
  41. array(
  42. 'id',
  43. 'title',
  44. 'active',
  45. 'expires' => new Zend_Db_Expr('CAST(expires AS DATE)'),
  46. 'alternating',
  47. 'type'))
  48. ->join(array(
  49. 'c' => 'dui_clients'), 'h.client = c.id', array(
  50. 'sys_name'))
  51. ->join(array(
  52. 'u' => 'dui_users'),
  53. 'c.admin = u.id OR c.users REGEXP CONCAT( \'(^|[0-9]*,)\', u.id, \'(,|$)\' ) ',
  54. array())
  55. ->order('id ASC');
  56. if (is_int($_admin)) {
  57. // treat it as the integer user ID
  58. $select->where('u.id = ?', $_admin, 'INTEGER');
  59. } else {
  60. // treat is as the username
  61. $select->where('u.username = ?', $_admin);
  62. }
  63. $result = $select->query()->fetchAll(Zend_Db::FETCH_ASSOC);
  64. // error_log($select->assemble());
  65. return $result;
  66. }
  67. return array();
  68. }
  69. /**
  70. * Determines whether the given administrator has permission to modify the given headline
  71. *
  72. * If $_boolean = FALSE, this function will return the ID instead of TRUE.
  73. * @param int|string $_admin
  74. * @param int $_id
  75. * @param bool $_boolean
  76. * @return bool|int
  77. */
  78. public function canModify ($_admin, $_id, $_boolean = TRUE)
  79. {
  80. if (! is_null($this->db)) {
  81. /*
  82. * A complex SQL query to find ONLY headlines to which the current user has access
  83. * It's a lot of work because there is no admin/user column in the headlines table, only
  84. * a client column. We have to use SQL to find admins who have access to clients
  85. * which are listed in the row.
  86. */
  87. $query = $this->db->select()
  88. ->from(array(
  89. 'h' => 'dui_headlines'), array(
  90. 'h.id'))
  91. ->joinInner(array(
  92. 'c' => 'dui_clients'), 'h.client = c.id', array())
  93. ->joinInner(array(
  94. 'u' => 'dui_users'),
  95. 'c.admin = u.id OR c.users REGEXP CONCAT( \'(^|[0-9]*,)\', u.id, \'(,|$)\' ) ',
  96. array())
  97. ->where('h.id = ?', $_id)
  98. ->limit(1);
  99. // Oops, debugging code.
  100. // error_log($query->assemble());
  101. if (is_int($_admin))
  102. $query->where('u.id = ?', $_admin);
  103. else
  104. $query->where('u.username = ?', $_admin);
  105. $retrievedId = $this->db->fetchOne($query);
  106. if ($_boolean) {
  107. return ($_id == $retrievedId);
  108. } else
  109. return $retrievedId;
  110. }
  111. return FALSE;
  112. }
  113. /**
  114. * Deletes a row from the headlines table
  115. * @param int $_id
  116. * @return bool
  117. */
  118. public function deleteHeadline ($_id)
  119. {
  120. $_id = (int) $_id;
  121. if (! is_null($this->db)) {
  122. $result = $this->db->delete('dui_headlines',
  123. $this->db->quoteInto('id = ?', $_id, 'INTEGER'));
  124. return (bool) $result;
  125. }
  126. return FALSE;
  127. }
  128. /**
  129. * Inserts a headline into the database from the Quickline form
  130. * @param string $_title
  131. * @param int $_clientId
  132. * @param string $_type
  133. * @return bool
  134. */
  135. public function insertHeadline ($_title, $_clientId, $_type, $_expires = '',
  136. $_alternating = null)
  137. {
  138. if (! is_null($this->db)) {
  139. $this->db->insert('dui_headlines',
  140. array(
  141. 'title' => trim($_title),
  142. 'active' => 1,
  143. // make it expire in 1 month by default
  144. 'expires' => (! empty($_expires) &&
  145. is_numeric($_expires)) ? $_expires : new Zend_Db_Expr(
  146. 'DATE_ADD(UTC_TIMESTAMP(), INTERVAL 1 MONTH)'),
  147. 'type' => $_type,
  148. 'alternating' => ($_alternating != 0) ? $_alternating : null,
  149. 'client' => $_clientId));
  150. if ($this->db->lastInsertId())
  151. return TRUE;
  152. }
  153. return FALSE;
  154. }
  155. }