<?php
// ========================
// == tbs_mycontacts3.php == == NOW~ WITH  tbssql for database access
// == =====================

include_once("../php_inc/tbssql_adapter_3.2a.php");    // Instantiate the tbssql class w/ connection info

// ===============  tips from  Skrol29  ===================
if (isset($this)) {
  
//    we are under the TBS Subtemplate Mode =>
  //    variables are always local, not global,
  //    and the TBS object is referenced by variable $this.
  
$TBS =& $this;
  
//$TBS->NoErr = true; 
} else {
  
//    This sub-script can also be run under the normal mode =>
  //  its corresponding template will be displayed like a main template.
  
$TBS = new clsTinyButStrong;
  
//$TBS->NoErr = true; 
}
global 
$TBS;
// ========================================================
// ======  TbsSQL v.3.0 properties to be configured =======

// $Db->Mode = TBSSQL_TRACE + TBSSQL_GRID + TBSSQL_CONSOLE;  // default false

// $Db->DefaultRowType = TBSSQL_OBJECT; // default is: TBSSQL_ARRAY;
// $Db->ConfInfo(); // Show the current configuration of TbsSQL. 
$Db->CacheTimeout 0//mins //TBSSQL_1HOUR; //mins // DEFAULT: no caching // when debugging
//$Db->TempCacheTimeout = 2; //mins //TBSSQL_1HOUR; //mins // DEFAULT: no caching // when debugging 
$Db->CacheAutoClear 60//mins //TBSSQL_1HOUR, TBSSQL_1DAY, or TBSSQL_1WEEK
//$Db->CacheDir = "tbssql_cache";      // Default is: "."

global $Db;
// ========================================================

(isset($debug)) ? $showhint "Y" :  $showhint "N";

$pagetitle=" MyContacts (TbsSQL) Application-in-a-Page";

$case_template "tbs_mycontacts3.html";

switch (
$action) {
case 
"add":

    
$casehint"ANTI SPAM PROTECTED"// ANTI SPAM
        
break; // ANTI SPAM 
        
        //$sql = "select * from mycontacts WHERE id=".$_GET['id']."";
        
$button_text 'Add New';
        
        
$actionmsg "Use this form to add NEW contact - click $button_text";
        
$actionflag "Y";
        
$casehint"action=$action / id= $id / sql= $sql\n";
        
        
$casetemplate="tbs_addcontacts3.html";
        
        
$TBS->LoadTemplate($casetemplate) ;

        
$TBS->Show() ;

break;
case 
"Add New":

    
$casehint"ANTI SPAM PROTECTED"// ANTI SPAM
        
break; // ANTI SPAM 

        
$x_notes=addslashes(strip_tags($x_notes));
        
$x_address=addslashes(strip_tags($x_address));
        
$x_lname=addslashes($x_lname);
        
$x_fname=addslashes($x_fname);

        
$x_fname=ucfirst($x_fname);
        
        
$x_lname=ucfirst($x_lname);
        
$lcap substr($x_lname01); 

        
$sql="INSERT INTO mycontacts (email,url,lettername,lname,fname,ph_home,ph_work,ph_cell,fax,address,notes,password,username) VALUES( '$x_email', '$x_url', '$lcap', '$x_lname',  '$x_fname', '$x_ph_home', '$x_ph_work', '$x_ph_cell', '$x_fax', '$x_address', '$x_notes', '$x_password', '$x_username')";
        
        
$actionflag "Y";
        if(
$Db->Execute($sql)){
        
$actionmsg "Good on ya, mate - added $x_fname $x_lname to database";
        }else{
        
$actionmsg "Ooops: database error! ";
        }    
        
$casehint"action=$action / id= $id / sql= $sql\n";

        break;
case 
"edit":

    
$casehint"ANTI SPAM PROTECTED"// ANTI SPAM
        
break; // ANTI SPAM 
  
        
$sql "select * from mycontacts WHERE id=".$_GET['id']."";
        
$button_text 'Do Edit';
        
$actionmsg "Make changes and click $button_text";
        
$actionflag "Y";

        
$casehint"action=$action / id= $id / sql= $sql\n";
        
        
$casetemplate="tbs_admincontacts3.html";
        
        
$TBS->LoadTemplate($casetemplate) ;
        
// $TBS->MergeBlock('blk1',$db,$sql) ;
        
$result $Db->GetRows($sql);
        
$TBS->MergeBlock('blk1',$result) ;
        
        
$TBS->Show() ;

        break;     
case 
"Do Edit":

    
$casehint"ANTI SPAM PROTECTED"// ANTI SPAM
        
break; // ANTI SPAM 

        
$id $x_id;        
        
$x_notes=addslashes(strip_tags($x_notes));
        
$x_address=addslashes(strip_tags($x_address));
        
$x_lname=addslashes($x_lname);
        
$x_fname=addslashes($x_fname);

        
$x_fname=ucfirst($x_fname);
        
$x_lname=ucfirst($x_lname);
        
$lcap substr($x_lname01); 


    
$sql="UPDATE mycontacts SET email='$x_email', url='$x_url', lettername='$lcap', lname='$x_lname', fname='$x_fname', ph_home='$x_ph_home', ph_work='$x_ph_work', ph_cell='$x_ph_cell', fax='$x_fax', address='$x_address', notes='$x_notes', password='$x_password', username='$x_username' WHERE id='$id'";

        if(
$Db->Execute($sql)){
        
$actionmsg "Good on ya, mate! Contact $x_fname $x_lname is updated.";
        
$actionflag "Y";
        }else{
        
$actionmsg "Database error! ";
        
$actionflag "Y";
        }        

        break;
        
case 
"delete":

    
$casehint"ANTI SPAM PROTECTED"// ANTI SPAM
        
break; // ANTI SPAM 

        
$sql="select * from mycontacts where id=$id";

        
$button_text 'Do Delete';
        
$actionmsg .= "Make sure you're sure about deleting this -then- click $button_text";
        
$actionflag "Y";
        
$casehint"action=$action / id= $id / sql= $sql\n";

        
// $casetemplate="tbs_editcontacts.html";
        
$casetemplate="tbs_admincontacts3.html";

        
$TBS->LoadTemplate($casetemplate) ;
        
$result $Db->GetRow($sql);        
        
$TBS->MergeBlock('blk1',$result) ;
    
        
$TBS->Show() ;

   
//echo "i equals 2";
   
break;
case 
"Do Delete":

    
$casehint"ANTI SPAM PROTECTED"// ANTI SPAM
        
break; // ANTI SPAM 


    
$sql="DELETE FROM mycontacts WHERE id=$x_id";
//      $result = mysql_query($sql);
      
$result $Db->Execute($sql);

    
    
$casetemplate="tbs_mycontacts3.html";
    
//$casetemplate="tbs_admincontacts2.html";
    
$casehint"action=$action / id= $id / sql= $sql\n";
    
$actionmsg="Contact DELETED";
    
$actionflag "Y";

   
//break;
    
//   break;

default:

$casehint"case:DEFAULT: / action=$action / id= $id / sql= $sql\n";
}


