function AccountSearchSelect(&$s, $request = null) { global $DB; if ($request != null) { $_REQUEST = array_merge($_REQUEST, $request); } $last_hour = gmdate('G', TIME_NOW - 3600); $this_hour = gmdate('G', TIME_NOW); $sorters = array_merge($DB->GetColumns('tlx_accounts', TRUE, TRUE), $DB->GetColumns('tlx_account_fields', TRUE, TRUE), $DB->GetColumns('tlx_account_hourly_stats', TRUE, TRUE), array('username' => '`tlx_accounts`.`username`', 'avg_rating' => '`ratings_total`/`ratings`', 'raw_in_last_hr' => '`raw_in_' . $last_hour . '`', 'unique_in_last_hr' => '`unique_in_' . $last_hour . '`', 'raw_out_last_hr' => '`raw_out_' . $last_hour . '`', 'unique_out_last_hr' => '`unique_out_' . $last_hour . '`', 'clicks_last_hr' => '`clicks_' . $last_hour . '`', 'raw_in_this_hr' => '`raw_in_' . $this_hour . '`', 'unique_in_this_hr' => '`unique_in_' . $this_hour . '`', 'raw_out_this_hr' => '`raw_out_' . $this_hour . '`', 'unique_out_this_hr' => '`unique_out_' . $this_hour . '`', 'clicks_this_hr' => '`clicks_' . $this_hour . '`')); if (preg_match('~(.*?)_days_(\\d+)~', $_REQUEST['order'], $matches)) { $sorters[$_REQUEST['order']] = "SUM(`{$matches[1]}`)"; } $s = new SelectBuilder('*,' . $sorters[$_REQUEST['order']] . ' AS `sorter`', 'tlx_accounts'); $fulltext = array('title,description,keywords'); $user = $DB->GetColumns('tlx_account_fields'); if ($_REQUEST['field'] == 'avg_rating') { $_REQUEST['field'] = 'ratings_total/ratings'; } if ($_REQUEST['field'] == 'return_percent') { $_REQUEST['search'] = $_REQUEST['search'] / 100; } // Special handling of date searches (transform MM-DD-YYYY to YYYY-MM-DD format) if (preg_match('~^date_~', $_REQUEST['field'])) { $_REQUEST['search'] = trim($_REQUEST['search']); if (preg_match('~^(\\d\\d)-(\\d\\d)-(\\d\\d\\d\\d)$~', $_REQUEST['search'], $date)) { $_REQUEST['search_type'] = ST_BETWEEN; $_REQUEST['search'] = "{$date[3]}-{$date[1]}-{$date[2]} 00:00:00,{$date[3]}-{$date[1]}-{$date[2]} 23:59:59"; } else { if (preg_match('~^\\d\\d\\d\\d-\\d\\d-\\d\\d$~', $_REQUEST['search'])) { $_REQUEST['search_type'] = ST_BETWEEN; $_REQUEST['search'] = "{$_REQUEST['search']} 00:00:00,{$_REQUEST['search']} 23:59:59"; } } $_REQUEST['search'] = preg_replace('~(\\d\\d)-(\\d\\d)-(\\d\\d\\d\\d)~', '\\3-\\1-\\2', $_REQUEST['search']); } if (preg_match('~_days_\\d+~', $_REQUEST['order'])) { $s->AddJoin('tlx_accounts', 'tlx_account_daily_stats', 'LEFT', 'username'); $s->AddGroup('tlx_accounts.username'); $s->AddWhereString("`date_stats` >= DATE_ADD('" . MYSQL_CURDATE . "', INTERVAL -{$matches[2]} DAY)"); } else { if (preg_match('~(raw_|unique_|clicks_)~', $_REQUEST['order'])) { $s->AddJoin('tlx_accounts', 'tlx_account_hourly_stats', '', 'username'); } } if (in_array($_REQUEST['field'], $user) || in_array($_REQUEST['order'], $user)) { $s->AddJoin('tlx_accounts', 'tlx_account_fields', '', 'username'); } if (in_array($_REQUEST['field'], $user)) { $s->AddWhere($_REQUEST['field'], $_REQUEST['search_type'], $_REQUEST['search'], $_REQUEST['search_type'] != ST_EMPTY); } else { if (in_array($_REQUEST['field'], $fulltext)) { $s->AddFulltextWhere($_REQUEST['field'], $_REQUEST['search'], $_REQUEST['search_type'] != ST_EMPTY); } else { $s->AddWhere($_REQUEST['field'], $_REQUEST['search_type'], $_REQUEST['search'], $_REQUEST['search_type'] != ST_EMPTY); } } $s_checked = count($_REQUEST['status']); if ($s_checked > 0 && $s_checked < 3) { $s->AddWhere('status', ST_IN, join(',', $_REQUEST['status'])); } if (isset($_REQUEST['locked'])) { $s->AddWhere('locked', ST_MATCHES, 1); } if (isset($_REQUEST['disabled'])) { $s->AddWhere('disabled', ST_MATCHES, 1); } if (isset($_REQUEST['edited'])) { $s->AddWhere('edited', ST_MATCHES, 1); } if (count($_REQUEST['categories']) > 0 && !in_array('', $_REQUEST['categories'])) { $s->AddWhere('category_id', isset($_REQUEST['cat_exclude']) ? ST_NOT_IN : ST_IN, join(',', $_REQUEST['categories'])); } $_REQUEST['order'] = 'sorter'; $_REQUEST['order_next'] = 'tlx_accounts.username'; return TRUE; }
function txGalleryBreakdown() { global $DB, $json, $C; VerifyPrivileges(P_GALLERY, TRUE); $out = array('status' => JSON_SUCCESS, 'breakdown' => array()); switch ($_REQUEST['group']) { case 'category': $categories =& $DB->FetchAll('SELECT * FROM `tx_categories` ORDER BY `name`'); $breakdown = array(); foreach ($categories as $category) { $s = new SelectBuilder('COUNT(*) AS `amount`', 'tx_galleries'); if ($_REQUEST['status']) { $s->AddWhere('status', ST_MATCHES, $_REQUEST['status']); } if ($_REQUEST['type']) { $s->AddWhere('type', ST_MATCHES, $_REQUEST['type']); } $s->AddFulltextWhere('categories', $category['tag']); $amount = $DB->Count($s->Generate(), $s->binds); $breakdown[] = array('grouper' => htmlspecialchars($category['name']), 'amount' => number_format($amount, 0, $C['dec_point'], $C['thousands_sep']), 'sorter' => $amount); } usort($breakdown, 'txBreakdownCmp'); $out['breakdown'] =& $breakdown; break; case 'sponsor': $s = new SelectBuilder("`name` AS `grouper`,COUNT(*) AS `amount`", 'tx_galleries'); $s->AddJoin('tx_galleries', 'tx_sponsors', 'LEFT', 'sponsor_id'); if ($_REQUEST['type']) { $s->AddWhere('type', ST_MATCHES, $_REQUEST['type']); } if ($_REQUEST['status']) { $s->AddWhere('status', ST_MATCHES, $_REQUEST['status']); } $s->AddGroup('tx_galleries.sponsor_id'); $s->AddOrder('amount', 'DESC'); $result = $DB->Query($s->Generate(), $s->binds); while ($breakdown = $DB->NextRow($result)) { $breakdown['amount'] = number_format($breakdown['amount'], 0, $C['dec_point'], $C['thousands_sep']); $breakdown['grouper'] = $breakdown['grouper'] ? ucfirst(htmlspecialchars($breakdown['grouper'])) : '-'; $out['breakdown'][] = $breakdown; } $DB->Free($result); break; default: $group_field = array('added' => 'DATE_FORMAT(date_added, \'%Y-%m-%d\')', 'displayed' => 'DATE_FORMAT(date_displayed, \'%Y-%m-%d\')', 'format' => 'format'); $s = new SelectBuilder("{$group_field[$_REQUEST['group']]} AS `grouper`,COUNT(*) AS `amount`", 'tx_galleries'); if ($_REQUEST['type']) { $s->AddWhere('type', ST_MATCHES, $_REQUEST['type']); } if ($_REQUEST['status']) { $s->AddWhere('status', ST_MATCHES, $_REQUEST['status']); } $result = $DB->Query($s->Generate() . " GROUP BY {$group_field[$_REQUEST['group']]} ORDER BY " . (in_array($_REQUEST['group'], array('added', 'displayed')) ? '`grouper`' : '`amount`') . " DESC", $s->binds); while ($breakdown = $DB->NextRow($result)) { $breakdown['amount'] = number_format($breakdown['amount'], 0, $C['dec_point'], $C['thousands_sep']); $breakdown['grouper'] = $breakdown['grouper'] ? ucfirst(htmlspecialchars($breakdown['grouper'])) : '-'; $out['breakdown'][] = $breakdown; } $DB->Free($result); break; } $type = $_REQUEST['type'] ? ucfirst(htmlspecialchars($_REQUEST['type'])) : 'Overall'; $status = $_REQUEST['status'] ? ucfirst(htmlspecialchars($_REQUEST['status'])) : ''; $by = ucfirst(htmlspecialchars($_REQUEST['group'])); $out['type'] = "{$type} {$status} Galleries By {$by}"; echo $json->encode($out); }
/** * Callback for comment search select * * @param SelectBuilder $s The SelectBuilder object */ function CommentSelect($s) { if ($_REQUEST['field'] == 'comment') { $s->AddFulltextWhere('comment', $_REQUEST['search'], TRUE); } else { $s->AddWhere($_REQUEST['field'], $_REQUEST['search_type'], $_REQUEST['search'], FALSE); } $s->AddWhere('status', ST_MATCHES, $_REQUEST['status'], TRUE); return TRUE; }
function compile_pages_tag($tag_args) { global $DB; $attrs = $this->parse_attributes($tag_args); if (empty($attrs['var'])) { return $this->syntax_error("pages: missing 'var' attribute"); } $s = new SelectBuilder('*,`tlx_categories`.`name` AS `category`', 'tlx_pages'); $s->AddJoin('tlx_pages', 'tlx_categories', 'LEFT', 'category_id'); if (isset($attrs['tags'])) { $s->AddFulltextWhere('tags', $attrs['tags']); } if (isset($attrs['category'])) { if (is_numeric($attrs['category'])) { if ($attrs['category'] == 0) { $s->AddWhere('tlx_pages.category_id', ST_NULL, null); } else { $s->AddWhere('tlx_pages.category_id', ST_MATCHES, $attrs['category']); } } else { if ($attrs['category'][0] == '$') { $attrs['category'] = $this->parse_vars($attrs['category']); $s->AddWhere('tlx_pages.category_id', ST_MATCHES, '%CATEGORY_ID%'); } else { $category = $DB->Row('SELECT * FROM `tlx_categories` WHERE `name`=?', array($attrs['category'])); if (!$category) { return $this->syntax_error("pages: 'category' attribute has an invalid category name specified"); } $s->AddWhere('tlx_pages.category_id', ST_MATCHES, $category['category_id']); } } } if (isset($attrs['filecontains'])) { $s->AddWhere('filename', ST_CONTAINS, $attrs['filecontains']); } if (isset($attrs['amount'])) { $s->SetLimit($attrs['amount']); } $s->AddOrder('build_order', 'ASC'); $query = $DB->Prepare($s->Generate(), $s->binds); $query = str_replace("='%CATEGORY_ID%'", "\".({$attrs['category']} ? \"='\".{$attrs['category']}.\"'\" : ' IS NULL').\"", $query); $attrs['var'] = $this->parse_vars($attrs['var']); return S_PHP . " {$attrs['var']} =& \$GLOBALS['DB']->FetchAll(\"{$query}\"); " . E_PHP; }
function &GenerateQuery() { global $DB, $configuration; $s = new SelectBuilder('*', 'tx_galleries'); $s->AddWhere('allow_scan', ST_MATCHES, 1); if (count($configuration['status']) > 0 && count($configuration['status']) < 5) { $s->AddWhere('status', ST_IN, join(',', array_keys($configuration['status']))); } if (count($configuration['type']) == 1) { $keys = array_keys($configuration['type']); $s->AddWhere('type', ST_MATCHES, $keys[0]); } if (count($configuration['format']) == 1) { $keys = array_keys($configuration['format']); $s->AddWhere('format', ST_MATCHES, $keys[0]); } if (is_numeric($configuration['id_start']) && is_numeric($configuration['id_end'])) { $s->AddWhere('gallery_id', ST_BETWEEN, "{$configuration['id_start']},{$configuration['id_end']}"); } if (preg_match(RE_DATETIME, $configuration['date_added_start']) && preg_match(RE_DATETIME, $configuration['date_added_end'])) { $s->AddWhere('date_added', ST_BETWEEN, "{$configuration['date_added_start']},{$configuration['date_added_end']}"); } if (preg_match(RE_DATETIME, $configuration['date_approved_start']) && preg_match(RE_DATETIME, $configuration['date_approved_end'])) { $s->AddWhere('date_approved', ST_BETWEEN, "{$configuration['date_approved_start']},{$configuration['date_approved_end']}"); } if (preg_match(RE_DATETIME, $configuration['date_scanned_start']) && preg_match(RE_DATETIME, $configuration['date_scanned_end'])) { $s->AddWhere('date_scanned', ST_BETWEEN, "{$configuration['date_scanned_start']},{$configuration['date_scanned_end']}"); } // Only galleries submitted by partners if ($configuration['only_parter']) { $s->AddWhere('partner', ST_NOT_EMPTY); } // Only galleries that currently have a zero thumbnail count if ($configuration['only_zerothumb']) { $s->AddWhere('thumbnails', ST_MATCHES, 0); } // Only galleries that have not yet been scanned if ($configuration['only_notscanned']) { $s->AddWhere('date_scanned', ST_NULL); } // Specific categories selected if (!IsEmptyString($configuration['categories'][0])) { $tags = array(); foreach ($configuration['categories'] as $category_id) { $tags[] = $DB->Count('SELECT `tag` FROM `tx_categories` WHERE `category_id`=?', array($category_id)); } if (count($tags)) { $s->AddFulltextWhere('categories', join(' ', $tags)); } } // Specific sponsors selected if (!IsEmptyString($configuration['sponsors'][0])) { $s->AddWhere('sponsor_id', ST_IN, join(',', array_unique($configuration['sponsors']))); } // Only galleries that do not currently have a preview thumbnail if ($configuration['only_nothumb']) { $s->AddWhere('has_preview', ST_MATCHES, 0); } return $s; }
function compile_ad_tag($tag_args) { global $DB; $defaults = array('pagedupes' => 'false', 'weight' => 'any', 'order' => 'times_displayed, (unique_clicks/times_displayed) DESC'); $attrs = $this->parse_attributes($tag_args); $attrs = array_merge($defaults, $attrs); // Convert boolean values $attrs['pagedupes'] = $this->to_bool($attrs['pagedupes']); // Prepare RAND() values in order $attrs['order'] = preg_replace('~rand\\(\\)~i', 'RAND(%RAND%)', $attrs['order']); $s = new SelectBuilder('*,`lx_ads`.`ad_id` AS `ad_id`', 'lx_ads'); // Process pagedupes if ($attrs['pagedupes'] === FALSE) { $s->AddJoin('lx_ads', 'lx_ads_used_page', 'LEFT', 'ad_id'); $s->AddWhere('lx_ads_used_page.ad_id', ST_NULL, null); } // Process tags attribute if (isset($attrs['tags'])) { $s->AddFulltextWhere('tags', $attrs['tags']); } // Process weight attribute if (isset($attrs['weight']) && $attrs['weight'] != 'any') { $s->AddWhereString("`weight` {$attrs['weight']}"); } $s->SetOrderString($attrs['order'], $DB->GetColumns('lx_ads')); $s->SetLimit('1'); $query = $DB->Prepare($s->Generate(), $s->binds); // Perform replacements for placeholders $replacements = array('%RAND%' => '".rand()."'); foreach ($replacements as $find => $replace) { $query = str_replace($find, $replace, $query); } return S_PHP . " if( !isset(\$GLOBALS['_CLEAR_PAGE_USED_']) )\n{\n" . "\$GLOBALS['DB']->Update('DELETE FROM `lx_ads_used_page`');\n" . "\$GLOBALS['_CLEAR_PAGE_USED_'] = TRUE;\n" . "}\n" . "\$_temp_ad = \$GLOBALS['DB']->Row(\"{$query}\");\n" . "if( \$_temp_ad )\n{\n" . "\$GLOBALS['DB']->Update(\"UPDATE `lx_ads` SET `times_displayed`=`times_displayed`+1 WHERE `ad_id`=?\", array(\$_temp_ad['ad_id']));\n" . "\$GLOBALS['DB']->Update(\"REPLACE INTO `lx_ads_used_page` VALUES (?)\", array(\$_temp_ad['ad_id']));\n" . "echo \$_temp_ad['ad_html'];\n" . "}\n" . E_PHP; }
function compile_galleries_tag($tag_args) { global $DB; $defaults = array('preview' => 'any', 'type' => 'submitted', 'format' => 'any', 'category' => 'MIXED', 'sponsor' => 'any', 'amount' => '20', 'globaldupes' => isset($this->defines['globaldupes']) ? $this->defines['globaldupes'] : 'true', 'pagedupes' => isset($this->defines['pagedupes']) ? $this->defines['pagedupes'] : 'false', 'getnew' => 'true', 'allowused' => 'true', 'description' => 'false', 'weight' => 'any'); $attrs = $this->parse_attributes($tag_args); $attrs = array_merge($defaults, $attrs); if (empty($attrs['var'])) { return $this->syntax_error("galleries: missing 'var' attribute"); } if (!empty($attrs['previewsize']) && !preg_match('~^\\d+x\\d+$~', $attrs['previewsize'])) { return $this->syntax_error("galleries: the 'previewsize' value must be provided and in WxH format"); } $attrs['var'] = $this->parse_vars($attrs['var']); // Convert boolean values $attrs['preview'] = $this->to_bool($attrs['preview']); $attrs['globaldupes'] = $this->to_bool($attrs['globaldupes']); $attrs['pagedupes'] = $this->to_bool($attrs['pagedupes']); $attrs['getnew'] = $this->to_bool($attrs['getnew']); $attrs['allowused'] = $this->to_bool($attrs['allowused']); $attrs['description'] = $this->to_bool($attrs['description']); // Whether or not to pull a preview thumb $pull_preview = 'FALSE'; // Set default sorting options if ($attrs['getnew'] === TRUE) { if (isset($attrs['order']) && !isset($attrs['reorder'])) { $attrs['reorder'] = $attrs['order']; } else { if (!isset($attrs['reorder'])) { $attrs['reorder'] = 'date_displayed DESC, date_approved'; } } if (!isset($attrs['order'])) { $attrs['order'] = 'date_approved'; } } else { if (isset($attrs['order']) && !isset($attrs['reorder'])) { $attrs['reorder'] = $attrs['order']; } else { if (!isset($attrs['reorder'])) { $attrs['reorder'] = 'date_displayed DESC, date_approved'; } } if (!isset($attrs['order'])) { $attrs['order'] = 'date_displayed DESC, date_approved'; } } // Prepare RAND() values in order and reorder $attrs['order'] = preg_replace('~rand\\(\\)~i', 'RAND(%RAND%)', $attrs['order']); $attrs['reorder'] = preg_replace('~rand\\(\\)~i', 'RAND(%RAND%)', $attrs['reorder']); // Pulling galleries from a text file if (isset($attrs['file'])) { // galleries from text file...coming soon } else { if (isset($attrs['select'])) { if (!isset($attrs['reselect']) || empty($attrs['reselect'])) { $attrs['reselect'] = $attrs['select']; } $replacements = array('%MYSQL_CURDATE%' => '".MYSQL_CURDATE."', '%MYSQL_NOW%' => '".MYSQL_NOW."', '%RAND%' => '".rand()."', '%LIMIT%' => isset($attrs['fillvar']) ? '" . (' . $attrs['amount'] . ' - count(' . $attrs['fillvar'] . ') - $_got) . "' : '" . (' . $attrs['amount'] . ' - $_got) . "', '%PAGEID%' => '" . $this->vars[\'page\'][\'page_id\'] . "', '%FILL%' => '" . (' . $attrs['amount'] . ' - count(' . $attrs['fillvar'] . ')). "'); foreach ($replacements as $find => $replace) { $attrs['select'] = str_replace($find, $replace, $attrs['select']); $attrs['reselect'] = str_replace($find, $replace, $attrs['reselect']); } return S_PHP . NEWLINE . "if( \$GLOBALS['_build_type'] == BT_BUILD_WITH_NEW )" . NEWLINE . "{" . NEWLINE . "{$attrs['var']} =& LoadGalleries(\"{$attrs['select']}\", \$this->vars['this_page']['page_id'], \$this->vars['this_page']['category_id']);" . NEWLINE . "}" . NEWLINE . "else" . NEWLINE . "{" . NEWLINE . "{$attrs['var']} =& LoadGalleries(\"{$attrs['reselect']}\", \$this->vars['this_page']['page_id'], \$this->vars['this_page']['category_id']);" . NEWLINE . "}" . NEWLINE . E_PHP; } else { if (isset($attrs['age']) && (isset($attrs['minage']) || isset($attrs['maxage']))) { return $this->syntax_error("galleries: 'age' attribute cannot be combined with 'minage' or 'maxage'"); } $s = new SelectBuilder('*,`tx_galleries`.`gallery_id` AS `gallery_id`', 'tx_galleries'); // Process globaldupes if ($attrs['globaldupes'] === FALSE) { $s->AddJoin('tx_galleries', 'tx_gallery_used', 'LEFT', 'gallery_id'); $s->AddWhere('tx_gallery_used.gallery_id', ST_NULL, null); } // Process pagedupes if ($attrs['pagedupes'] === FALSE) { $s->AddJoin('tx_galleries', 'tx_gallery_used_page', 'LEFT', 'gallery_id'); $s->AddWhere('tx_gallery_used_page.gallery_id', ST_NULL, null); } // Process preview attribute if ($attrs['preview'] === TRUE) { if (empty($attrs['previewsize'])) { $s->AddWhere('has_preview', ST_MATCHES, 1); $pull_preview = 'TRUE'; } else { $s->AddJoin('tx_galleries', 'tx_gallery_previews', '', 'gallery_id'); $s->AddWhere('dimensions', ST_MATCHES, $attrs['previewsize']); } } else { if ($attrs['preview'] === FALSE) { $s->AddWhere('has_preview', ST_MATCHES, 0); } else { $pull_preview = 'TRUE'; } } // Process type attribute $attrs['type'] = strtolower($attrs['type']); if ($attrs['type'] != 'any') { $s->AddWhere('type', ST_MATCHES, $attrs['type']); } // Process format attribute $attrs['format'] = strtolower($attrs['format']); if ($attrs['format'] != 'any') { $s->AddWhere('format', ST_MATCHES, $attrs['format']); } // Process description attribute if ($attrs['description'] === TRUE) { $s->AddWhere('description', ST_NOT_EMPTY, null); } // Process partner attribute if (isset($attrs['partner'])) { $s->AddWhere('partner', ST_MATCHES, $attrs['partner']); } // Process keywords attribute if (isset($attrs['keywords'])) { $s->AddFulltextWhere('keywords', $attrs['keywords']); } // Process tags attribute if (isset($attrs['tags'])) { $s->AddFulltextWhere('tags', $attrs['tags']); } // Process category attribute if (isset($attrs['category'])) { $attrs['category'] = FormatCommaSeparated($attrs['category']); if ($this->flags['category_id']) { if (!isset($GLOBALS['CATEGORY_CACHE_ID'])) { $GLOBALS['CATEGORY_CACHE_ID'] =& $DB->FetchAll('SELECT * FROM `tx_categories`', null, 'category_id'); } $s->AddFulltextWhere('categories', $GLOBALS['CATEGORY_CACHE_ID'][$this->flags['category_id']]['tag'], TRUE); } else { if (strtoupper($attrs['category']) != 'MIXED') { $fulltext = array(); if (!isset($GLOBALS['CATEGORY_CACHE'])) { $GLOBALS['CATEGORY_CACHE'] =& $DB->FetchAll('SELECT * FROM `tx_categories`', null, 'name'); } foreach (explode(',', $attrs['category']) as $category) { switch ($category) { case 'MIXED': case 'mixed': case 'Mixed': $fulltext[] = MIXED_CATEGORY; break; default: $minus = ''; if (preg_match('~^([-+])(.*)~i', $category, $matches)) { $minus = $matches[1]; $category = $matches[2]; } if ($GLOBALS['CATEGORY_CACHE'][$category]) { $fulltext[] = $minus . $GLOBALS['CATEGORY_CACHE'][$category]['tag']; } } } if (count($fulltext) < 1) { return $this->syntax_error("galleries: 'category' attribute contains an invalid category name [{$attrs['category']}]"); } $s->AddFulltextWhere('categories', join(' ', $fulltext)); } } } // Process sponsor attribute if (isset($attrs['sponsor'])) { $attrs['sponsor'] = FormatCommaSeparated($attrs['sponsor']); if (strtolower($attrs['sponsor']) != 'any') { $sponsors =& $DB->FetchAll('SELECT * FROM `tx_sponsors`', null, 'name'); $sponsor_ids = array(); $not_in = FALSE; foreach (explode(',', $attrs['sponsor']) as $sponsor) { $minus = FALSE; if (preg_match('~^-(.*)~i', $sponsor, $matches)) { $minus = TRUE; $sponsor = $matches[1]; } if ($sponsor == 'any') { $not_in = TRUE; } else { if (isset($sponsors[$sponsor])) { $sponsor_ids[] = $sponsors[$sponsor]['sponsor_id']; } } } if ($not_in) { $s->AddMultiWhere(array('sponsor_id', 'sponsor_id'), array(ST_NOT_IN, ST_NULL), array(join(',', $sponsor_ids), null)); } else { $s->AddWhere('sponsor_id', ST_IN, join(',', $sponsor_ids)); } } } // Process weight attribute if (isset($attrs['weight']) && $attrs['weight'] != 'any') { $s->AddWhereString("`weight` {$attrs['weight']}"); } // Process amount attribute $s->SetLimit($attrs['amount']); if (isset($attrs['fillvar'])) { $attrs['fillvar'] = $this->parse_vars($attrs['fillvar']); $s->SetLimit('%FILL%'); } // Get available sorting columns and copy the SelectBuilder object // Little hack to workaround differences in object copying between PHP4 and PHP5 $tx_galleries = $DB->GetColumns('tx_galleries'); if (function_exists('array_combine')) { $s_new = unserialize(serialize($s)); } else { $s_new = $s; } if ($attrs['getnew'] === TRUE) { $s->AddWhere('status', ST_IN, 'used,holding'); $s->SetOrderString($attrs['reorder'], $tx_galleries); $s_new->AddWhere('status', ST_MATCHES, 'approved'); $s_new->AddMultiWhere(array('date_scheduled', 'date_scheduled'), array(ST_NULL, ST_LESS), array(null, '%MYSQL_NOW%')); $s_new->SetOrderString($attrs['order'], $tx_galleries); } else { $s->AddWhere('status', ST_IN, 'used,holding'); $s->SetOrderString($attrs['reorder'], $tx_galleries); $s_new->AddWhere('status', ST_IN, 'used,holding'); $s_new->SetOrderString($attrs['order'], $tx_galleries); } // Process age, minage and maxage attributes if (isset($attrs['age'])) { $s->AddWhereString("`date_displayed` BETWEEN SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['age']} DAY) AND SUBDATE('%MYSQL_CURDATE% 23:59:59', INTERVAL {$attrs['age']} DAY)"); if ($attrs['getnew'] !== TRUE) { $s_new->AddWhereString("`date_displayed` BETWEEN SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['age']} DAY) AND SUBDATE('%MYSQL_CURDATE% 23:59:59', INTERVAL {$attrs['age']} DAY)"); } } else { if (isset($attrs['minage']) && isset($attrs['maxage'])) { $s->AddWhereString("`date_displayed` BETWEEN SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['maxage']} DAY) AND SUBDATE('%MYSQL_CURDATE% 23:59:59', INTERVAL {$attrs['minage']} DAY)"); if ($attrs['getnew'] !== TRUE) { $s_new->AddWhereString("`date_displayed` BETWEEN SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['maxage']} DAY) AND SUBDATE('%MYSQL_CURDATE% 23:59:59', INTERVAL {$attrs['minage']} DAY)"); } } else { if (isset($attrs['minage'])) { $s->AddWhereString("`date_displayed` <= SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['minage']} DAY)"); if ($attrs['getnew'] !== TRUE) { $s_new->AddWhereString("`date_displayed` <= SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['minage']} DAY)"); } } else { if (isset($attrs['maxage'])) { $s->AddWhereString("`date_displayed` >= SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['maxage']} DAY)"); if ($attrs['getnew'] !== TRUE) { $s_new->AddWhereString("`date_displayed` >= SUBDATE('%MYSQL_CURDATE% 00:00:00', INTERVAL {$attrs['maxage']} DAY)"); } } } } } // Generate the build and build with new queries $b_query = $DB->Prepare($s->Generate(), $s->binds); $bwn_query = $DB->Prepare($s_new->Generate(), $s_new->binds); $s->SetLimit('%LIMIT%'); $bwnau_query = $DB->Prepare($s->Generate(), $s->binds); $replacements = array('%MYSQL_CURDATE%' => '".MYSQL_CURDATE."', '%MYSQL_NOW%' => '".MYSQL_NOW."', '%RAND%' => '".rand()."', '%LIMIT%' => isset($attrs['fillvar']) ? '" . (' . $attrs['amount'] . ' - count(' . $attrs['fillvar'] . ') - $_got) . "' : '" . (' . $attrs['amount'] . ' - $_got) . "', '%PAGEID%' => '" . $this->vars[\'page\'][\'page_id\'] . "', '%FILL%' => '" . (' . $attrs['amount'] . ' - count(' . $attrs['fillvar'] . ')). "'); foreach ($replacements as $find => $replace) { $b_query = str_replace($find, $replace, $b_query); $bwn_query = str_replace($find, $replace, $bwn_query); $bwnau_query = str_replace($find, $replace, $bwnau_query); } return S_PHP . NEWLINE . "{$attrs['var']} = null;" . NEWLINE . "\$this->vars['_temp_galleries'] = null;" . NEWLINE . (isset($attrs['fillvar']) ? 'if(' . $attrs['amount'] . ' - count(' . $attrs['fillvar'] . ') > 0)' . NEWLINE . '{' . NEWLINE : '') . "if( \$GLOBALS['_build_type'] == BT_BUILD_WITH_NEW )" . NEWLINE . "{" . NEWLINE . "{$attrs['var']} =& LoadGalleries(\"{$bwn_query}\", \$this->vars['this_page']['page_id'], \$this->vars['this_page']['category_id'], {$pull_preview});" . NEWLINE . ($attrs['getnew'] === TRUE && $attrs['allowused'] === TRUE ? "\$_got = count({$attrs['var']});" . NEWLINE . "if( \$_got < " . (isset($attrs['fillvar']) ? "{$attrs['amount']} - count({$attrs['fillvar']})" : $attrs['amount']) . " )" . NEWLINE . "{" . NEWLINE . "\$this->vars['_temp_galleries'] =& LoadGalleries(\"{$bwnau_query}\", \$this->vars['this_page']['page_id'], \$this->vars['this_page']['category_id'], {$pull_preview});" . NEWLINE . "{$attrs['var']} = ArrayIntermix({$attrs['var']}, \$this->vars['_temp_galleries'], 'end');" . NEWLINE . "}" . NEWLINE : '') . "}" . NEWLINE . "else" . NEWLINE . "{" . NEWLINE . "{$attrs['var']} =& LoadGalleries(\"{$b_query}\", \$this->vars['this_page']['page_id'], \$this->vars['this_page']['category_id'], {$pull_preview});" . NEWLINE . (isset($attrs['fillvar']) ? "}" . NEWLINE : '') . "}" . NEWLINE . E_PHP; } } }