diff options
Diffstat (limited to 'app/Model/Base.php')
-rw-r--r-- | app/Model/Base.php | 149 |
1 files changed, 87 insertions, 62 deletions
diff --git a/app/Model/Base.php b/app/Model/Base.php index 72d91c3c..51ae782d 100644 --- a/app/Model/Base.php +++ b/app/Model/Base.php @@ -2,44 +2,15 @@ namespace Model; -use Core\Event; -use Core\Tool; -use Core\Registry; -use PicoDb\Database; +use Pimple\Container; /** * Base model class * * @package model * @author Frederic Guillot - * - * @property \Model\Acl $acl - * @property \Model\Action $action - * @property \Model\Authentication $authentication - * @property \Model\Board $board - * @property \Model\Category $category - * @property \Model\Comment $comment - * @property \Model\CommentHistory $commentHistory - * @property \Model\Color $color - * @property \Model\Config $config - * @property \Model\DateParser $dateParser - * @property \Model\File $file - * @property \Model\LastLogin $lastLogin - * @property \Model\Notification $notification - * @property \Model\Project $project - * @property \Model\ProjectPermission $projectPermission - * @property \Model\SubTask $subTask - * @property \Model\SubtaskHistory $subtaskHistory - * @property \Model\Task $task - * @property \Model\TaskExport $taskExport - * @property \Model\TaskFinder $taskFinder - * @property \Model\TaskHistory $taskHistory - * @property \Model\TaskValidator $taskValidator - * @property \Model\TimeTracking $timeTracking - * @property \Model\User $user - * @property \Model\Webhook $webhook */ -abstract class Base +abstract class Base extends \Core\Base { /** * Database instance @@ -50,44 +21,35 @@ abstract class Base protected $db; /** - * Event dispatcher instance - * - * @access public - * @var \Core\Event - */ - public $event; - - /** - * Registry instance - * - * @access protected - * @var \Core\Registry - */ - protected $registry; - - /** * Constructor * * @access public - * @param \Core\Registry $registry Registry instance + * @param \Pimple\Container $container */ - public function __construct(Registry $registry) + public function __construct(Container $container) { - $this->registry = $registry; - $this->db = $this->registry->shared('db'); - $this->event = $this->registry->shared('event'); + $this->container = $container; + $this->db = $this->container['db']; } /** - * Load automatically models + * Save a record in the database * * @access public - * @param string $name Model name - * @return mixed + * @param string $table Table name + * @param array $values Form values + * @return boolean|integer */ - public function __get($name) + public function persist($table, array $values) { - return Tool::loadModel($this->registry, $name); + return $this->db->transaction(function($db) use ($table, $values) { + + if (! $db->table($table)->save($values)) { + return false; + } + + return (int) $db->getConnection()->getLastId(); + }); } /** @@ -95,7 +57,7 @@ abstract class Base * * @access public * @param array $values Input array - * @param array $keys List of keys to remove + * @param string[] $keys List of keys to remove */ public function removeFields(array &$values, array $keys) { @@ -110,8 +72,8 @@ abstract class Base * Force some fields to be at 0 if empty * * @access public - * @param array $values Input array - * @param array $keys List of keys + * @param array $values Input array + * @param string[] $keys List of keys */ public function resetFields(array &$values, array $keys) { @@ -126,8 +88,8 @@ abstract class Base * Force some fields to be integer * * @access public - * @param array $values Input array - * @param array $keys List of keys + * @param array $values Input array + * @param string[] $keys List of keys */ public function convertIntegerFields(array &$values, array $keys) { @@ -137,4 +99,67 @@ abstract class Base } } } + + /** + * Build SQL condition for a given time range + * + * @access protected + * @param string $start_time Start timestamp + * @param string $end_time End timestamp + * @param string $start_column Start column name + * @param string $end_column End column name + * @return string + */ + protected function getCalendarCondition($start_time, $end_time, $start_column, $end_column) + { + $start_column = $this->db->escapeIdentifier($start_column); + $end_column = $this->db->escapeIdentifier($end_column); + + $conditions = array( + "($start_column >= '$start_time' AND $start_column <= '$end_time')", + "($start_column <= '$start_time' AND $end_column >= '$start_time')", + "($start_column <= '$start_time' AND ($end_column = '0' OR $end_column IS NULL))", + ); + + return $start_column.' IS NOT NULL AND '.$start_column.' > 0 AND ('.implode(' OR ', $conditions).')'; + } + + /** + * Get common properties for task calendar events + * + * @access protected + * @param array $task + * @return array + */ + protected function getTaskCalendarProperties(array &$task) + { + return array( + 'timezoneParam' => $this->config->getCurrentTimezone(), + 'id' => $task['id'], + 'title' => t('#%d', $task['id']).' '.$task['title'], + 'backgroundColor' => $this->color->getBackgroundColor($task['color_id']), + 'borderColor' => $this->color->getBorderColor($task['color_id']), + 'textColor' => 'black', + 'url' => $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id'])), + ); + } + + /** + * Group a collection of records by a column + * + * @access public + * @param array $collection + * @param string $column + * @return array + */ + public function groupByColumn(array $collection, $column) + { + $result = array(); + + foreach ($collection as $item) { + $result[$item[$column]][] = $item; + } + + return $result; + } } |