<?
// Your agreement with TNuG gives you a limited license to access and use this library.
// You may access the library for your own private or business use. 
// You may not reproduce, copy, or redistribute, whole or in part, any of the library that constitutes the TNuG's library functions without written permission.

// Object Template Stripped Version
// Version 0.3h

// Version 0.3h - Changed methods system and added proclayout
//              - removed header/footer, never used
//              - added sorting of results
// Version 0.3g - Update to select optimization, changed text to number field
// Version 0.3f - Update to select optimization, added fieldname
// Version 0.3e - Multiple DB Support by declaration of $SQL_APP_DB
// Version 0.3d - Large record select optimize by full flag in getcommentformfields
// Version 0.3c - fixed typo in edit patch
// Version 0.3b - fixed typo
//              - start of security patch to edit($id)
// Version 0.3a - getcommentformfields added prepend
//              - massing beginnings
//              - cleaned up & simplified dir & field navigate
//              - moved unnessicary examples to addon to reduce filesize
// Version 0.3 - Page_nav fix
//             - Header Var hook to interface
// Version 0.2.9a - templat_query typo
// Version 0.2.9 - Made remove use view
//               - Created specific groups
// Version 0.2.8a - Fixed some case issues
// Version 0.2.8 - Fixed colors again, added cancel buttons
//                - Fixed find, added comment_ prefix to ses vars to allow layering
//		  - Added Search Form in display, Added LookUp on selects
// Version 0.2.7h - Fixed colors and removed display field ordering
//                - cleaned up search header/footer
//		  - fixed sql search
// Version 0.2.7g - add color in trs
// Version 0.2.7f - Fixed lotsa Errors
// Version 0.2.7e - Paging with search is fixed
//                - New Find/Search/PickRec selection integration
//                - REFixed ugly validation bug wif exit; 
// Version 0.2.7d - Speed Ups: change upd to pull 0 instead of $id, 
//                - add cache to getcommentform, allow passing of $row
// Version 0.2.7c - Fixed ugly validation bug wif exit; made searches have no rpp
// Version 0.2.7b - messed with pagenav
// Version 0.2.7a - Added Define loaded and Netscape 4.x hack
//
// Version 0.2.7 - Rearranged PagNav, Add RPP, and fixed some var declarations
//               - Fixed <tr> in display
// Version 0.2.6 - Fixed Next/Prev Error -Neuro.
//		 - Add new paging nav - Neuro.
// Version 0.2.5 - prepended comment to search_header and search_footer - Neuro.
//               - Also changed comment_view to use public group - Neuro.
//		 - removed $id from ins - Neuro.
// 		 - Fixed form cache error - Neuro.
//		 - Smore search syntax - Neuro.
// Version 0.2.4 - Added default HIDNUM for IDs to make methods work - Neuro.

if (defined("ALREADY_LOADED_comment_INTERFACE")) return;
define("ALREADY_LOADED_comment_INTERFACE",1);

// Interface Override
// $finish['method'] = "contact_view();";
// Authwrapper Override - Not working
// $security['method'] = array('users' => "''",'groups' => "''");

// $SQL_APP_DB = '';

$finish['ins']='
include "interfaces/picture.php3";
picture_display();
';

// Header Msgs
$header=array();
$header['new']="New comment";
$header['edit']="Edit comment";
$header['remove']="Remove comment";
$header['browse']="Browse comments";
$header['ins']="Added comment";
$header['upd']="Browse comments";
$header['del']="Browse comments";
$header['search']="Search comments";
$header['search_form']="Search comments";
$header=array();

function getcommentformfields($form,$row,$prepend='',$full=1) {
  global $filename;
  $form->setgroup(array('new','edit','search','display','view','mass'));  
  // Netscape 4.x Bug Hack (ns returns ' ', on <input hidnum name> (no val))
  if (isset($row['commentid'])) {
    $form->addcontrol('HIDNUM',$prepend.'commentid',$row['commentid']);
  }
  // varchar 128 Chars Long
  $form->addcontrol('HTML',$prepend.'',$row['timestamp'],'Timestamp:');
  $form->addcontrol('HIDDEN',$prepend.'timestamp',$row['timestamp']);
  $form->addcontrol('HIDDEN',$prepend.'userid',$row['userid']);
  $form->addcontrol('TEXT',$prepend.'name',$row['name'],'Name:');
  $form->addcontrol('EMAIL',$prepend.'email',$row['email'],'Email:');
  $form->addcontrol('LINK',$prepend.'homepage',$row['homepage'],'Homepage:');
  $form->addcontrol('HTMLEDIT',$prepend.'comment',$row['comment'],'Comment:','',' ROWS=5 COLS=60');
  $form->addcontrol('HIDDEN',$prepend.'pictureid',$row['pictureid']);
  return $form;
}

