Help:Sandbox/diffs

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< Help:Sandbox
Revision as of 16:40, 22 May 2016 by Woozle (talk | contribs) (cust base classes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

<php> class clsCusts extends clsTable_key_single {

   const TableName='core_custs';
   // ++ SETUP ++ //
   public function __construct($iDB) {

parent::__construct($iDB); $this->Name(self::TableName); $this->KeyName('ID'); $this->ClassSng('clsCust');

   }
   // -- SETUP -- //
   // ++ CLASS NAMES ++ //
   protected function NamesClass() {

return 'clsCustNames';

   }
   protected function AddrsClass() {

return 'clsCustAddrs';

   }
   // -- CLASS NAMES -- //
   // ++ DATA TABLE ACCESS ++ //
   protected function NameTable() {

return $this->Engine()->Make($this->NamesClass());

   }
   protected function AddrTable() {

return $this->Engine()->Make($this->AddrsClass());

   }
   // -- DATA RECORDS ACCESS -- //
   public function Recs_forUser($idUser) {

$rs = $this->GetData('(ID_Repl IS NULL) AND (ID_User='.$idUser.')'); return $rs;

   }
   // -- DATA RECORDS ACCESS -- //
   // ++ ACTIONS ++ //
   /*----
     ACTION: Creates records for customer, name, and address --

everything needed to record a new customer.

   */
   public function CreateCustomer($idUser,vcCartData_NameAddress $oPerson) {

$id = $this->CreateRecord($idUser);

       // create Name record

$tNames = $this->NameTable(); $sName = $oPerson->GetNameFieldValue(); $idName = $tNames->CreateRecord($id,$sName);

// create Address record $tAddrs = $this->AddrTable(); $idAddr = $tAddrs->CreateRecord($id,$oPerson);

// fill in name & address IDs in Cart record (do we stil store those?) if (!is_null($idName) && !is_null($idAddr)) { $rcCust = $this->GetItem($id); $rcCust->FinishRecord($idName,$idAddr); return TRUE; } else { return FALSE; }

   }
   /* 2016-05-08 old version
   public function CreateCustomer($idUser,$sName,clsPerson $oPerson) {

$id = $this->CreateRecord($idUser);

// create Name record $tNames = $this->NameTable(); $idName = $tNames->CreateRecord($id,$sName);

// create Address record $tAddrs = $this->AddrTable(); $idAddr = $tAddrs->CreateRecord($id,$oPerson);

if (!is_null($idName) && !is_null($idAddr)) { $rcCust = $this->GetItem($id); $rcCust->FinishRecord($idName,$idAddr); return TRUE; } else { return FALSE; }

   }//*/
   public function CreateRecord($idUser) {

$arUpd = array( 'ID_User' => $idUser, 'WhenCreated' => 'NOW()', ); $ok = $this->Insert($arUpd); if ($ok) { return $this->Engine()->NewID(); } else { return NULL; }

   }
   // -- ACTIONS -- //

} class clsCust extends clsVbzRecs {

   // ++ FIELD CALCULATIONS ++ //
   protected function SingleLine() {

$oName = $this->NameRecord(); $oAddr = $this->AddrObj(); $txt = $oName->ShortDescr(); if (empty($oAddr)) { $txt .= ' (no address, cust ID='.$this->KeyValue().')'; } else { $txt .= ' - '.$oAddr->ShortDescr(); } $ht = htmlspecialchars($txt); return $ht;

   }
   // -- FIELD CALCULATIONS -- //
   // ++ CLASS NAMES ++ //
   protected function MailAddrsClass() {

return 'clsCustAddrs';

   }
   protected function PhonesClass() {

return 'clsCustPhones';

   }
   protected function CardsClass() {

return 'clsCustCards';

   }
   // -- CLASS NAMES ++ //
   // ++ DATA TABLE ACCESS ++ //
   protected function MailAddrTable($id=NULL) {

return $this->Engine()->Make($this->MailAddrsClass(),$id);

   }
   protected function PhoneTable($id=NULL) {

return $this->Engine()->Make($this->PhonesClass(),$id);

   }
   protected function CardTable($id=NULL) {

return $this->Engine()->Make($this->CardsClass(),$id);

   }
   // -- DATA TABLE ACCESS -- //
   // ++ DATA RECORDS ACCESS ++ //
   /*----
     RETURNS: recordset of aliases for this customer ID
     HISTORY:

2012-01-09 created for admin page

   */
   protected function Aliases_rs() {

$id = $this->KeyValue(); $tbl = $this->Table(); $rs = $tbl->GetData('ID_Repl='.$id,NULL,'ID'); return $rs;

   }
   /*----
     RETURNS: recordset of Names for this Customer
     HISTORY:

2012-01-08 split off from AdminNames

   */
   public function Names() {

$tbl = $this->Engine()->CustNames(); $rs = $tbl->GetData('ID_Cust='.$this->KeyValue()); return $rs;

   }
   /*----
     RETURNS: recordset of default name for this customer
     HISTORY:

2013-11-09 Created for user-based checkout.

   */
   public function NameRecord() {

$id = $this->Value('ID_Name'); if (is_null($id)) { $rc = NULL; } else { $rc = $this->Engine()->CustNames($id); } return $rc;

   }
   /*----
     RETURNS: recordset of all Addresses for this Customer, optionally filtered
     HISTORY:

2012-01-08 split off from AdminAddrs 2013-11-08 Added optional $iFilt parameter. 2013-11-09 Moved most of the code to clsCusts::Recs_forCust()

   */
   public function Addrs($iFilt=NULL) {

throw new exception('Addrs() is deprecated; call AddrRecords().'); $tbl = $this->MailAddrTable(); $id = $this->KeyValue(); $rc = $tbl->Recs_forCust($id,$iFilt); return $rc;

   }
   protected function AddrRecords($doVoided) {

$tbl = $this->MailAddrTable(); $id = $this->KeyValue(); $sqlFilt = NULL; if (!$doVoided) { $sqlFilt = 'WhenVoid IS NULL'; } $rc = $tbl->Recs_forCust($id,$sqlFilt); return $rc;

   }
   /*----
     RETURNS: recordset of only active Addresses for this Customer
     HISTORY:

2013-11-08 Created for user-based checkout.

   */
   public function AddrsActive() {

return $this->Addrs('(WhenVoid IS NULL) AND NOT (WhenExp < NOW())');

   }
   /*----
     RETURNS: recordset of default address for this customer

If ID_Addr is not set, returns first active record found. If there are no active records, returns NULL.

     HISTORY:

2013-11-09 Added check for ID_Addr = NULL.

   */
   public function AddrObj() {

$id = $this->Value('ID_Addr'); if (is_null($id)) { $rc = $this->AddrsActive(); if ($rc->RowCount == 0) { $rc = NULL; } else { $rc->NextRow(); // load the first record } } else { $rc = $this->Engine()->CustAddrs($id); } return $rc;

   }
   /*----
     RETURNS: recordset of Emails for this Customer
     HISTORY:

2012-01-08 split off from AdminEmails

   */
   public function Emails() {

throw new exception('Emails() is deprecated; call EmailRecords().'); $tbl = $this->objDB->CustEmails(); $rs = $tbl->GetData('ID_Cust='.$this->KeyValue()); return $rs;

   }
   protected function EmailAddrRecords() {

$tbl = $this->EmailAddrTable(); $rs = $tbl->GetData('ID_Cust='.$this->KeyValue()); return $rs;

   }
   /*----
     RETURNS: recordset of Phones for this Customer
     HISTORY:

2012-01-08 split off from AdminPhones

   */
   public function Phones() {

$tbl = $this->PhoneTable(); $rs = $tbl->GetData('ID_Cust='.$this->KeyValue()); return $rs;

   }
   /*----
     RETURNS: recordset of Cards for this Customer
     HISTORY:

2012-01-08 split off from AdminCards

   */
   public function Cards() {

throw new exception('Cards() is deprecated; call CardRecords().');

   }
   public function CardRecords() {

$tbl = $this->CardTable(); $rs = $tbl->GetData('ID_Cust='.$this->KeyValue()); return $rs;

   }
   // -- DATA RECORD ACCESS -- //
   // ++ DATA RECORD ARRAYS ++ //


   // TODO: Each of these table-types needs a GetData_active() method, and we should be using that instead.
   /*----
     RETURNS: recordset of Names for all records in the current set
   */
   public function NameRecords_forRows() {

$sqlIDs = $this->KeyListSQL(); return $this->NameTable()->GetData("isActive AND (ID_Cust IN ($sqlIDs))");

   }
   public function AddrRecords_forRows() {

$sqlIDs = $this->KeyListSQL(); return $this->MailAddrTable()->GetData("(WhenVoid IS NULL) AND (NOW() <= IFNULL(WhenExp,NOW())) AND (ID_Cust IN ($sqlIDs))");

   }
   public function CardRecords_forRows() {

$sqlIDs = $this->KeyListSQL(); return $this->CardTable()->GetData("isActive AND (NOW() <= IFNULL(WhenInvalid,NOW())) AND (ID_Cust IN ($sqlIDs))");

   }
   public function AsArray($doNames,$doAddrs,$doCards) {

$ar = NULL; while ($this->NextRow()) { $idCust = $this->KeyValue(); $qRows = 0;

if ($doNames) { $rs = $this->Names(); // get names for this customer while ($rs->NextRow()) { $qRows++; $idName = $rs->KeyValue(); $sName = $rs->ShortDescr(); $ar[$idCust]['names'][$idName] = $sName; } }

if ($doAddrs) { $rs = $this->AddrRecords(FALSE); // get addresses for this customer while ($rs->NextRow()) { $qRows++; $idAddr = $rs->KeyValue(); $ht = htmlspecialchars($rs->AsSingleLine()); $ar[$idCust]['addrs'][$idAddr] = $ht; } }

if ($doCards) { $rs = $this->CardRecords(FALSE); // get addresses for this customer while ($rs->NextRow()) { $qRows++; $idRow = $rs->KeyValue(); $ht = htmlspecialchars($rs->AsSingleLine()); $ar[$idCust]['cards'][$idRow] = $ht; } } if ($qRows > 0) { $sCust = $this->SingleLine(); $ar[$idCust]['cust'] = $sCust; } } return $ar;

   }
   /*----
     RETURNS: array of orders for this customer
     FUTURE: also check ID_Buyer and ID_Recip
     HISTORY:

2012-01-08 split off from AdminOrders(), moved from admin.cust to base.cust

   */
   protected function Orders_array() {

$tOrd = $this->Engine()->Orders(); $idCust = $this->KeyValue(); $arRow = NULL; $arOrd = NULL;

// collect orders where customer is buyer $rs = $tOrd->GetData('ID_Buyer='.$idCust); while ($rs->NextRow()) { $idOrd = $rs->KeyValue(); $arRow = $rs->Values(); $arRow['roles'] = nz($arRow['roles']).'B'; $arOrd[$idOrd] = $arRow; }

// collect orders where customer is recipient $rs = $tOrd->GetData('ID_Recip='.$idCust); while ($rs->NextRow()) { $idOrd = $rs->KeyValue(); if (array_key_exists($idOrd,$arOrd)) { $arRow = $arOrd[$idOrd]; } else { $arRow = $rs->Values(); } $arRow['roles'] = nz($arRow['roles']).'R'; $arOrd[$idOrd] = $arRow; }

return $arOrd;

   }
   // -- DATA RECORD ARRAYS -- //
   // ++ ACTIONS ++ //
   /*----
     USED BY: Customers table
   */
   public function FinishRecord($idName,$idAddr) {

$arUpd = array( 'ID_Name' => $idName, 'ID_Addr' => $idAddr, 'WhenCreated' => 'NOW()', ); $ok = $this->Update($arUpd);

   }
   // -- ACTIONS -- //
   // ++ WEB UI ++ //
   /*----
     RETURNS: HTML for a drop-down list of all Addresses belonging to

any of the Customer records in the current set

   */
   public function Render_DropDown_Addrs($sName) {

$out = NULL; $rs = $this->AddrRecords_forRows(); if (is_null($rs) || ($rs->RowCount() == 0)) { $out .= 'No addresses found.'; // should this actually ever happen? } else { $out .= "\n<select name='$sName'>"; while ($rs->NextRow()) { $id = $rs->KeyValue(); $sRow = $rs->AsString(' / '); $ht = htmlspecialchars($sRow); $out .= "\n<option value=$id>$ht</option>"; } $out .= "\n</select>"; } return $out;

   }
   /*----
     RETURNS: HTML for a drop-down list of all Cards belonging to

any of the Customer records in the current set

   */
   public function Render_DropDown_Cards($sName) {

$out = NULL; $rs = $this->CardRecords_forRows(); if (is_null($rs) || ($rs->RowCount() == 0)) { $out .= 'No cards found.'; // should this actually ever happen? } else { $out .= "\n<select name='$sName'>"; while ($rs->NextRow()) { $id = $rs->KeyValue(); $sRow = $rs->SafeString(); $ht = htmlspecialchars($sRow); $out .= "\n<option value=$id>$ht</option>"; } $out .= "\n</select>"; } return $out;

   }
   /*----
     RETURNS: HTML for a drop-down list of all the customers

in the current recordset

     NOTE: This is somewhat DEPRECATED
   */
   public function Render_DropDown($iName,$doNames,$doAddrs,$doCards) {

$ar = $this->AsArray($doNames,$doAddrs,$doCards);

// output the results

$out = NULL;

if (is_null($ar) || (count($ar) == 0)) { $out .= 'No entries found.'; // should this actually ever happen? } else { $out .= "\n<select name=\"$iName\">";

foreach ($ar as $idCust => $arCust) {

//$ht = escapeshellarg($arCust['cust']); if (array_key_exists('addrs',$arCust)) { $ht = escapeshellarg("customer ID #$idCust"); $out .= "\n<optgroup label=$ht>";

$arAddr = $arCust['addrs']; foreach ($arAddr as $idAddr => $sAddr) { $ht = htmlspecialchars($sAddr); $out .= "\n<option value=$idAddr>$ht</option>"; }

$out .= "\n</optgroup>"; }

if (array_key_exists('cards',$arCust)) { // TODO: finish } }

$out .= "\n</select>"; }

return $out;

   }

} </php>