<?php if (!defined('PmWiki')) exit();
/* Rating system 
Display a form from https://www.rating-system.com/
+
  Copyright 2015 Simon Davis
  This text is written for PmWiki; you can redistribute it and/or modify it 
  under the terms of the GNU General Public License as published by the 
  Free Software Foundation; either version 3 of the License, or (at your option)
  any later version. See pmwiki.php for full details and lack of warranty.
+
# 2015-09-12 initial version
*/
# Version date
$RecipeInfo['RatingSystem']['Version'] = '2015-09-12';
$FmtPV['$RatingSystemVersion'] = "'RatingSystem version {$RecipeInfo['RatingSystem']['Version']}'"; // return version as a custom page variable

SDV($RatingSystemWidth, '100%'); # set default div width
SDV($RatingSystemHeight, '100%'); # set default div height
SDV($RatingSystemCompanyId, 0000); # set default CompanyId
SDV($RatingSystemUserId, '1'); # set default UserId
SDV($RatingSystemProductCode, 'window.location.href'); # set default ProductCode
SDV($RatingSystemSearchText, ''); # set default PageIndex
SDV($RatingSystemSortExpression, ''); # set default PageIndex
SDV($RatingSystemPageIndex, 1); # set default PageIndex
SDV($RatingSystemMaxpageDisplay, 10); # set default MaxpageDisplay
SDV($RatingSystemRSPage, 'rsbox'); # set default r-rspage
SDV($RatingSystemNewwin, 1); # set default new window setting
SDV($RatingSystemDebug, false); # set default debug setting

# declare $RatingSystem for (:if enabled RatingSystem:) recipe installation check
global $RatingSystem; $RatingSystem = 1;
# set debug flag
  $debugon = (bool) $RatingSystemDebug; # if on writes input and output to web page

## Add a PmWiki custom markup 
# (:ratingsystem width= height= float= clear= companyid= userid= pageindex= maxpagedisplay= rspage= :)
  $vlength      = '(?:0|\d{1,4}(?:\.\d{1,2})?(?:%|[a-z]{1,5}))'; # see http://www.w3.org/TR/css3-values/#lengths
  $vid          = '\d{1,10}'; # 
  $vpageindex   = '\d{1,2}'; # 
  $vmaxpagedisplay = '\d{1,2}'; # 
  $vuserid      = '\d{1,10}'; # 
  $vtext        = '\w{1,255}'; #
  $vrspage      = '\w{1,12}'; #
## parameters
  $pwidth       = 'width='  . $vlength;
  $pheight      = 'height=' . $vlength;
  $pcompanyid   = 'companyid=' . $vid;
  $pratingboxid = 'ratingboxid=' . $vid;
  $psearchtext  = 'searchtext=' . $vtext;
  $psortexpression  = 'sortexpression=' . $vtext;
  $pageindex    = 'pageindex=' . $vid;
  $pmaxpagedisplay = 'maxpagedisplay=' . $vmaxpagedisplay;
  $puserid      = 'userid=' . $vuserid;
  $prspage      = 'rspage=(?:rsbox|rsratingbox|rsaverage|rsratereviewbox|rsreviewbox|rsqanda)';
#   float= -- left, right
  $pfloat       = 'float=(?:left|right)';
#   clear= -- left, right, both
  $pclear       = 'clear=(?:left|right|both)';
## 
  $qparms  = '(' . $pcompanyid . 
       ')|(' . $pratingboxid .
       ')|(' . $psearchtext .
       ')|(' . $psortexpression .
       ')|(' . $ppageindex .
       ')|(' . $pmaxpagedisplay .
       ')|(' . $puserid .
       ')|(' . $prspage .
       ')|(' . $pheight .
       ')|(' . $pwidth .
       ')|(' . $pfloat .
       ')|(' . $pclear .
       ')';
##
Markup_e( 'ratingsystem', # an internal PmWiki function that defines the custom markup for the wiki (see http://www.pmwiki.org/wiki/PmWiki/CustomMarkup)
  'directives',
  "/\\(:ratingsystem (?:(?:" . $qparms . ")\s*)+:\\)/i", # 
  "Keep(RatingSystem_Parse(\$m[1], \$m[2], \$m[3], \$m[4], \$m[5], \$m[6], \$m[7], \$m[8], \$m[9]))" );
