PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/root/includes/acp/acp_ads.php

https://github.com/EXreaction/Advertisement-Management
PHP | 676 lines | 639 code | 13 blank | 24 comment | 20 complexity | fdf22f3d2468912a864606e68c50907d MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3 Advertisement Management
  5. * @version $Id$
  6. * @copyright (c) 2008 EXreaction
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * @package acp
  19. */
  20. class acp_ads
  21. {
  22. var $u_action;
  23. var $new_config = array();
  24. function main($id, $mode)
  25. {
  26. global $db, $user, $auth, $template;
  27. global $cache, $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
  28. require($phpbb_root_path . 'ads/constants.' . $phpEx);
  29. require($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  30. $user->add_lang('mods/ads');
  31. $this->tpl_name = 'acp_ads';
  32. $this->page_title = 'ACP_ADVERTISEMENT_MANAGEMENT';
  33. $action = request_var('action', '');
  34. $submit = (isset($_POST['submit'])) ? true : false;
  35. $position_id = request_var('p', 0);
  36. $ad_id = request_var('a', 0);
  37. $error = array();
  38. $ad_data = $position_data = false;
  39. // Check Form Key
  40. add_form_key('acp_ads');
  41. if ($submit && !check_form_key('acp_ads'))
  42. {
  43. trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
  44. }
  45. // Get the ad/position info if either id is sent.
  46. if ($ad_id)
  47. {
  48. $result = $db->sql_query('SELECT * FROM ' . ADS_TABLE . ' WHERE ad_id = ' . $ad_id);
  49. $ad_data = $db->sql_fetchrow($result);
  50. $db->sql_freeresult($result);
  51. if ($ad_data)
  52. {
  53. $ad_data['forums'] = $ad_data['groups'] = $ad_data['positions'] = array();
  54. $result = $db->sql_query('SELECT forum_id FROM ' . ADS_FORUMS_TABLE . ' WHERE ad_id = ' . $ad_id);
  55. while ($row = $db->sql_fetchrow($result))
  56. {
  57. $ad_data['forums'][] = $row['forum_id'];
  58. }
  59. $db->sql_freeresult($result);
  60. $result = $db->sql_query('SELECT group_id FROM ' . ADS_GROUPS_TABLE . ' WHERE ad_id = ' . $ad_id);
  61. while ($row = $db->sql_fetchrow($result))
  62. {
  63. $ad_data['groups'][] = $row['group_id'];
  64. }
  65. $db->sql_freeresult($result);
  66. $result = $db->sql_query('SELECT position_id FROM ' . ADS_IN_POSITIONS_TABLE . ' WHERE ad_id = ' . $ad_id);
  67. while ($row = $db->sql_fetchrow($result))
  68. {
  69. $ad_data['positions'][] = $row['position_id'];
  70. }
  71. $db->sql_freeresult($result);
  72. }
  73. }
  74. if ($position_id)
  75. {
  76. $result = $db->sql_query('SELECT * FROM ' . ADS_POSITIONS_TABLE . ' WHERE position_id = ' . $position_id);
  77. $position_data = $db->sql_fetchrow($result);
  78. $db->sql_freeresult($result);
  79. }
  80. // Config Variables
  81. $config_vars = array(
  82. 'legend1' => 'ACP_ADVERTISEMENT_MANAGEMENT_SETTINGS',
  83. 'ads_enable' => array('lang' => 'ADS_ENABLE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
  84. 'ads_rules_groups' => array('lang' => 'ADS_RULES_GROUPS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
  85. 'ads_rules_forums' => array('lang' => 'ADS_RULES_FORUMS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
  86. 'ads_count_clicks' => array('lang' => 'ADS_COUNT_CLICKS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
  87. 'ads_count_views' => array('lang' => 'ADS_COUNT_VIEWS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
  88. 'ads_accurate_views' => array('lang' => 'ADS_ACCURATE_VIEWS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
  89. 'ads_group' => array('lang' => 'ADS_GROUP', 'validate' => 'int:0', 'type' => 'select', 'method' => 'group_select', 'explain' => true),
  90. );
  91. $this->new_config = $config;
  92. $this->new_config = (isset($_REQUEST['config'])) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
  93. // Other settings
  94. $ad_name = utf8_normalize_nfc(request_var('ad_name', '', true));
  95. $ad_code = utf8_normalize_nfc(request_var('ad_code', '', true));
  96. $ad_note = utf8_normalize_nfc(request_var('ad_note', '', true));
  97. $ad_time_end = (utf8_normalize_nfc(request_var('ad_time_end', '', true))) ? strtotime(utf8_normalize_nfc(request_var('ad_time_end', '', true))) : 0;
  98. $ad_groups = request_var('ad_groups', array(0), true);
  99. $ad_forums = request_var('ad_forums', array(0), true);
  100. $ad_positions = request_var('ad_positions', array(0), true);
  101. $position_name = request_var('position_name', '');
  102. $ad_owner = utf8_normalize_nfc(request_var('ad_owner', '', true));
  103. $ad_owner_id = 0;
  104. switch ($action)
  105. {
  106. /**************************************************************************************
  107. *
  108. * Add/Edit Advertisement/Position
  109. *
  110. **************************************************************************************/
  111. case 'add' :
  112. case 'edit' :
  113. case 'copy' :
  114. if ($position_id || ($position_name && $submit))
  115. {
  116. if (($action == 'edit' || $action == 'copy') && !$position_data)
  117. {
  118. trigger_error($user->lang['POSITION_NOT_EXIST'] . adm_back_link($this->u_action));
  119. }
  120. if ($action == 'add')
  121. {
  122. // Make sure the given position name isn't already in the database.
  123. $sql = 'SELECT position_id FROM ' . ADS_POSITIONS_TABLE . ' WHERE lang_key = \'' . $db->sql_escape($position_name) . "'";
  124. $result = $db->sql_query($sql);
  125. if ($db->sql_fetchrow($result))
  126. {
  127. trigger_error($user->lang['POSTITION_ALREADY_EXIST'] . adm_back_link($this->u_action));
  128. }
  129. $db->sql_query('INSERT INTO ' . ADS_POSITIONS_TABLE . ' ' . $db->sql_build_array('INSERT', array('lang_key' => $position_name)));
  130. }
  131. else
  132. {
  133. if ($submit && $position_name != $position_data['lang_key'])
  134. {
  135. // Make sure the given position name isn't already in the database.
  136. $sql = 'SELECT position_id FROM ' . ADS_POSITIONS_TABLE . ' WHERE lang_key = \'' . $db->sql_escape($position_name) . "'";
  137. $result = $db->sql_query($sql);
  138. if ($db->sql_fetchrow($result))
  139. {
  140. trigger_error($user->lang['POSTITION_ALREADY_EXIST'] . adm_back_link($this->u_action));
  141. }
  142. $db->sql_query('UPDATE ' . ADS_POSITIONS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array('lang_key' => $position_name)) . ' WHERE position_id = ' . $position_id);
  143. }
  144. else
  145. {
  146. $template->assign_vars(array(
  147. 'S_EDIT_POSITION' => true,
  148. 'POSITION_NAME' => $position_data['lang_key'],
  149. ));
  150. }
  151. }
  152. if ($submit)
  153. {
  154. $cache->destroy('sql', ADS_POSITIONS_TABLE);
  155. trigger_error((($action == 'add') ? $user->lang['POSTITION_ADD_SUCCESS'] : $user->lang['POSITION_EDIT_SUCCESS']) . adm_back_link($this->u_action));
  156. }
  157. }
  158. else if ($ad_id || !$position_name)
  159. {
  160. if (($action == 'edit' || $action == 'copy') && !$ad_data)
  161. {
  162. trigger_error($user->lang['AD_NOT_EXIST'] . adm_back_link($this->u_action));
  163. }
  164. // Check for errors
  165. if ($submit)
  166. {
  167. if (!$ad_name)
  168. {
  169. $error[] = $user->lang['NO_AD_NAME'];
  170. }
  171. if ($ad_time_end !== false && $ad_time_end > 0 && $ad_time_end < time())
  172. {
  173. $error[] = $user->lang['AD_TIME_END_BEFORE_NOW'];
  174. }
  175. if ($ad_owner)
  176. {
  177. $sql = 'SELECT user_id FROM ' . USERS_TABLE . '
  178. WHERE user_type <> ' . USER_IGNORE . '
  179. AND ' . ((is_numeric($ad_owner)) ? 'user_id = ' . (int) $ad_owner : 'username_clean = \'' . $db->sql_escape(utf8_clean_string($ad_owner)) . '\'');
  180. $result = $db->sql_query($sql);
  181. $user_row = $db->sql_fetchrow($result);
  182. $db->sql_freeresult($result);
  183. if (!$user_row)
  184. {
  185. $error[] = $user->lang['NO_USER'];
  186. }
  187. else
  188. {
  189. $ad_owner_id = $user_row['user_id'];
  190. }
  191. }
  192. }
  193. if ($submit && !sizeof($error))
  194. {
  195. $sql_ary = array(
  196. 'ad_name' => $ad_name,
  197. 'ad_code' => $ad_code,
  198. 'ad_note' => $ad_note,
  199. 'ad_time' => ($action == 'edit') ? $ad_data['ad_time'] : time(),
  200. 'ad_time_end' => ($ad_time_end !== false && $ad_time_end > 0) ? $ad_time_end : 0,
  201. 'ad_views' => request_var('ad_views', 0),
  202. 'ad_view_limit' => request_var('ad_view_limit', 0),
  203. 'ad_clicks' => request_var('ad_clicks', 0),
  204. 'ad_click_limit' => request_var('ad_click_limit', 0),
  205. 'ad_priority' => request_var('ad_priority', 5),
  206. 'ad_enabled' => (isset($_POST['ad_enabled'])) ? true : false,
  207. 'all_forums' => (isset($_POST['all_forums']) || !$config['ads_rules_forums']) ? true : false,
  208. 'ad_owner' => $ad_owner_id,
  209. );
  210. // Set them as able to see the ads page (stored as 1 for ad_user) and add them to the ads group if required
  211. if ($ad_owner_id && ($action != 'edit' || $ad_owner_id != $ad_data['ad_owner']))
  212. {
  213. $sql = 'UPDATE ' . USERS_TABLE . ' SET ad_owner = 1
  214. WHERE user_id = ' . (int) $ad_owner_id;
  215. $db->sql_query($sql);
  216. if ($config['ads_group'])
  217. {
  218. group_user_add($config['ads_group'], array($ad_owner_id));
  219. }
  220. }
  221. if ($action == 'edit')
  222. {
  223. $db->sql_query('UPDATE ' . ADS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE ad_id = ' . $ad_id);
  224. // Does the old owner have any ads anymore if the owner was changed?
  225. if ($ad_owner_id != $ad_data['ad_owner'])
  226. {
  227. $sql = 'SELECT COUNT(ad_id) AS count FROM ' . ADS_TABLE . '
  228. WHERE ad_owner = ' . $ad_data['ad_owner'];
  229. $db->sql_query($sql);
  230. $count = $db->sql_fetchfield('count');
  231. $db->sql_freeresult();
  232. if (!$count)
  233. {
  234. $sql = 'UPDATE ' . USERS_TABLE . ' SET ad_owner = 0
  235. WHERE user_id = ' . (int) $ad_data['ad_owner'];
  236. $db->sql_query($sql);
  237. if ($config['ads_group'])
  238. {
  239. group_user_del($config['ads_group'], array($ad_data['ad_owner']));
  240. }
  241. }
  242. }
  243. // This is the simplest way to update the groups/forums/positions list
  244. if ($config['ads_rules_groups'])
  245. {
  246. $db->sql_query('DELETE FROM ' . ADS_GROUPS_TABLE . ' WHERE ad_id = ' . $ad_id);
  247. }
  248. if ($config['ads_rules_forums'])
  249. {
  250. $db->sql_query('DELETE FROM ' . ADS_FORUMS_TABLE . ' WHERE ad_id = ' . $ad_id);
  251. }
  252. $db->sql_query('DELETE FROM ' . ADS_IN_POSITIONS_TABLE . ' WHERE ad_id = ' . $ad_id);
  253. }
  254. else
  255. {
  256. $db->sql_query('INSERT INTO ' . ADS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  257. $ad_id = $db->sql_nextid();
  258. }
  259. if ($config['ads_rules_groups'])
  260. {
  261. foreach ($ad_groups as $group_id)
  262. {
  263. $db->sql_query('INSERT INTO ' . ADS_GROUPS_TABLE . ' ' . $db->sql_build_array('INSERT', array('ad_id' => $ad_id, 'group_id' => $group_id)));
  264. }
  265. $cache->destroy('sql', ADS_GROUPS_TABLE);
  266. }
  267. if ($config['ads_rules_forums'])
  268. {
  269. foreach ($ad_forums as $forum_id)
  270. {
  271. $db->sql_query('INSERT INTO ' . ADS_FORUMS_TABLE . ' ' . $db->sql_build_array('INSERT', array('ad_id' => $ad_id, 'forum_id' => $forum_id)));
  272. }
  273. $cache->destroy('sql', ADS_FORUMS_TABLE);
  274. }
  275. foreach ($ad_positions as $position_id)
  276. {
  277. $sql_ary = array(
  278. 'ad_id' => $ad_id,
  279. 'position_id' => $position_id,
  280. 'ad_priority' => request_var('ad_priority', 5),
  281. 'ad_enabled' => (isset($_POST['ad_enabled'])) ? true : false,
  282. 'all_forums' => (isset($_POST['all_forums']) || !$config['ads_rules_forums']) ? true : false,
  283. );
  284. $db->sql_query('INSERT INTO ' . ADS_IN_POSITIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
  285. }
  286. trigger_error((($action == 'edit') ? $user->lang['AD_EDIT_SUCCESS'] : $user->lang['AD_ADD_SUCCESS']) . adm_back_link($this->u_action));
  287. }
  288. else
  289. {
  290. $ad_owner = utf8_normalize_nfc(request_var('ad_owner', '', true));
  291. if (($action == 'edit' || $action == 'copy') && !$submit && $ad_data['ad_owner'])
  292. {
  293. $sql = 'SELECT username FROM ' . USERS_TABLE . '
  294. WHERE user_id = ' . (int) $ad_data['ad_owner'];
  295. $db->sql_query($sql);
  296. $ad_owner = $db->sql_fetchfield('username');
  297. $db->sql_freeresult();
  298. }
  299. $template->assign_vars(array(
  300. 'S_ADD_AD' => ($action == 'add' || $action == 'copy') ? true : false,
  301. 'S_EDIT_AD' => ($action == 'edit') ? true : false,
  302. 'S_RULES_GROUPS' => ($config['ads_rules_groups']) ? true : false,
  303. 'S_RULES_FORUMS' => ($config['ads_rules_forums']) ? true : false,
  304. 'AD_NAME' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_name'] : $ad_name,
  305. 'AD_CODE' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_code'] : $ad_code,
  306. 'AD_NOTE' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_note'] : $ad_note,
  307. 'AD_TIME_END' => (($action == 'edit' || $action == 'copy') && !$submit) ? (($ad_data['ad_time_end']) ? date('d F Y', $ad_data['ad_time_end']) : '') : (($ad_time_end) ? date('d F Y', $ad_time_end) : ''),
  308. 'AD_VIEW_LIMIT' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_view_limit'] : request_var('ad_view_limit', 0),
  309. 'AD_VIEWS' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_views'] : request_var('ad_views', 0),
  310. 'AD_CLICK_LIMIT' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_click_limit'] : request_var('ad_click_limit', 0),
  311. 'AD_CLICKS' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_clicks'] : request_var('ad_clicks', 0),
  312. 'AD_PRIORITY' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_priority'] : request_var('ad_priority', 5),
  313. 'AD_ENABLED' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['ad_enabled'] : ((!$submit && $action == 'add') || isset($_POST['ad_enabled'])) ? true : false,
  314. 'ALL_FORUMS' => (($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['all_forums'] : ((!$submit && $action == 'add') || isset($_POST['all_forums'])) ? true : false,
  315. 'AD_OWNER' => $ad_owner,
  316. 'U_ACTION' => $this->u_action . '&amp;a=' . $ad_id . '&amp;action=' . $action,
  317. ));
  318. // List the groups
  319. $sql = 'SELECT group_id, group_name FROM ' . GROUPS_TABLE . ' ORDER BY group_name ASC';
  320. $result = $db->sql_query($sql);
  321. while ($row = $db->sql_fetchrow($result))
  322. {
  323. $template->assign_block_vars('groups', array(
  324. 'GROUP_ID' => $row['group_id'],
  325. 'GROUP_NAME' => (isset($user->lang['G_' . $row['group_name']])) ? $user->lang['G_' . $row['group_name']] : $row['group_name'],
  326. 'S_SELECTED' => (in_array($row['group_id'], ((($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['groups'] : $ad_groups))) ? true : false,
  327. ));
  328. }
  329. $db->sql_freeresult($result);
  330. // List the forums
  331. $right = $padding = 0;
  332. $padding_store = array('0' => 0);
  333. $sql = 'SELECT forum_id, forum_name, parent_id, left_id, right_id FROM ' . FORUMS_TABLE . ' ORDER BY left_id ASC';
  334. $result = $db->sql_query($sql);
  335. while ($row = $db->sql_fetchrow($result))
  336. {
  337. if ($row['left_id'] < $right)
  338. {
  339. $padding++;
  340. $padding_store[$row['parent_id']] = $padding;
  341. }
  342. else if ($row['left_id'] > $right + 1)
  343. {
  344. $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
  345. }
  346. $right = $row['right_id'];
  347. $template->assign_block_vars('forums', array(
  348. 'FORUM_ID' => $row['forum_id'],
  349. 'FORUM_NAME' => $row['forum_name'],
  350. 'S_SELECTED' => (in_array($row['forum_id'], ((($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['forums'] : $ad_forums))) ? true : false,
  351. ));
  352. for ($i = 0; $i < $padding; $i++)
  353. {
  354. $template->assign_block_vars('forums.level', array());
  355. }
  356. }
  357. $db->sql_freeresult($result);
  358. // List the positions
  359. $sql = 'SELECT * FROM ' . ADS_POSITIONS_TABLE . ' ORDER BY position_id ASC';
  360. $result = $db->sql_query($sql);
  361. while ($row = $db->sql_fetchrow($result))
  362. {
  363. $template->assign_block_vars('positions', array(
  364. 'POSITION_ID' => $row['position_id'],
  365. 'POSITION_NAME' => (isset($user->lang[$row['lang_key']])) ? $user->lang[$row['lang_key']] : $row['lang_key'],
  366. 'S_SELECTED' => (in_array($row['position_id'], ((($action == 'edit' || $action == 'copy') && !$submit) ? $ad_data['positions'] : $ad_positions))) ? true : false,
  367. ));
  368. }
  369. $db->sql_freeresult($result);
  370. }
  371. }
  372. break;
  373. /**************************************************************************************
  374. *
  375. * Delete Advertisement/Position
  376. *
  377. **************************************************************************************/
  378. case 'delete' :
  379. // Confirm that the ad/position exist.
  380. if ($ad_id)
  381. {
  382. if (!$ad_data)
  383. {
  384. trigger_error($user->lang['AD_NOT_EXIST'] . adm_back_link($this->u_action));
  385. }
  386. }
  387. else if ($position_id)
  388. {
  389. if (!$position_data)
  390. {
  391. trigger_error($user->lang['POSITION_NOT_EXIST'] . adm_back_link($this->u_action));
  392. }
  393. }
  394. if (confirm_box(true))
  395. {
  396. if ($ad_id)
  397. {
  398. $db->sql_query('DELETE FROM ' . ADS_TABLE . ' WHERE ad_id = ' . $ad_id);
  399. $db->sql_query('DELETE FROM ' . ADS_FORUMS_TABLE . ' WHERE ad_id = ' . $ad_id);
  400. $db->sql_query('DELETE FROM ' . ADS_GROUPS_TABLE . ' WHERE ad_id = ' . $ad_id);
  401. $db->sql_query('DELETE FROM ' . ADS_IN_POSITIONS_TABLE . ' WHERE ad_id = ' . $ad_id);
  402. $cache->destroy('sql', ADS_FORUMS_TABLE);
  403. $cache->destroy('sql', ADS_GROUPS_TABLE);
  404. // Does the old owner have any ads anymore if the owner was changed?
  405. $sql = 'SELECT COUNT(ad_id) AS count FROM ' . ADS_TABLE . '
  406. WHERE ad_owner = ' . $ad_data['ad_owner'];
  407. $db->sql_query($sql);
  408. $count = $db->sql_fetchfield('count');
  409. $db->sql_freeresult();
  410. if (!$count)
  411. {
  412. $sql = 'UPDATE ' . USERS_TABLE . ' SET ad_owner = 0
  413. WHERE user_id = ' . (int) $ad_data['ad_owner'];
  414. $db->sql_query($sql);
  415. if ($config['ads_group'])
  416. {
  417. group_user_del($config['ads_group'], array($ad_data['ad_owner']));
  418. }
  419. }
  420. trigger_error($user->lang['DELETE_AD_SUCCESS'] . adm_back_link($this->u_action));
  421. }
  422. else if ($position_id)
  423. {
  424. $db->sql_query('DELETE FROM ' . ADS_POSITIONS_TABLE . ' WHERE position_id = ' . $position_id);
  425. $db->sql_query('DELETE FROM ' . ADS_IN_POSITIONS_TABLE . ' WHERE position_id = ' . $position_id);
  426. $cache->destroy('sql', ADS_POSITIONS_TABLE);
  427. trigger_error($user->lang['DELETE_POSITION_SUCCESS'] . adm_back_link($this->u_action));
  428. }
  429. }
  430. else
  431. {
  432. confirm_box(false, (($ad_id) ? 'DELETE_AD' : 'DELETE_POSITION'));
  433. }
  434. redirect($this->u_action);
  435. break;
  436. /**************************************************************************************
  437. *
  438. * Enable/Disable Advertisements
  439. *
  440. **************************************************************************************/
  441. case 'enable' :
  442. case 'disable' :
  443. // Confirm that the ad exists
  444. if ($ad_id)
  445. {
  446. if (!$ad_data)
  447. {
  448. trigger_error($user->lang['AD_NOT_EXIST'] . adm_back_link($this->u_action));
  449. }
  450. }
  451. $sql = 'UPDATE ' . ADS_TABLE . '
  452. SET ad_enabled = ' . (($action == 'enable') ? 1 : 0) . '
  453. WHERE ad_id = ' . $ad_id;
  454. $db->sql_query($sql);
  455. $sql = 'UPDATE ' . ADS_IN_POSITIONS_TABLE . '
  456. SET ad_enabled = ' . (($action == 'enable') ? 1 : 0) . '
  457. WHERE ad_id = ' . $ad_id;
  458. $db->sql_query($sql);
  459. redirect($this->u_action);
  460. break;
  461. /**************************************************************************************
  462. *
  463. * List Advertisements, Positions, Config Settings
  464. *
  465. **************************************************************************************/
  466. default :
  467. validate_config_vars($config_vars, $this->new_config, $error);
  468. if ($submit && !sizeof($error))
  469. {
  470. // Config Variables
  471. foreach ($config_vars as $config_name => $null)
  472. {
  473. if (strpos($config_name, 'legend') === false)
  474. {
  475. set_config($config_name, $this->new_config[$config_name]);
  476. }
  477. }
  478. trigger_error($user->lang['ADVERTISEMENT_MANAGEMENT_UPDATE_SUCCESS'] . adm_back_link($this->u_action));
  479. }
  480. else
  481. {
  482. $template->assign_vars(array(
  483. 'S_POSITION_LIST' => true,
  484. 'S_AD_LIST' => true,
  485. 'U_ACTION' => $this->u_action,
  486. ));
  487. // Positions
  488. $sql = 'SELECT * FROM ' . ADS_POSITIONS_TABLE . ' ORDER BY position_id ASC';
  489. $result = $db->sql_query($sql);
  490. while ($row = $db->sql_fetchrow($result))
  491. {
  492. $template->assign_block_vars('positions', array(
  493. 'POSTITION_ID' => $row['position_id'],
  494. 'POSITION_NAME' => (isset($user->lang[$row['lang_key']])) ? $user->lang[$row['lang_key']] : $row['lang_key'],
  495. 'POSITION_CODE' => '{ADS_' . $row['position_id'] . '}',
  496. 'U_EDIT' => $this->u_action . '&amp;action=edit&amp;p=' . $row['position_id'],
  497. 'U_DELETE' => $this->u_action . '&amp;action=delete&amp;p=' . $row['position_id'],
  498. ));
  499. }
  500. $db->sql_freeresult($result);
  501. // Advertisements
  502. $sql_ary = array(
  503. 'SELECT' => 'u.user_id, u.username, u.user_colour, a. *',
  504. 'FROM' => array(
  505. ADS_TABLE => 'a',
  506. ),
  507. 'LEFT_JOIN' => array(
  508. array(
  509. 'FROM' => array(USERS_TABLE => 'u'),
  510. 'ON' => 'u.user_id = a.ad_owner'
  511. )
  512. ),
  513. 'ORDER_BY' => 'a.ad_owner DESC, a.ad_enabled DESC, a.ad_name ASC',
  514. );
  515. $sql = $db->sql_build_query('SELECT', $sql_ary);
  516. $result = $db->sql_query($sql);
  517. while ($row = $db->sql_fetchrow($result))
  518. {
  519. $template->assign_block_vars('ads', array(
  520. 'AD_ID' => $row['ad_id'],
  521. 'AD_NAME' => $row['ad_name'],
  522. 'AD_NOTE' => nl2br($row['ad_note']),
  523. 'AD_TIME' => ($row['ad_time']) ? date('Y-m-d', $row['ad_time']) : $user->lang['NA'],
  524. 'AD_ENABLED' => ($row['ad_enabled']) ? $user->lang['TRUE'] : '<strong>' . $user->lang['FALSE'] . '</strong>',
  525. 'AD_VIEWS' => $row['ad_views'],
  526. 'AD_CLICKS' => ($row['ad_clicks']) ? $row['ad_clicks'] : $user->lang['0_OR_NA'],
  527. 'AD_PRIORITY' => $row['ad_priority'],
  528. 'AD_OWNER' => ($row['ad_owner']) ? get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']) : '',
  529. 'U_EDIT' => $this->u_action . '&amp;action=edit&amp;a=' . $row['ad_id'],
  530. 'U_DELETE' => $this->u_action . '&amp;action=delete&amp;a=' . $row['ad_id'],
  531. 'U_COPY' => $this->u_action . '&amp;action=copy&amp;a=' . $row['ad_id'],
  532. 'U_ENABLE_DISABLE' => $this->u_action . '&amp;action=' . (($row['ad_enabled']) ? 'disable' : 'enable') . '&amp;a=' . $row['ad_id'],
  533. ));
  534. }
  535. $db->sql_freeresult($result);
  536. // Config Variables
  537. foreach ($config_vars as $config_key => $vars)
  538. {
  539. if (!is_array($vars) && strpos($config_key, 'legend') === false)
  540. {
  541. continue;
  542. }
  543. if (strpos($config_key, 'legend') !== false)
  544. {
  545. $template->assign_block_vars('options', array(
  546. 'S_LEGEND' => true,
  547. 'LEGEND' => (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
  548. );
  549. continue;
  550. }
  551. $type = explode(':', $vars['type']);
  552. $l_explain = '';
  553. if ($vars['explain'] && isset($vars['lang_explain']))
  554. {
  555. $l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
  556. }
  557. else if ($vars['explain'])
  558. {
  559. $l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
  560. }
  561. $template->assign_block_vars('options', array(
  562. 'KEY' => $config_key,
  563. 'TITLE' => (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
  564. 'S_EXPLAIN' => $vars['explain'],
  565. 'TITLE_EXPLAIN' => $l_explain,
  566. 'CONTENT' => build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars),
  567. ));
  568. }
  569. }
  570. break;
  571. }
  572. $template->assign_vars(array(
  573. 'ERROR' => implode('<br />', $error),
  574. 'ADS_VERSION' => $config['ads_version'],
  575. ));
  576. }
  577. function group_select($selected_value, $key)
  578. {
  579. global $db, $user;
  580. $selected = ($selected_value == 0) ? ' selected="selected"' : '';
  581. $select = '<option value="0"' . $selected . '>----- ' . $user->lang['NA'] . ' -----</option>';
  582. $sql = 'SELECT *
  583. FROM ' . GROUPS_TABLE;
  584. $result = $db->sql_query($sql);
  585. while ($row = $db->sql_fetchrow($result))
  586. {
  587. if ($user->data['user_type'] != USER_FOUNDER && $row['group_founder_manage'])
  588. {
  589. continue;
  590. }
  591. $selected = ($selected_value == $row['group_id']) ? ' selected="selected"' : '';
  592. $lang = (isset($user->lang['G_' . $row['group_name']])) ? $user->lang['G_' . $row['group_name']] : $row['group_name'];
  593. $select .= '<option value="' . $row['group_id'] . '"' . $selected . '>' . $lang . '</option>';
  594. }
  595. $db->sql_freeresult($result);
  596. return $select;
  597. }
  598. }
  599. ?>