function commenttopmethods() {
global $filename,$pictureid;
# if (useringroup(array('admin'))) {
/*
echo '[<A 
HREF="'.geturl($filename.'?obj=comment&action=new&pictureid='.$pictureid).'">Add
new comment</A>]';
*/
echo '<br><br>';
# }
}

function getcommentformmethods($form) {
  global $pickrec,$ses;
  $sid=getauthses();
  if (!isset($pickrec)) {
  $pickrec=$ses->get_var($sid,'comment_pickrec');
  } else {
  $ses->set_var($sid,'comment_pickrec',$pickrec);
  }  
#var_dump(getusergroups());
 if (useringroup(array('administrator'))) {  
  $form->addmethod($filename . '?obj=comment&action=edit&id=', 'edit comment', 'commentid',0);
  $form->addmethod($filename . '?obj=comment&action=remove&id=', 'remove comment', 'commentid',0);
 }
  if ($pickrec) {
    $form->addmethod($filename . '?obj=comment&action=return&id=', 'select', 'commentid',3);
  }
  return $form;
}

function getcommentform($commentid,$action,$name,$params="",$cache="") {
  global $conn,$SYND,$filename,$pictureid,$NeuroOekakiUserid;
  if (empty($commentid)) {
    list($row['name'],$row['email'],$row['homepage'])=getnfo($NeuroOekakiUserid);
    # Defaults
    $row['timestamp']=date('Y-m-d H:i:s'); 
    $row['pictureid']=$pictureid;
    $row['userid']=$NeuroOekakiUserid;
  } else {
    if ($cache == "") {
      $sql="select * from comments where commentid=$commentid";
      $result=sqlexec($sql,$conn,0);
      $row=sqlgetrow($result);
    } else {
      $row=$cache;
    }
  }
  $form=new form(geturl("$filename?obj=comment&id=" . $commentid . "&action=" .$action . $params) ,"POST",1,1,1);
  if (empty($action)) { $full=0; } else { $full=1; }
  $form=getcommentformfields($form,$row,'',$full);
  if (!$full) {
    $form=getcommentformmethods($form);
  }
  $form->conn=$conn;
  // End
  $form->addcontrol("SUBMIT","submit",$name);
  return $form;
}

function comment_return($id) {
global $DEBUG_LEVEL,$ses;
    if (($DEBUG_LEVEL & 16) == 16) { 
      $name="formdata_commentid";
    } else {
      $name='commentid';
    }
    $sid=getauthses();
    $ses->clear_var($sid,'comment_pickrec');
?>
<Script Language="JavaScript">
<!--
// Forget everything we are doing
// and close this window.
  window.opener.<? echo $name; ?>_ItemInEdit.value = <? echo $id?>;
  opener.focus();
  window.close();
//-->
</Script>
<?
}

function comment_debug() {
  $form=getcommentform(0,'','');
  $form->debugform();
}

function comment_del($pid="") {
  // Put all relationships here
  global $ses,$auth_sid;
  $id=(int)$ses->get_var($auth_sid,'comment_remove_id');    
  sqlexec("delete from comments where commentid=$id");
  log_transac("comment","remove",$id,"");
}

function comment_new($id=0, $params="") {
  global $pictureid;
  $form=getcommentform(0,'ins','Add comment',$params);
  if ($pictureid) {
    include "interfaces/picture.php3";
    picture_view($pictureid);
  }
  echo $form->generate("edit",array('new'));
#  echo '<FORM ACTION="'. geturl($filename.'?obj=comment&action=display') . '" METHOD="POST">
#        <INPUT TYPE="SUBMIT" VALUE="List comments"></FORM>';  
  echo '<FORM ACTION="'. geturl($filename.'?obj=picture&action=display') . 
'" METHOD="POST"><INPUT TYPE="SUBMIT" VALUE="Cancel"></FORM>';  
}