// - - - - - - - -    END of CASE stmnt - - - - - - - - - - - 
        
// -with-ezsql- ??? this-tbs-bit-not-needed ???  
// mysql_close($cnx_id) ; // how does ezsql handle this?

if($letterpage){
$sql2="SELECT * FROM mycontacts WHERE lettername='$letterpage' ORDER BY lname";
}else{
$sql2="SELECT * FROM mycontacts ORDER BY lname";
}


$casehint"|| action=$action || id= $id || sql= $sql ||\n";

//$actionmsg .="<br>ZZZ AAA XXX BBB ZZZ";

if( !isset($actionflag) ){
$actionflag "N";
}    


$TBS->LoadTemplate('tbs_mycontacts3.html');

//$TBS->MergeBlock('blk2,blk3',$db,"SELECT DISTINCT lettername from mycontacts ORDER BY lettername");
$sql "SELECT DISTINCT lettername from mycontacts ORDER BY lettername";
$result $Db->GetRows($sql);
$TBS->MergeBlock('blk2,blk3',$result);

$result $Db->GetRows($sql2);
$TBS->MergeBlock('blk1',$result); //  -ezsql- change "$cnx_id" to "$db" in MergeBlock stmts

$TBS->Show();


// =====================  event functions ============================

function f_distinct_blk2($BlockName,&$CurrRec,$RecNum) {
//        if(  $CurrRec['lettername'] != $CurrRec[($RecNum-1)]) {
        
if(  $BlockName[$RecNum] != $BlockName[$RecNum-1]) {
//       $CurrRec['lettername']="(".$CurrRec['lettername'].")";
         
$CurrRec['lettername']="(".$BlockName[$RecNum].")";
         }else{
         
$CurrRec['lettername']=false;
         }
}

//function m_event_blk1($BlockName,&$CurrRec,&$DetailSrc,$RecNum){ // syntax for 'onsection'
function m_event_blk1($BlockName,&$CurrRec,$RecNum){   // syntax for 'ondata'
//$BlockName : name of the block that calls the function (read only)
//$CurrRec   : array that contains columns of the current record (read/write)
//$DetailSrc : source of the current section (read/write)
//$RecNum    : number of the current record (read only)

  
if ($CurrRec['email']) {$CurrRec['email'] = ' <a href="mailto:'.$CurrRec['email'].'"> &lt;email&gt;</a>';
    }else{
    
$CurrRec['email']="";
    }
  if (
$CurrRec['url']) {$CurrRec['url'] = ' <a href="'.$CurrRec['url'].'"> &lt;url&gt;</a>';
    }else{
    
$CurrRec['url']="";
    }
    
// combine above 'email' and 'url' into one param for ease of display
    
if($CurrRec['email']!="" || $CurrRec['url']!=""){ 
    
$CurrRec['url'] = $CurrRec['email'].' '.$CurrRec['url'].'<br>';
    
$CurrRec['email'] = "";
    }
    
  if (
$CurrRec['ph_home']) $CurrRec['ph_home'] = ' Home: '.$CurrRec['ph_home']."<br>";
  if (
$CurrRec['ph_work']) $CurrRec['ph_work'] = ' Work: '.$CurrRec['ph_work']."<br>";
  if (
$CurrRec['ph_cell']) $CurrRec['ph_cell'] = ' Cell: '.$CurrRec['ph_cell'];
if (
$CurrRec['address']) { $CurrRec['address'] = nl2br($CurrRec['address']); }else{$CurrRec['address'] = "&#160;";}
//  if ($CurrRec['address']) $CurrRec['address'] = nl2br($CurrRec['address']);
  
if ($CurrRec['notes']) { $CurrRec['notes'] = nl2br($CurrRec['notes']);  }else{$CurrRec['notes'] = "&#160;";}
//  if ($CurrRec['notes']) $CurrRec['notes'] = "<hr>".$CurrRec['notes'];
//  if ($CurrRec['ph_home']) $CurrRec['ph_home'] = '(H)'.$CurrRec['ph_home'];
//  if ($CurrRec['ph_home']) $CurrRec['ph_home'] = '(H)'.$CurrRec['ph_home'];
}

?>