Skip to content

Commit

Permalink
Merge pull request #123 from senky/hide-ad-for-group
Browse files Browse the repository at this point in the history
Make group hiding ad-wise
  • Loading branch information
iMattPro committed Mar 29, 2018
2 parents 4ea723e + 3e2fc4d commit 0a266ab
Show file tree
Hide file tree
Showing 29 changed files with 358 additions and 314 deletions.
77 changes: 68 additions & 9 deletions ad/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,25 @@ class manager
/** @var string */
protected $ad_locations_table;

/** @var string */
protected $ad_group_table;

/**
* Constructor
*
* @param \phpbb\db\driver\driver_interface $db DB driver interface
* @param \phpbb\config\config $config Config object
* @param string $ads_table Ads table
* @param string $ad_locations_table Ad locations table
* @param string $ad_group_table Ad group table
*/
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $ads_table, $ad_locations_table)
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $ads_table, $ad_locations_table, $ad_group_table)
{
$this->db = $db;
$this->config = $config;
$this->ads_table = $ads_table;
$this->ad_locations_table = $ad_locations_table;
$this->ad_group_table = $ad_group_table;
}

/**
Expand All @@ -62,14 +67,16 @@ public function get_ad($ad_id)
* Get one ad per every location
*
* @param array $ad_locations List of ad locations to fetch ads for
* @param array $user_groups List of user groups
* @param bool $non_content_page Is current page non-content oriented (e.g.: login, UCP, MCP)? Default is false.
* @return array List of ad codes for each location
*/
public function get_ads($ad_locations, $non_content_page = false)
public function get_ads($ad_locations, $user_groups, $non_content_page = false)
{
$sql_where_views = $this->config['phpbb_ads_enable_views'] ? 'AND (a.ad_views_limit = 0 OR a.ad_views_limit > a.ad_views)' : '';
$sql_where_clicks = $this->config['phpbb_ads_enable_clicks'] ? 'AND (a.ad_clicks_limit = 0 OR a.ad_clicks_limit > a.ad_clicks)' : '';
$sql_where_non_content = $non_content_page ? 'AND a.ad_content_only = 0' : '';
$sql_where_user_groups = !empty($user_groups) ? 'AND NOT EXISTS (SELECT ag.group_id FROM ' . $this->ad_group_table . ' ag WHERE ag.ad_id = a.ad_id AND ' . $this->db->sql_in_set('ag.group_id', $user_groups) . ')' : '';

$sql = 'SELECT al.location_id, a.ad_id, a.ad_code
FROM ' . $this->ad_locations_table . ' al
Expand All @@ -81,6 +88,7 @@ public function get_ads($ad_locations, $non_content_page = false)
$sql_where_views
$sql_where_clicks
$sql_where_non_content
$sql_where_user_groups
AND " . $this->db->sql_in_set('al.location_id', $ad_locations) . '
ORDER BY al.location_id, (' . $this->sql_random() . ' * a.ad_priority) DESC';
$result = $this->db->sql_query($sql);
Expand Down Expand Up @@ -173,12 +181,18 @@ public function increment_ad_clicks($ad_id)
*/
public function insert_ad($data)
{
// extract ad groups here because it gets filtered in intersect_ad_data()
$ad_groups = $data['ad_groups'];
$data = $this->intersect_ad_data($data);

// add a row to ads table
$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
$this->db->sql_query($sql);
$ad_id = $this->db->sql_nextid();

return $this->db->sql_nextid();
$this->insert_ad_group_data($ad_id, $ad_groups);

return $ad_id;
}

/**
Expand All @@ -190,14 +204,20 @@ public function insert_ad($data)
*/
public function update_ad($ad_id, $data)
{
// extract ad groups here because it gets filtered in intersect_ad_data()
$ad_groups = isset($data['ad_groups']) ? $data['ad_groups'] : array();
$data = $this->intersect_ad_data($data);

$sql = 'UPDATE ' . $this->ads_table . '
SET ' . $this->db->sql_build_array('UPDATE', $data) . '
WHERE ad_id = ' . (int) $ad_id;
$this->db->sql_query($sql);
$result = $this->db->sql_affectedrows();

return $this->db->sql_affectedrows();
$this->remove_ad_group_data($ad_id);
$this->insert_ad_group_data($ad_id, $ad_groups);

return $result;
}

/**
Expand Down Expand Up @@ -315,14 +335,20 @@ public function load_memberships($user_id)
/**
* Load all board groups
*
* @param int $ad_id Advertisement ID
* @return array List of groups
*/
public function load_groups()
public function load_groups($ad_id)
{
$sql = 'SELECT group_id, group_name, group_type
FROM ' . GROUPS_TABLE . "
WHERE group_name <> 'BOTS'
ORDER BY group_name ASC";
$sql = 'SELECT g.group_id, g.group_name, (
SELECT COUNT(ad_id)
FROM ' . $this->ad_group_table . ' ag
WHERE ag.ad_id = ' . (int) $ad_id . '
AND ag.group_id = g.group_id
) as group_selected
FROM ' . GROUPS_TABLE . " g
WHERE g.group_name <> 'BOTS'
ORDER BY g.group_name ASC";
$result = $this->db->sql_query($sql);
$groups = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
Expand Down Expand Up @@ -377,4 +403,37 @@ protected function sql_random()
return 'RAND()';
}
}