function comment_upd($pid) {
  global $filename;
  $form=getcommentform(0,'','');
  $check=$form->check_results(array('edit'));
  if (is_array($check)) {
    $ok=1;
    while(list($key,$val)=each($check)) {
            if ($val != "") {  echo $val . ' is not a valid value<br>'; $ok=0; }	    
    }
    if (!$ok) { exit; }
  }
  global $ses,$auth_sid;
  $id=(int)$ses->get_var($auth_sid,'comment_edit_id');    
  $form->update_table("comments","commentid='$id'",array('edit'));
  log_transac("comment","edit",$id,"");
}

function comment_browse() {
  //global $SYND;
  //comment_display(" where syndicateid=$SYND");
  global $ses;
  $sid=getauthses();
  $sql="select * from comments";
  $ses->set_var($sid,'comment_query',$sql);
  comment_display();
}

function comment_pagnav($start,$ttlrow) {
global $filename,$rpp,$action,$id,$obj;
  if ($rpp == 0) { return; }
  if ($ttlrow<$rpp) { return; }  
  $back = $start-$rpp; if ($back < 0) { $back=0; }
  $next = $start+$rpp; 
  $action="display";
#  echo "begin[0]start[$start]ttlrow[$ttlrow]<br>\n";
#  echo "back[$back]next[$next]<br>\n";
  if ($start == 0 ) {
    echo '<center><a href="'.geturl($filename.'?obj='.$obj.'&start='.$next.'&id='.$id.'&action='.$action) . '">Next 25</A> | ';
    echo '<a href="'.geturl($filename.'?obj='.$obj.'&start='.($ttlrow-$rpp).'&id='.$id.'&action='.$action) . '">End</A>';    
  } elseif ( $ttlrow < $next )  {
    echo '<a href="'.geturl($filename.'?obj='.$obj.'&start=0'.'&id='.$id.'&action='.$action) . '">Beginning</A> | ';  
    echo '<a href="'.geturl($filename.'?obj='.$obj.'&start='.$back.'&id='.$id.'&action='.$action) . '">Prev 25</A>';
  } else {
    echo '<a href="'.geturl($filename.'?obj='.$obj.'&start=0'.'&id='.$id.'&action='.$action) . '">Beginning</A> | '; 
    echo '<a href="'.geturl($filename.'?obj='.$obj.'&start=' . $back.'&id='.$id.'&action='.$action) . '">Prev 25</A> | <a href="'.geturl($filename . '?obj='.$obj.'&start=' . $next.'&id='.$id.'&action='.$action).'">Next 25</A> | ';
    echo '<a href="'.geturl($filename.'?obj='.$obj.'&start='.($ttlrow-$rpp).'&id='.$id.'&action='.$action) . '">End</A></center>';    
  }
}