# s = dot matches all chars including newline
# i = case insensitive
# m = multiline
# uses lazy evaluation, preserves leading and trailing white space
# Keep prevents PmWiki markup being applied
#
/** Main RatingSystem parser
 *   /param   arguments as documented above
 *   /return  The HTML-formatted RatingSystem markup wrapped in a <div> of class "ratingsystem", see http://rating-system.com/.
 */
function RatingSystem_Parse($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8, $p9) {
#
global $RatingSystemNewwin, $RatingSystemWidth, $RatingSystemHeight; # configuration variables from configuration file, e.g. config.php
global $RatingSystemCompanyId, $RatingSystemUserId, $RatingSystemRatingBoxId;
global $RatingSystemProductCode, $RatingSystemSearchText, $RatingSystemSortExpression;
global $RatingSystemPageIndex, $RatingSystemMaxpageDisplay, $RatingSystemRSPage;
global $debugon; # for debug
// Initialise variables
  $retval    = ''; # return value
  $debugval  = ''; #return debug information
  $opt       = ParseArgs($p1 . ' ' . $p2 . ' ' . $p3 . ' ' . $p4 . ' ' . $p5 . ' ' . $p6 . ' ' . $p7 . ' ' . $p8 . ' ' . $p9); # see pmWiki documentation
  if ( $debugon ) { $debugval .= $p1 . ' ' . $p2 . ' ' . $p3 . ' ' . $p4 . ' ' . $p5 . ' ' . $p6 . ' ' . $p7 . ' ' . $p8 . ' ' . $p9;};
  $newwin    = $RatingSystemNewwin; # open map in new window
  //
  $width     = (bool) $opt['width']  ? 'width:'  . $opt['width'] . ';' : $RatingSystemWidth . ';'; # parameter
  $height    = (bool) $opt['height'] ? 'height:' . $opt['height'] . ';' : $RatingSystemHeight . ';'; # parameter
  $float     = (bool) $opt['float']  ? 'float:'  . $opt['float'] . ';' : ''; # div parameter
  $clear     = (bool) $opt['clear']  ? 'clear:'  . $opt['clear'] . ';' : ''; # div parameter
  //
  $companyid   = (bool) $opt['companyid']     ? $opt['companyid'] : $RatingSystemCompanyId; # parameter
  $userid      = (bool) $opt['userid']        ? $opt['userid'] : $RatingSystemUserId; # parameter
  $ratingboxid = (bool) $opt['ratingboxid']   ? $opt['ratingboxid'] : $RatingSystemRatingBoxId; # parameter
  $productcode = (bool) $opt['productcode']   ? $opt['productcode'] : $RatingSystemProductCode; # parameter
  $searchtext  = (bool) $opt['searchtext']    ? $opt['searchtext'] : $RatingSystemSearchText; # parameter
  $sortexpression = (bool) $opt['sortexpression'] ? $opt['sortexpression'] : $RatingSystemSortExpression; # parameter
  $pageindex   = (bool) $opt['pageindex']     ? $opt['pageindex'] : $RatingSystemPageIndex; # parameter
  $maxpagedisplay = (bool) $opt['maxpagedisplay'] ? $opt['maxpagedisplay'] : $RatingSystemMaxpageDisplay; # parameter
  $rspage = (bool) $opt['rspage'] ? $opt['rspage'] : $RatingSystemRSPage; # parameter
  //
  $retval .= <<<RETVALEND
<div style="overflow:auto;$width$height$float$clear" class='ratingsystem'>
<script type="text/javascript">
var r_obj = {
"Company": { "CompanyId": $companyid },
"RatingboxId": $ratingboxid,
"ProductCode": 'encodeURIComponent($productcode)',
"SearchText": "",
"SortExpression": "",
"PageIndex": $pageindex,
"MaxpageDisplay": $maxpagedisplay,
"User": { "UserId": "$userid" }
};
var r_rspage = "$rspage";
</script>
<script type="text/javascript" src="http://www.rating-system.com/widget/rsiframe.js"></script>
</div>
<div style='font-size:8px' class='ratingsystem-com'>Powered by <a href='http://www.rating-system.com' target='_blank' title='Ratings and Reviews are powered by Rating-System.com'>Rating-System.com</a></div><!-- DO NOT REMOVE THE LAST LINE, please contact us first if you need to do it -->
RETVALEND;
  if ( $debugon ) { $debugval = '<br><span<div style="font-size:8px">debug:' . $debugval . "</span>\n";};
  return $retval . $debugval;
} # RatingSystem_Parse