/**
* Add rows to ad_group table.
*
* @param int $ad_id Advertisement ID
* @param array $ad_groups List of groups that should not see this ad
* @return void
*/
protected function insert_ad_group_data($ad_id, $ad_groups)
{
$sql_ary = array();
foreach ($ad_groups as $group)
{
$sql_ary[] = array(
'ad_id' => $ad_id,
'group_id' => $group,
);
}
$this->db->sql_multi_insert($this->ad_group_table, $sql_ary);
}

/**
* Remove all rows of specified ad in ad_group table
*
* @param int $ad_id Advertisement ID
* @return void
*/
protected function remove_ad_group_data($ad_id)
{
$sql = 'DELETE FROM ' . $this->ad_group_table . '
WHERE ad_id = ' . (int) $ad_id;
$this->db->sql_query($sql);
}
}
9 changes: 9 additions & 0 deletions adm/style/manage_ads.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ <h3>{{ lang('WARNING') }}</h3>
<dt><label for="ad_end_date">{{ lang('AD_END_DATE') ~ lang('COLON') }}</label><br /><span>{{ lang('AD_END_DATE_EXPLAIN') }}</span></dt>
<dd><input class="text" id="ad_end_date" name="ad_end_date" value="{{ AD_END_DATE ? AD_END_DATE|date(constant('\\phpbb\\ads\\ext::DATE_FORMAT')) }}" size="20" maxlength="20" /></dd>
</dl>
<dl>
<dt><label for="ad_groups">{{ lang('HIDE_GROUPS') ~ lang('COLON') }}</label><br /><span>{{ lang('HIDE_GROUPS_EXPLAIN') }}</span></dt>
<dd><select id="ad_groups" name="ad_groups[]" multiple size="8">
{% for group in loops.groups %}
<option value="{{ group.ID }}"{% if group.S_SELECTED %} selected{% endif %}>{{ group.NAME }}</option>
{% endfor %}
</select>
</dd>
</dl>
</fieldset>
<fieldset class="submit-buttons">
<input class="button1" type="submit" id="preview" name="preview" value="{{ lang('PREVIEW') }}" />&nbsp;
Expand Down
12 changes: 0 additions & 12 deletions adm/style/settings_ads.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ <h3>{{ lang('WARNING') }}</h3>
<label><input type="radio" class="radio" name="enable_clicks" value="0"{% if not ENABLE_CLICKS %} checked{% endif %} /> {{ lang('NO') }}</label></dd>
</dl>
</fieldset>
<fieldset>
<legend>{{ lang('GENERAL_SETTINGS') }}</legend>
<dl>
<dt><label for="hide_groups">{{ lang('HIDE_GROUPS') ~ lang('COLON') }}</label><br /><span>{{ lang('HIDE_GROUPS_EXPLAIN') }}</span></dt>
<dd><select id="hide_groups" name="hide_groups[]" multiple size="8">
{% for group in loops.groups %}
<option value="{{ group.ID }}"{% if group.S_SELECTED %} selected{% endif %}>{{ group.NAME }}</option>
{% endfor %}
</select>
</dd>
</dl>
</fieldset>
<fieldset class="submit-buttons">
<input class="button1" type="submit" id="submit" name="submit" value="{{ lang('SUBMIT') }}" />&nbsp;
<input class="button2" type="reset" id="reset" name="reset" value="{{ lang('RESET') }}" />
Expand Down
5 changes: 3 additions & 2 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
- '@config'
- '%phpbb.ads.tables.ads%'
- '%phpbb.ads.tables.ad_locations%'
- '%phpbb.ads.tables.ad_group%'

phpbb.ads.banner.banner:
class: phpbb\ads\banner\banner
Expand All @@ -28,7 +29,9 @@ services:
- '@language'
- '@template'
- '@log'
- '@phpbb.ads.ad.manager'
- '@phpbb.ads.location.manager'
- '@group_helper'
- '%core.root_path%'
- '%core.php_ext%'

Expand All @@ -47,7 +50,6 @@ services:
- '@template'
- '@template_context'
- '@user'
- '@config_text'
- '@config'
- '@phpbb.ads.ad.manager'
- '@phpbb.ads.location.manager'
Expand All @@ -67,7 +69,6 @@ services:
- '@phpbb.ads.ad.manager'
- '@config_text'
- '@config'
- '@group_helper'
- '@phpbb.ads.admin.input'
- '@phpbb.ads.helper'
- '@phpbb.ads.analyser.manager'
Expand Down
1 change: 1 addition & 0 deletions config/tables.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
phpbb.ads.tables.ads: '%core.table_prefix%ads'
phpbb.ads.tables.ad_group: '%core.table_prefix%ad_group'
phpbb.ads.tables.ad_locations: '%core.table_prefix%ad_locations'
20 changes: 2 additions & 18 deletions controller/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class admin_controller
/** @var \phpbb\config\config */
protected $config;