function comment_display($result=0) {
  global $filename,$conn,$start,$rpp,$action,$start,$ses,$sortby;
  $sid=getauthses();
  if (empty($start)) { $start = 0; }
#  echo "result is [$result]<br>\n";
  if ($result==0) { 
    $sql=$ses->get_var($sid,'comment_query');
#    echo "Found Query [$sql] [$sid]<br>\n";
    if (empty($sql)) {
      $sql="select * from comments";
      $ses->set_var($sid,'comment_query',$sql);
    }
    if ($sortby) {
      if (eregi('order by',$sql)) {
        # be careful of limit and order by
	$sql=substr($sql, 0, strpos($sql,'order by')). ' order by $sortby';
      }
    }
    $result=sqlexec($sql,$conn,0);
  }
  if (!isset($pickrec)) { $pickrec=0; }
  $ttlrow=sqlgetnumrows($result);
  comment_pagnav($start,$ttlrow);
#  comment_search_form();
  commenttopmethods();
#  echo '<table border=0><tr>';
#  $form=getcommentform(0,'','');
  # aligns = 1 top/left, 2 center, 3 bot/right
  # showfields($groups,$link='',$align=0,$valign=0,$colors=array('ffffff','eeeeee'))
#  echo $form->showfields(array('display'));
#  echo '</tr>';
  $end = $start+$rpp; if ($start == $end) { $end=$ttlrow; }
$layout='
<p><b><name></b>&nbsp;&nbsp;&nbsp; 
<b><email></b> &nbsp;&nbsp;&nbsp;<b><homepage></b><br>
<comment></p>
';
  for ($count = 0; $count < $start; $count++) { sqlgetrow($result); }
  for ($count = $start; (($count <= $end) && ($row=sqlgetrow($result))); $count++) {
#    if (($count-1)%2) { $color='ffffff'; } else { $color='eeeeee'; }
    $form=new form();
    $form=getcommentformfields($form,$row,'',0);
#    echo "<tr>";
#    echo $form->showdata(array('display'),'','','',0);
#    echo "</tr>";
    echo $form->proclayout($layout,'view',array('display'));
    if (useringroup(array('administrator'))) {
      $form=getcommentformmethods($form);
      echo $form->getmethod('','',0);
    }
  }
#  echo '</table>';
  comment_pagnav($start,$ttlrow);
}

function comment_edit($id,$params="") {
  $form=getcommentform($id,'upd','Update comment',$params);
  global $ses,$auth_sid;
  $ses->set_var($auth_sid,'comment_edit_id',$id);
#  $form->addcontrol('HIDDEN','id',$id);
  echo $form->generate("edit",array('edit'));
# echo $form->proclayout($comment,'edit');  
  echo '<FORM ACTION="'. geturl($filename.'?obj=comment&action=display') . '" METHOD="POST">
        <INPUT TYPE="SUBMIT" VALUE="List comments"></FORM>';
}

function comment_ins() {
  global $REMOTE_ADDR;
  $form=getcommentform(0,'','');
  $check=$form->check_results(array('new'));
  if (is_array($check)) {
    $ok=1;
    while(list($key,$val)=each($check)) {
            if ($val != "") {  echo $val . ' is not a valid value<br>'; $ok=0; }
    }
    if (!$ok) { exit; }
  }
  $form->addcontrol('hidden','ipaddress',$REMOTE_ADDR);
  httpform_set('ipaddress',$REMOTE_ADDR);
  $id=$form->insert_results('comments',array('new'));
  log_transac("comment","inserted",$id,"");
//  echo "<center><h2>Inserted Comment</h2></center>\n";
  return $id;
}

function comment_remove($id, $params='') {
  global $filename;
  global $ses,$auth_sid;
  $ses->set_var($auth_sid,'comment_remove_id',$id);  
  comment_view($id);
  echo '<br><br><center><b>Are you sure you would like to remove this record?<b><br><br>';
  echo '<table><tr><td>';
  echo '<FORM ACTION="'.geturl($filename.'?obj=comment&action=del'.$params) .  '" METHOD="POST">';
  echo '<INPUT type="submit" value="Yes">';
  echo '</form></td><td>';
  echo '<FORM ACTION="'.geturl($filename.'?obj=comment&action=display'.$params).'" METHOD="POST">';
  echo '<INPUT type="submit" value="No">';
  echo '</form>';
  echo '</td></tr></table></center>';
}

function comment_view($id) {
$form=getcommentform($id,'','','');
echo $form->generate("view",array('view'));
# echo $form->proclayout($comment,'view');
echo $form->getmethod('','',0);
}

function comment_find() {
global $ses;
$ses->set_var(getauthses(),'comment_pickrec',1);
comment_search_form();
}

function comment_search_form() {
	$form=getcommentform(0,'search','Search');
#	$form->addcontrol('checkbox','strict',0,'Strict:');
	echo $form->generate("edit",array('search'));
}

function comment_search() {
global $rpp,$strict,$ses;
if (empty($strict)) { $strict=0; }
	$form=getcommentform(0,'','');
	$result=$form->get_search_results('comments',array('search'),'',$strict);
	$oldrpp=$rpp; $rpp=0;
	$sql=getsqlq();
	$ses->set_var(getauthses(),'comment_query',$sql);
	comment_display($result);
	$rpp=$oldrpp;
}

?>
