/server/application/modules/admin/models/Headlines.php
PHP | 155 lines | 91 code | 0 blank | 64 comment | 11 complexity | 60530729f2fe52cb5c7bdc8d034f9548 MD5 | raw file
- <?php
- /**
- * Headlines model for control panel
- *
- * Copyright 2009 Frederick Ding<br />
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- *
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * or the full licensing terms for this project at
- * http://code.google.com/p/display-ui/wiki/License
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @author Frederick
- * @license http://code.google.com/p/display-ui/wiki/License Apache License 2.0
- * @version $Id: Headlines.php 437 2011-06-21 06:23:04Z frederickjding $
- */
- /**
- * Provides logic and data for managing headlines
- */
- class Admin_Model_Headlines extends Default_Model_DatabaseAbstract
- {
- /**
- * Fetch an array of headlines from the database table
- * @param int|string $_admin
- * @return array
- */
- public function fetchHeadlines ($_admin)
- {
- if (! is_null($this->db)) {
- // Fetch headlines for clients to which the active user has access
- $select = $this->db->select()
- ->from(array(
- 'h' => 'dui_headlines'),
- array(
- 'id',
- 'title',
- 'active',
- 'expires' => new Zend_Db_Expr('CAST(expires AS DATE)'),
- 'alternating',
- 'type'))
- ->join(array(
- 'c' => 'dui_clients'), 'h.client = c.id', array(
- 'sys_name'))
- ->join(array(
- 'u' => 'dui_users'),
- 'c.admin = u.id OR c.users REGEXP CONCAT( \'(^|[0-9]*,)\', u.id, \'(,|$)\' ) ',
- array())
- ->order('id ASC');
- if (is_int($_admin)) {
- // treat it as the integer user ID
- $select->where('u.id = ?', $_admin, 'INTEGER');
- } else {
- // treat is as the username
- $select->where('u.username = ?', $_admin);
- }
- $result = $select->query()->fetchAll(Zend_Db::FETCH_ASSOC);
- // error_log($select->assemble());
- return $result;
- }
- return array();
- }
- /**
- * Determines whether the given administrator has permission to modify the given headline
- *
- * If $_boolean = FALSE, this function will return the ID instead of TRUE.
- * @param int|string $_admin
- * @param int $_id
- * @param bool $_boolean
- * @return bool|int
- */
- public function canModify ($_admin, $_id, $_boolean = TRUE)
- {
- if (! is_null($this->db)) {
- /*
- * A complex SQL query to find ONLY headlines to which the current user has access
- * It's a lot of work because there is no admin/user column in the headlines table, only
- * a client column. We have to use SQL to find admins who have access to clients
- * which are listed in the row.
- */
- $query = $this->db->select()
- ->from(array(
- 'h' => 'dui_headlines'), array(
- 'h.id'))
- ->joinInner(array(
- 'c' => 'dui_clients'), 'h.client = c.id', array())
- ->joinInner(array(
- 'u' => 'dui_users'),
- 'c.admin = u.id OR c.users REGEXP CONCAT( \'(^|[0-9]*,)\', u.id, \'(,|$)\' ) ',
- array())
- ->where('h.id = ?', $_id)
- ->limit(1);
- // Oops, debugging code.
- // error_log($query->assemble());
- if (is_int($_admin))
- $query->where('u.id = ?', $_admin);
- else
- $query->where('u.username = ?', $_admin);
- $retrievedId = $this->db->fetchOne($query);
- if ($_boolean) {
- return ($_id == $retrievedId);
- } else
- return $retrievedId;
- }
- return FALSE;
- }
- /**
- * Deletes a row from the headlines table
- * @param int $_id
- * @return bool
- */
- public function deleteHeadline ($_id)
- {
- $_id = (int) $_id;
- if (! is_null($this->db)) {
- $result = $this->db->delete('dui_headlines',
- $this->db->quoteInto('id = ?', $_id, 'INTEGER'));
- return (bool) $result;
- }
- return FALSE;
- }
- /**
- * Inserts a headline into the database from the Quickline form
- * @param string $_title
- * @param int $_clientId
- * @param string $_type
- * @return bool
- */
- public function insertHeadline ($_title, $_clientId, $_type, $_expires = '',
- $_alternating = null)
- {
- if (! is_null($this->db)) {
- $this->db->insert('dui_headlines',
- array(
- 'title' => trim($_title),
- 'active' => 1,
- // make it expire in 1 month by default
- 'expires' => (! empty($_expires) &&
- is_numeric($_expires)) ? $_expires : new Zend_Db_Expr(
- 'DATE_ADD(UTC_TIMESTAMP(), INTERVAL 1 MONTH)'),
- 'type' => $_type,
- 'alternating' => ($_alternating != 0) ? $_alternating : null,
- 'client' => $_clientId));
- if ($this->db->lastInsertId())
- return TRUE;
- }
- return FALSE;
- }
- }