/** @var \phpbb\group\helper */
protected $group_helper;

/** @var \phpbb\ads\controller\admin_input */
protected $input;

Expand Down Expand Up @@ -68,23 +65,21 @@ class admin_controller
* @param \phpbb\ads\ad\manager $manager Advertisement manager object
* @param \phpbb\config\db_text $config_text Config text object
* @param \phpbb\config\config $config Config object
* @param \phpbb\group\helper $group_helper Group helper object
* @param \phpbb\ads\controller\admin_input $input Admin input object
* @param \phpbb\ads\controller\helper $helper Helper object
* @param \phpbb\ads\analyser\manager $analyser Ad code analyser object
* @param \phpbb\controller\helper $controller_helper Controller helper object
* @param string $root_path phpBB root path
* @param string $php_ext PHP extension
*/
public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\group\helper $group_helper, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\helper $helper, \phpbb\ads\analyser\manager $analyser, \phpbb\controller\helper $controller_helper, $root_path, $php_ext)
public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\helper $helper, \phpbb\ads\analyser\manager $analyser, \phpbb\controller\helper $controller_helper, $root_path, $php_ext)
{
$this->template = $template;
$this->language = $language;
$this->request = $request;
$this->manager = $manager;
$this->config_text = $config_text;
$this->config = $config;
$this->group_helper = $group_helper;
$this->input = $input;
$this->helper = $helper;
$this->analyser = $analyser;
Expand Down Expand Up @@ -138,25 +133,13 @@ public function mode_settings()
$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));

$this->success('ACP_AD_SETTINGS_SAVED');
}

$this->error('FORM_INVALID');
}

$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
$groups = $this->manager->load_groups();
foreach ($groups as $group)
{
$this->template->assign_block_vars('groups', array(
'ID' => $group['group_id'],
'NAME' => $this->group_helper->get_name($group['group_name']),
'S_SELECTED' => in_array($group['group_id'], $hide_groups),
));
}

$this->template->assign_vars(array(
'U_ACTION' => $this->u_action,
'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
Expand Down Expand Up @@ -202,6 +185,7 @@ protected function action_add()
else
{
$this->helper->assign_locations();
$this->helper->assign_groups();
}

// Set output vars for display in the template
Expand Down
1 change: 1 addition & 0 deletions controller/admin_input.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function get_form_data()
'ad_views_limit' => $this->request->variable('ad_views_limit', 0),
'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
'ad_owner' => $this->request->variable('ad_owner', '', true),
'ad_groups' => $this->request->variable('ad_groups', array(0)),
);

// Validate form key
Expand Down
32 changes: 31 additions & 1 deletion controller/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ class helper
/** @var \phpbb\log\log */
protected $log;

/** @var \phpbb\ads\ad\manager */
protected $manager;

/** @var \phpbb\ads\location\manager */
protected $location_manager;

/** @var \phpbb\group\helper */
protected $group_helper;

/** @var string root_path */
protected $root_path;

Expand All @@ -47,18 +53,22 @@ class helper
* @param \phpbb\language\language $language Language object
* @param \phpbb\template\template $template Template object
* @param \phpbb\log\log $log The phpBB log system
* @param \phpbb\ads\ad\manager $manager Ad manager object
* @param \phpbb\ads\location\manager $location_manager Template location manager object
* @param \phpbb\group\helper $group_helper Group helper object
* @param string $root_path phpBB root path
* @param string $php_ext PHP extension
*/
public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\location\manager $location_manager, $root_path, $php_ext)
public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\group\helper $group_helper, $root_path, $php_ext)
{
$this->user = $user;
$this->user_loader = $user_loader;
$this->language = $language;
$this->template = $template;
$this->log = $log;
$this->location_manager = $location_manager;
$this->manager = $manager;
$this->group_helper = $group_helper;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
}
Expand All @@ -72,6 +82,7 @@ public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader,
public function assign_data($data, $errors)
{
$this->assign_locations($data['ad_locations']);
$this->assign_groups(isset($data['ad_id']) ? $data['ad_id'] : 0);

$errors = array_map(array($this->language, 'lang'), $errors);
$this->template->assign_vars(array(
Expand Down Expand Up @@ -117,6 +128,25 @@ public function assign_locations($ad_locations = false)
}
}

/**
* Assign groups data to the template.
*
* @param int $ad_id Advertisement ID
* @return void
*/
public function assign_groups($ad_id = 0)
{
$groups = $this->manager->load_groups($ad_id);
foreach ($groups as $group)
{
$this->template->assign_block_vars('groups', array(
'ID' => $group['group_id'],
'NAME' => $this->group_helper->get_name($group['group_name']),
'S_SELECTED' => (bool) $group['group_selected'],
));
}
}

/**
* Log action
*
Expand Down

0 comments on commit 0a266ab

Please sign in to comment.