// v0.1 Initial public release // v0.11 Added !category support // v0.12 Added textpos option (thanks lee) and imageid option (thanks wouaren) // v0.2 js and css can now be loaded elsewhere (thanks jstubbs). Added wraptag and changed defaults to make output less like tag-soup (thanks wouaren) // Use this to include the libraries in the section of the page function smd_slimbox_inc($atts) { global $slimbox_libs; if (is_array($atts)) extract($atts); // Function options $jsdir = isset($jsdir) ? doSlash($jsdir) : ''; $cssdir = isset($cssdir) ? doSlash($cssdir) : ''; $scripts = isset($scripts) ? $scripts : ''; $skip = isset($skip) ? $skip : "0"; $modsRequired = 3; $modsLoaded = 0; $outStr = ''; if ($skip > 0) { $modsLoaded = $skip; } // Include the js libs if required if ($jsdir != '') { $outStr .= ''.n; $outStr .= ''.n; $modsLoaded += 2; } // Find which scripts to auto-load if (($scripts != '') && ($modsLoaded < $modsRequired)) { $modsList = preg_split('/[,]+/',$scripts,$modsRequired); $jsprefix = "'.n; } else { $outStr .= ''.n; } } $modsLoaded++; } } // Include the css file if required if ($cssdir != '') { $outStr .= ''.n; $modsLoaded++; } // Set the flag to say the libs are ok. Shame there's no easy provision for // checking the files actually exist at the given locations, but hey if ($modsLoaded == $modsRequired) { $slimbox_libs = true; } else { $slimbox_libs = false; $outStr = 'Slimbox: if using the scripts option, specify up to three required scripts in this order: <txp:smd_slimbox_inc scripts="mootoolsJSLocation, slimboxJSLocation, slimboxCSSLocation" />. If any positions are left empty, the missing scripts must be supplied via the jsdir, cssdir or loaded externally and told to skip this step. The three values can either be a full XHTML string (e.g. as returned by txp:stm_javascript or txp:css, although they may not work directly within the tag; see the docs) or simply paths to the files. Note that no validation is performed to check if the files exist.'.n; } return $outStr; } function smd_slimbox($atts) { global $pretext, $thisarticle, $img_dir; global $slimbox_libs; $maxlimit = 99999; if (is_array($atts)) extract($atts); // Plugin options $imageid = isset($imageid) ? doSlash($imageid) : ''; $category = isset($category) ? doSlash($category) : (($imageid=="") ? $pretext['c']: ''); $catlabel = isset($catlabel) ? $catlabel: "article-"; $groupname = isset($groupname) ? $groupname: "smd"; $limit = isset($limit) ? $limit: "0"; $orderby = isset($orderby) ? $orderby: "category"; $showpagelinks = isset($showpagelinks) ? $showpagelinks: "1"; $prevlabel= isset($prevlabel) ? $prevlabel: "Prev"; $nextlabel= isset($nextlabel) ? $nextlabel: "Next"; $showcaption = isset($showcaption) ? $showcaption: "0"; $showalt = isset($showalt) ? $showalt: "0"; $textpos = isset($textpos) ? $textpos: "below"; $wraptag = isset($wraptag) ? $wraptag: ""; $cellclass = isset($cellclass) ? $cellclass: ""; $navclass = isset($navclass) ? $navclass: ""; $prevclass = isset($prevclass) ? $prevclass: "smd_slimbox_prev"; $nextclass = isset($nextclass) ? $nextclass: "smd_slimbox_next"; $captionclass = isset($captionclass) ? $captionclass: ""; $altclass = isset($altclass) ? $altclass: ""; $fixedOrder = false; // Main code body if ($slimbox_libs == false) { $outStr = 'Slimbox: Required libraries not loaded. Call <txp:smd_slimbox_inc /> in the <head> of this page.'; } else { // The database fields required $fields = 'id,name,caption,alt,category,ext'; $subCatList = array(); $fullCatList = array(); $notCatList = array(); // Convert the category (possibly a comma-sep list) into an SQL-style 'in' list. // Process the special ('?something') cases too. $catList = preg_split('/[,\s]+/',$category,-1,PREG_SPLIT_NO_EMPTY); for ($idx = 0; $idx < count($catList); $idx++) { $catname = $catList[$idx]; if ($catname === "?c") { // Use the global article category, if it exists if ($pretext['c'] != "") { if (!in_array($pretext['c'], $fullCatList)) { $fullCatList[] = $pretext['c']; } } } else if ($catname === "?id") { // Use the global article ID if (!in_array($catlabel . $pretext['id'], $fullCatList)) { $fullCatList[] = $catlabel . $pretext['id']; } } else if ($catname[0] === "?") { // Use the given field name; which may be a comma-separated sublist. // Split off the field name from the question mark $fieldname = substr($catname,1); if (isset($thisarticle[$fieldname])) { $fieldContents = $thisarticle[$fieldname]; } else { $fieldContents = ''; } if (!empty($fieldContents)) { $subCatList = preg_split('/[,\s]+/', $fieldContents, -1, PREG_SPLIT_NO_EMPTY); foreach ($subCatList as $subcatname) { if (!in_array($subcatname, $fullCatList)) { $fullCatList[] = $subcatname; } } } } else if ($catname[0] === "!") { // May either be a field name (and maybe another CSL) or a fixed term. // Split off the name from the exclamation mark $fieldname = substr($catname,1); if (isset($thisarticle[$fieldname])) { $fieldContents = $thisarticle[$fieldname]; } else { $fieldContents = $fieldname; } if (!empty($fieldContents)) { $subCatList = preg_split('/[,\s]+/', $fieldContents, -1, PREG_SPLIT_NO_EMPTY); foreach ($subCatList as $subcatname) { if (!in_array($subcatname, $notCatList)) { $notCatList[] = $subcatname; } } } } else { if (!in_array($catname, $fullCatList)) { $fullCatList[] = $catname; } } } $subIDList = array(); $fullIDList = array(); $notIDList = array(); // Convert the imageid (possibly a comma-sep list) into an SQL-style 'in' list. // Process the special ('?something') cases too. $idList = preg_split('/[,\s]+/',$imageid,-1,PREG_SPLIT_NO_EMPTY); for ($idx = 0; $idx < count($idList); $idx++) { $idnum = $idList[$idx]; if ($idnum === "?id") { // Use the global article ID if (!in_array($pretext['id'], $fullIDList)) { $fullIDList[] = $pretext['id']; } } else if ($idnum[0] === "?") { // Use the given field name; which may be a comma-separated sublist. // Split off the field name from the question mark $fieldname = substr($idnum,1); if (isset($thisarticle[$fieldname])) { $fieldContents = $thisarticle[$fieldname]; } else { $fieldContents = ''; } if (!empty($fieldContents)) { $subIDList = preg_split('/[,\s]+/', $fieldContents, -1, PREG_SPLIT_NO_EMPTY); foreach ($subIDList as $subidnum) { if (!in_array($subidnum, $fullIDList)) { $fullIDList[] = $subidnum; } } } } else if ($idnum[0] === "!") { // May either be a field name (and maybe another CSL) or a fixed term. // Split off the name from the exclamation mark $fieldname = substr($idnum,1); if (isset($thisarticle[$fieldname])) { $fieldContents = $thisarticle[$fieldname]; } else { $fieldContents = $fieldname; } if (!empty($fieldContents)) { $subIDList = preg_split('/[,\s]+/', $fieldContents, -1, PREG_SPLIT_NO_EMPTY); foreach ($subIDList as $subidnum) { if (!in_array($subidnum, $notIDList)) { $notIDList[] = $subidnum; } } } } else { if (!in_array($idnum, $fullIDList)) { $fullIDList[] = $idnum; } } } // Make up the WHERE statement from the combined list of categories. // The categories/image IDs are ORed together in brackets first // before the negation options are tacked on as ANDs afterwards $whereTmp = ''; if (count($fullCatList) > 0) { $whereTmp = 'category in ('; for ($idx = count($fullCatList); $idx >=0; $idx--) { if (empty($fullCatList[$idx])) { unset($fullCatList[$idx]); } else { $fullCatList[$idx] = "'" . $fullCatList[$idx] . "'"; } } $whereTmp .= implode(",",$fullCatList); $whereTmp .= ')'; } if (trim($whereTmp) != "") { $whereOR[] = $whereTmp; } // Add to the WHERE statement the combined list of image IDs $whereTmp = ''; if (count($fullIDList) > 0) { $whereTmp = 'id in ('; for ($idx = count($fullIDList); $idx >=0; $idx--) { if (empty($fullIDList[$idx])) { unset($fullIDList[$idx]); } else { $fullIDList[$idx] = "'" . $fullIDList[$idx] . "'"; } } $whereTmp .= implode(",",$fullIDList); $whereTmp .= ')'; } if (trim($whereTmp) != "") { $whereOR[] = $whereTmp; } $where = '(' .implode(" OR ",$whereOR). ')'; if (trim($where) == "()") { $where = '1=1'; } // Add to the WHERE statement the combined list of NOT categories if (count($notCatList) > 0) { $where .= ' and category not in ('; for ($idx = count($notCatList); $idx >=0; $idx--) { if (empty($notCatList[$idx])) { unset($notCatList[$idx]); } else { $notCatList[$idx] = "'" . $notCatList[$idx] . "'"; } } $where .= implode(",",$notCatList); $where .= ')'; } // Add to the WHERE statement the combined list of NOT image IDs if (count($notIDList) > 0) { $where .= ' and id not in ('; for ($idx = count($notIDList); $idx >=0; $idx--) { if (empty($notIDList[$idx])) { unset($notIDList[$idx]); } else { $notIDList[$idx] = "'" . $notIDList[$idx] . "'"; } } $where .= implode(",",$notIDList); $where .= ')'; } // Convert the orderby into a valid ORDER BY clause $whereOB = Array(); $obList = preg_split('/[,\s]+/',$orderby,-1,PREG_SPLIT_NO_EMPTY); for ($idx = 0; $idx < count($obList); $idx++) { $ob_dir = explode(':',$obList[$idx]); $direction = ''; if (count($ob_dir) > 1) { if($ob_dir[1] == "desc") { $direction = ' desc'; } else { $direction = ' asc'; } } switch ($ob_dir[0]) { case 'name': $whereOB[] = 'name'.$direction; break; case 'id': $whereOB[] = 'id'.$direction; break; case 'alt': $whereOB[] = 'alt'.$direction; break; case 'caption': $whereOB[] = 'caption'.$direction; break; case 'date': $whereOB[] = 'date'.$direction; break; case 'random': $whereOB[] = 'rand()'; break; case 'fixed': // 'fixed' only works on image ids if ($imageid != "") { $fixedOrder = true; } break; case 'category': default: $whereOB[] = 'category'.$direction; break; } } if (count($whereOB) > 0) { $where .= ' ORDER BY ' . implode(",",$whereOB); } // Handle paging if (($limit > 0) && $showpagelinks) { $rs = safe_rows($fields, 'txp_image', $where .' LIMIT 0,'.$maxlimit); $numthum = count($rs); $numPages = ($numthum > 0) ? ceil($numthum/$limit) : 1; $thumbpage = (!gps('tpg')) ? 1 : gps('tpg'); $offset = ($thumbpage - 1) * $limit; $rs = safe_rows($fields, 'txp_image', $where .' LIMIT '.$offset.','.$limit); } else { // No paging required $numPages = 1; $thumbpage = 1; $rs = safe_rows($fields, 'txp_image', $where .' LIMIT 0,'. (($limit==0) ? $maxlimit : $limit)); } // Is fixed-order output desired? If so, re-order the $rs if ($fixedOrder) { $orderedRS = Array(); for ($idx = 0; $idx < count($fullIDList); $idx++) { foreach($rs as $row) { if ("'".$row['id']."'" == $fullIDList[$idx]) { $orderedRS[] = $row; break; } } } $rs = $orderedRS; } // Construct the output $outStr = ''; if($rs) { foreach($rs as $row) { $thumb = hu.$img_dir.'/' . $row['id'] . 't' . $row['ext']; $full = hu.$img_dir.'/' . $row['id'] . $row['ext']; if ($groupname === "?c") { $grp = $row['category']; } else if ($groupname[0] === "?") { // Split off the field name from the question mark $fieldname = substr($groupname,1); if (isset($thisarticle[$fieldname])) { $grp = $thisarticle[$fieldname]; } else { $grp = ''; } } else { $grp = $groupname; } // Generate the caption/alt if required $capStr = ($showalt && $row['alt']) ? '' .$row['alt']. '' : ''; $capStr .= ($showcaption && $row['caption']) ? '' .$row['caption']. '' : ''; // Construct the link and put caption/alt in the appropriate position $outStr .= ($wraptag != "") ? '<' .$wraptag. (($cellclass != "") ? ' class="' .$cellclass. '"' : '') . '>' : ''; $outStr .= ($textpos == "above") ? $capStr : ''; $outStr .= '' .(($textpos == "before") ? $capStr : ''). '' .$row['alt']. '' .(($textpos == "after") ? $capStr : ''). ''.n; $outStr .= ($textpos == "below") ? $capStr : ''; $outStr .= ($wraptag != "") ? '' : ''; $outStr .= n; } // Add the paging features if required if (($limit > 0) && $showpagelinks) { $next = ($numPages > 1 && $thumbpage != $numPages); $prev = ($numPages > 1 && $thumbpage > 1); // Replace any paging info in the query string $prevPage = addQuerystringVar($pretext['req'], 'tpg', $thumbpage-1); $nextPage = addQuerystringVar($pretext['req'], 'tpg', $thumbpage+1); $outStr .= ($wraptag != "") ? '<' .$wraptag. (($navclass != "") ? ' class="' .$navclass. '"' : '') . '>' : ''; $outStr .= ($prev) ? '' .$prevlabel. ''.n : ''; $outStr .= ($next) ? '' .$nextlabel. ''.n : ''; $outStr .= ($wraptag != "") ? '' : ''; $outStr .= n; } } } return $outStr; } function addQuerystringVar($url, $key, $value) { $url = removeQuerystringVar($url, $key); if (strpos($url, '?') === false) { return ($url . '?' . $key . '=' . $value); } else { return ($url . '&' . $key . '=' . $value); } } function removeQuerystringVar($url, $key) { $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); $url = substr($url, 0, -1); return ($url); }