// v0.1 Initial public release // v0.11 Added PFX to query (thanks mercury) // v0.12 Fixed help to include trailing slashes (thanks vittorio) // v0.13 Unreleased. Added look_in=category and referer // v0.14 Added match_text and use_alt (thanks lukepermsn) // v0.15 Added always_display and limit (thanks lukepermsn) // v0.15a Bugfix for linkcaption (thanks mercury) // v0.16 Tightened image() params for txp v4.0.4 compliance // v0.17 Added thumb, linkprefix and linkalt options // v0.17a bid option now works with existing url params (thanks Papuass) // v0.18 Added imgclass option (thanks DigitalRealm) // v0.2 Major overhaul. Added subcategory support (thanks lukepermsn and santheo). Uses smd_lib. Enhanced match_text/avoid_text so look_in is no longer needed; dropped use_alt and added compare_to; replaced linkcaption/linkalt with link_in; added id_prefix; linkprefix is now link_prefix function smd_random_banner($atts) { require_plugin('smd_lib'); // ** allowable options (default listed first) $compareTo = array("name","id","alt","caption","category"); $linkIn = array("","caption","alt"); // ** plugin options if (is_array($atts)) extract($atts); $category = isset($category) ? (($category=="") ? "%" : doSlash($category)) : "%"; $match_text = isset($match_text) ? $match_text : ""; $avoid_text = isset($avoid_text) ? $avoid_text : ""; $referer = isset($referer) ? $referer : "bid"; $limit = isset($limit) ? $limit: "1"; $imgclass = isset($imgclass) ? $imgclass : ""; $id_prefix = isset($id_prefix) ? $id_prefix : "article-"; $link_prefix = isset($link_prefix) ? $link_prefix : ""; $link_in = (isset($link_in) && in_array($link_in,$linkIn)) ? $link_in : $linkIn[0]; $subcats = isset($subcats) ? true : false; $thumb = isset($thumb) ? true : false; $always_display = isset($always_display) ? true : false; $compareToOpts = array(); if (isset($compare_to)) { $ctList = preg_split('/[,\s]+/',$compare_to,-1,PREG_SPLIT_NO_EMPTY); for ($idx = 0; $idx < count($ctList); $idx++) { if(in_array($ctList[$idx],$compareTo)) { $compareToOpts[] = $ctList[$idx]; } } } if(count($compareToOpts) <= 0) { $compareToOpts[] = $compareTo[0]; } // ** init variables $where = ""; $fields = "id, caption, alt"; $matchArray = array(); $avoidArray = array(); $matchVars = array("?c", "?s", "?t", "?id", "?field"); $whereAND = array(); // ** process match_text if ($match_text != "" ) { // Convert the string into an SQL-style 'in' list. // Process the special ('?something') cases too. $retArray = smd_getAtts($match_text, $matchVars, $id_prefix); $matchArray = $retArray[0]; // Make up the WHERE statement from the combined list of matches. // The categories/image IDs are ORed together in brackets first // before the negation options are tacked on as ANDs afterwards $whereTmp = ""; $whereOR = array(); if (count($matchArray) > 0) { foreach ($compareToOpts as $comp) { $hold = array(); for ($idx = count($matchArray); $idx >=0; $idx--) { if (empty($matchArray[$idx])) { unset($matchArray[$idx]); } else { $hold[] = $comp . " LIKE '%" .$matchArray[$idx]. "%'"; } } $whereTmp = implode(" OR ",$hold); if (trim($whereTmp) != "") { $whereOR[] = $whereTmp; } } } if (count($whereOR) > 0) { $whereAND[] = '(' .implode(" OR ",$whereOR). ')'; } } // ** do the same for avoid_text if ($avoid_text != "" ) { // Convert the string into an SQL-style 'in' list. // Process the special ('?something') cases too. $retArray = smd_getAtts($avoid_text, $matchVars, $id_prefix); $avoidArray = $retArray[0]; // Make up the WHERE statement from the combined list of NOT matches. $whereTmp = ""; $whereOR = array(); if (count($avoidArray) > 0) { foreach ($compareToOpts as $comp) { $hold = array(); for ($idx = count($avoidArray); $idx >=0; $idx--) { if (empty($avoidArray[$idx])) { unset($avoidArray[$idx]); } else { $hold[] = $comp . " LIKE '%" .$avoidArray[$idx]. "%'"; } } $whereTmp = implode(" OR ",$hold); if (trim($whereTmp) != "") { $whereOR[] = $whereTmp; } } } if (count($whereOR) > 0) { $whereAND[] = 'NOT (' .implode(" OR ",$whereOR). ')'; } } // Work out the image categories to search $catsin = array(); $fullCatList = array(); $notCatList = array(); $catVars = array("?c","?id","?s","?t","?field","!c","!id","!s","!t","!field"); $retArray = smd_getAtts($category, $catVars, $id_prefix); $fullCatList = $retArray[0]; $notCatList = $retArray[1]; // included categories $tmpa = array(); for ($idx = 0; $idx < count($fullCatList); $idx++) { if ($subcats) { $categs = smd_getSubCats($fullCatList[$idx],'image'); for ($jdx = 0; $jdx < count($categs); $jdx++) { if ($categs[$jdx]['name'] != "root") { $tmpa[] = "'" .$categs[$jdx]['name']. "'"; } } } else { $tmpa[] = "'" .$fullCatList[$idx]. "'"; } } if (count($tmpa) > 0) { $catsin[] = "category IN (" .implode(",", $tmpa). ")"; } // excluded categories $tmpa = array(); for ($idx = 0; $idx < count($notCatList); $idx++) { if ($subcats) { $categs = smd_getSubCats($notCatList[$idx],'image'); for ($jdx = 0; $jdx < count($categs); $jdx++) { if ($categs[$jdx]['name'] != "root") { $tmpa[] = "'" .$categs[$jdx]['name']. "'"; } } } else { $tmpa[] = "'" .$notCatList[$idx]. "'"; } } if (count($tmpa) > 0) { $catsin[] = "category NOT IN (" .implode(",", $tmpa). ")"; } if (($category == "%") || (count($catsin) <= 0)) { $whereCat = "category LIKE '%'"; } else { $whereCat = implode(" AND ", $catsin); } // ** make up the final WHERE statement $whereOB = " ORDER BY RAND() LIMIT $limit"; $where = $whereCat . ((count($whereAND)>0) ? " AND " .implode(" AND ",$whereAND) : "") . $whereOB; // ** pull out the requested images $rs = safe_rows($fields, 'txp_image', $where); if (!$rs) { if ($always_display) { $rs = safe_rows($fields, 'txp_image', $whereCat . $whereOB); } } // ** display them $staticprefix = "http://"; $outStr = ''; if($rs) { foreach($rs as $row) { $linkName=trim(($link_in=="alt") ? $row['alt'] : (($link_in=="caption") ? $row['caption'] : "")); $linkurl = ''; // ** handle links if (strlen($linkName) > 0) { if (substr($linkName, 0, strlen($staticprefix)) === $staticprefix) { $linkType = "FULL"; } else if ($linkName[0] == '/') { if ($link_prefix == '') { $linkType = "ROOT_NO_PREFIX"; } else if (substr($link_prefix, 0, strlen($staticprefix)) === $staticprefix) { $linkType = "FULL_PREFIX"; } else if ($link_prefix[0] == '/') { $linkType = "ROOT"; } else { $linkType = "RELATIVE"; } } else if ($link_prefix == '') { $linkType = "RELATIVE"; } else if ($link_prefix[0] == '/') { $linkType = "ROOT"; } else if (substr($link_prefix, 0, strlen($staticprefix)) === $staticprefix) { $linkType = "FULL_PREFIX"; } else { $linkType = "RELATIVE"; } switch ($linkType) { case "FULL": $linkurl = $linkName; break; case "ROOT_NO_PREFIX": $linkurl = site_url($atts) . substr($linkName,1); break; case "ROOT": $linkurl = site_url($atts) . substr($link_prefix,1) . $linkName; break; case "RELATIVE": default: $linkurl = $link_prefix . $linkName; break; } } // ** throw image out to the page $imgOptions = array("id" => $row['id']); if ($imgclass != '') { $imgOptions["class"] = $imgclass; } if (($link_in != "") && (strlen($linkurl) > 0)) { if ($referer != "") { $refertxt = smd_addQSVar($linkurl, $referer, $row['id']); } else { $refertxt = $linkurl; } $outStr .= '' . (($thumb) ? thumbnail($imgOptions) : image($imgOptions)). ''; } else { $outStr .= (($thumb) ? thumbnail($imgOptions) : image($imgOptions)); } } } else { return; } return $outStr; }