Skip to content

Commit

Permalink
Extract main class into sub classes
Browse files Browse the repository at this point in the history
  • Loading branch information
iMattPro committed Sep 15, 2015
1 parent b394fbc commit 7810b84
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 213 deletions.
14 changes: 10 additions & 4 deletions config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
services:
vse.topicpreview.manager:
class: vse\topicpreview\core\topic_preview
vse.topicpreview.data:
class: vse\topicpreview\core\data
arguments:
- @config
- @user

vse.topicpreview.display:
class: vse\topicpreview\core\display
arguments:
- @config
- @dbal.conn
- @dispatcher
- @template
- @user
Expand All @@ -18,7 +23,8 @@ services:
vse.topicpreview.listener:
class: vse\topicpreview\event\listener
arguments:
- @vse.topicpreview.manager
- @vse.topicpreview.data
- @vse.topicpreview.display
tags:
- { name: event.listener }

Expand Down
66 changes: 66 additions & 0 deletions core/base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
*
* Topic Preview
*
* @copyright (c) 2013 Matt Friedman
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace vse\topicpreview\core;

class base
{
/** @var \phpbb\config\config */
protected $config;

/** @var \phpbb\user */
protected $user;

/**
* Constructor
*
* @param \phpbb\config\config $config
* @param \phpbb\user $user
* @access public
*/
public function __construct(\phpbb\config\config $config, \phpbb\user $user)
{
$this->config = $config;
$this->user = $user;
}

/**
* Show topic previews, given current board and user configurations
*
* @return bool
* @access public
*/
public function is_enabled()
{
return (bool) !empty($this->config['topic_preview_limit']) && !empty($this->user->data['user_topic_preview']);
}

/**
* Show avatars, given current board and user configurations
*
* @return bool
* @access public
*/
public function avatars_enabled()
{
return (bool) $this->config['topic_preview_avatars'] && $this->config['allow_avatar'] && $this->user->optionget('viewavatars');
}

/**
* Show last post text, given current board configuration
*
* @return bool
* @access public
*/
public function last_post_enabled()
{
return (bool) $this->config['topic_preview_last_post'];
}
}
134 changes: 134 additions & 0 deletions core/data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
*
* Topic Preview
*
* @copyright (c) 2013 Matt Friedman
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace vse\topicpreview\core;

class data extends base
{
/**
* Update an SQL SELECT statement to get data needed for topic previews
*
* @return string SQL SELECT appendage
* @access public
*/
public function tp_sql_select()
{
$sql = ', fp.post_text AS first_post_text';

if ($this->last_post_enabled())
{
$sql .= ', lp.post_text AS last_post_text';
}

if ($this->avatars_enabled())
{
$sql .= ', fpu.user_avatar AS fp_avatar,
fpu.user_avatar_type AS fp_avatar_type,
fpu.user_avatar_width AS fp_avatar_width,
fpu.user_avatar_height AS fp_avatar_height';

if ($this->last_post_enabled())
{
$sql .= ', lpu.user_avatar AS lp_avatar,
lpu.user_avatar_type AS lp_avatar_type,
lpu.user_avatar_width AS lp_avatar_width,
lpu.user_avatar_height AS lp_avatar_height';
}
}

return $sql;
}

/**
* Update an SQL JOIN statement to get data needed for topic previews
*
* @return array SQL JOIN params
* @access public
*/
public function tp_sql_join()
{
$sql_array = array();

$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(POSTS_TABLE => 'fp'),
'ON' => 'fp.post_id = t.topic_first_post_id'
);

if ($this->avatars_enabled())
{
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(USERS_TABLE => 'fpu'),
'ON' => 'fpu.user_id = t.topic_poster'
);
}

if ($this->last_post_enabled())
{
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(POSTS_TABLE => 'lp'),
'ON' => 'lp.post_id = t.topic_last_post_id'
);

if ($this->avatars_enabled())
{
$sql_array['LEFT_JOIN'][] = array(
'FROM' => array(USERS_TABLE => 'lpu'),
'ON' => 'lpu.user_id = t.topic_last_poster_id'
);
}
}

return $sql_array;
}

/**
* Modify SQL string|array to get post text
*
* @param string|array $sql_stmt SQL string or array to be modified
* @param string $type Type of SQL statement SELECT|JOIN
* @return string|array SQL statement string or array
* @access public
*/
public function modify_sql($sql_stmt, $type = 'SELECT')
{
if (!$this->is_enabled())
{
return $sql_stmt;
}

if (is_array($sql_stmt))
{
$array = $this->tp_sql_join();
foreach ($array['LEFT_JOIN'] as $join)
{
$sql_stmt['LEFT_JOIN'][] = $join;
}

$sql_stmt['SELECT'] .= $this->tp_sql_select();
}
else
{
if ($type == 'SELECT')
{
$sql_stmt .= $this->tp_sql_select();
}
else
{
$array = $this->tp_sql_join();
foreach ($array['LEFT_JOIN'] as $join)
{
$sql_stmt .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')';
}
}
}

return $sql_stmt;
}
}
Loading

0 comments on commit 7810b84

Please sign in to comment.