Difference between revisions of "VbzCart/v1/class/vcCartDataManager"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
(updated code; created by)
Line 3: Line 3:
 
* Nothing inherits this class.
 
* Nothing inherits this class.
 
* '''purpose''': essentially a helper class for {{l/version|class|vcrCart}}.
 
* '''purpose''': essentially a helper class for {{l/version|class|vcrCart}}.
 +
* '''created by''': {{l/version|class|vcrCart}}::FieldsManager()
 
* '''points to''':
 
* '''points to''':
 
** {{l/version|class|vcrCart}} (parent)
 
** {{l/version|class|vcrCart}} (parent)
Line 10: Line 11:
 
** Creates the BuyerObject ({{l/version|class|vcCartData_Buyer}}) and SellerObject ({{l/version|class|vcCartData_Recip}})
 
** Creates the BuyerObject ({{l/version|class|vcCartData_Buyer}}) and SellerObject ({{l/version|class|vcCartData_Recip}})
 
<source lang=php>
 
<source lang=php>
 +
<?php
 
/*::::
 
/*::::
 
   PURPOSE: manager class for Cart field groups
 
   PURPOSE: manager class for Cart field groups
 +
    Handles blob data collectively and per-field
 
   HISTORY:
 
   HISTORY:
 
     2016-06-16 split off from cart.data.fg.php (formerly cart.xdata.php)
 
     2016-06-16 split off from cart.data.fg.php (formerly cart.xdata.php)
Line 19: Line 22:
 
     // ++ SETUP ++ //
 
     // ++ SETUP ++ //
  
    public function __construct(vcrCart $rcCart, vcShipCountry $oZone) {
+
//    public function __construct(vcrCart $rcCart, vcShipCountry $oZone) {
 +
    public function __construct(vcrCart $rcCart, vcCartForm $oForm) {
 
$this->SetCartRecord($rcCart);
 
$this->SetCartRecord($rcCart);
$this->SetShipZone($oZone);
+
$this->SetFormObject($oForm);
 +
//$this->GetFormObject()->SetShipZone($oZone);
 
     }
 
     }
 
     private $rcCart;
 
     private $rcCart;
Line 30: Line 35:
 
return $this->rcCart;
 
return $this->rcCart;
 
     }
 
     }
     private $oZone;
+
     private $oForm;
     protected function SetShipZone(vcShipCountry $oZone) {
+
     protected function SetFormObject(vcCartForm $oForm) {
$this->oZone = $oZone;
+
$this->oForm = $oForm;
 
     }
 
     }
     protected function GetShipZone() {
+
     protected function GetFormObject() : vcCartForm {
return $this->oZone;
+
return $this->oForm;
 
     }
 
     }
     private $oBlob = NULL;
+
      
     protected function GetBlobObject() {
+
    // -- SETUP -- //
if (is_null($this->oBlob)) {
+
    // ++ INTERNAL OBJECTS ++ //
    $oBlob = new fcBlobField();
+
   
    $oBlob->SetString($this->GetBlobString());
+
     protected function GetBlobObject() : fcBlobField {
    $this->oBlob = $oBlob;
+
// GetFormObject() returns vcCartForm which inherits fcForm_blob
}
+
return $this->GetFormObject()->GetBlobField();
return $this->oBlob;
 
 
     }
 
     }
 
     protected function GetBlobString() {
 
     protected function GetBlobString() {
 +
    echo 'SERIAL BLOB:<pre>'.$this->GetCartRecord()->GetSerialBlob().'</pre>';
 
return $this->GetCartRecord()->GetSerialBlob();
 
return $this->GetCartRecord()->GetSerialBlob();
 
     }
 
     }
Line 59: Line 64:
 
       HISTORY:
 
       HISTORY:
 
2016-06-06 I have no idea why everything but the first line was commented out. Uncommented.
 
2016-06-06 I have no idea why everything but the first line was commented out. Uncommented.
 +
2019-03-08 Not sure why the entire function was commented out -- it's needed. Uncommented.
 
     */
 
     */
    /*
 
 
     public function FetchBlob() {
 
     public function FetchBlob() {
 
$sBlob = $this->GetBlobString();
 
$sBlob = $this->GetBlobString();
Line 70: Line 75:
 
}
 
}
 
     }
 
     }
    */
 
 
     /*----
 
     /*----
 
       ACTIONS:
 
       ACTIONS:
Line 91: Line 95:
 
       USED BY: $this->FetchBlob()
 
       USED BY: $this->FetchBlob()
 
     */
 
     */
    /*
 
 
     protected function SetBlobArray(array $ar) {
 
     protected function SetBlobArray(array $ar) {
 
$this->arBlob = $ar;
 
$this->arBlob = $ar;
     } */
+
     }
 
      
 
      
     // -- SETUP -- //
+
     // -- INTERNAL OBJECTS -- //
 +
    // ++ INTERNAL DATA ++ //
 +
 
 +
    private $arMissed;
 +
    protected function AddMissing(array $arMissed) {
 +
$this->arMissed = array_merge($this->GetMissingArray(),$arMissed);
 +
    }
 +
    public function GetMissingArray() {
 +
if (empty($this->arMissed)) {
 +
    $this->arMissed = array();
 +
}
 +
return $this->arMissed;
 +
    }
 +
 
 +
    // -- INTERNAL DATA -- //
 
     // ++ SUBSETS ++ //
 
     // ++ SUBSETS ++ //
 
      
 
      
     private $oBuyer;
+
     private $oBuyer = NULL;
     public function BuyerObject(vcShipCountry $oZone=NULL) {
+
    // PUBLIC so the shopping cart object can access
if (is_object($oZone)) { throw new exception('Stop passing oZone.'); }
+
     public function GetBuyerObject() {
if (empty($this->oBuyer)) {
+
if (is_null($this->oBuyer)) {
    $this->oBuyer = new vcCartData_Buyer($this->GetBlobObject(),$this->GetShipZone());
+
    $this->oBuyer = new vcCartData_Buyer($this->GetFormObject());
 
}
 
}
 
return $this->oBuyer;
 
return $this->oBuyer;
 
     }
 
     }
     private $oRecip;
+
   
     public function RecipObject(vcShipCountry $oZone=NULL) {
+
     private $oRecip = NULL;
if (is_object($oZone)) { throw new exception('Stop passing oZone.'); }
+
    // PUBLIC so the shopping cart object can access
if (empty($this->oRecip)) {
+
     public function GetRecipObject() {
    $this->oRecip = new vcCartData_Recip($this->GetBlobObject(),$this->GetShipZone());
+
if (is_null($this->oRecip)) {
 +
    $this->oRecip = new vcCartData_Recip($this->GetFormObject());
 
}
 
}
 
return $this->oRecip;
 
return $this->oRecip;
Line 117: Line 135:
  
 
     // -- SUBSETS -- //
 
     // -- SUBSETS -- //
 +
    // ++ PAGES ++ //
 +
   
 +
    /*
 +
      NOTES:
 +
* *rendering* a page includes loading any existing values so they can be displayed.
 +
* *capturing* a page includes loading any existing values so they are not overwritten by blanks.
 +
  This has to be done by the objects, though, since the field objects are not yet defined here.
 +
    */
 +
   
 +
      // SHIPPING //
 +
   
 +
    public function RenderShippingPage() {
 +
echo 'GOT TO RenderShippingPage()<br>';
 +
//$oCDMgr = $this->FieldsManager();
 +
$oCDMgr = $this;
 +
$oCDMgr->FetchBlob();
 +
 +
$oCD_Buyer = $oCDMgr->GetBuyerObject();
 +
$oCD_Recip = $oCDMgr->GetRecipObject();
 +
 +
$out =
 +
  (new fcSectionHeader('Buyer information:'))->Render()
 +
  .$oCD_Buyer->RenderContact(TRUE) // edit email/phone
 +
  .(new fcSectionHeader('Shipping information:'))->Render()
 +
  .$oCD_Recip->RenderShipping(TRUE) // edit shipping address / instructions
 +
  ;
 +
return $out;
 +
    }
 +
    public function CaptureShippingPage() {
 +
echo 'GOT TO CaptureShippingPage()<br>';
 +
//$oCDMgr = $this->FieldsManager();
 +
$oCDMgr = $this;
 +
$oCDMgr->FetchBlob();
 +
 +
$oCD_Buyer = $oCDMgr->GetBuyerObject();
 +
$oCD_Recip = $oCDMgr->GetRecipObject();
 +
 +
$arStat = $this->GetFormObject()->Receive($_POST);
 +
 +
$oCD_Buyer->CaptureContact($arStat); // email/phone
 +
$oCD_Recip->CaptureShipping($arStat); // shipping address / instructions
 +
 +
$this->AddMissing($oCD_Buyer->GetMissingArray());
 +
$this->AddMissing($oCD_Recip->GetMissingArray());
 +
 +
// calculate resulting blob
 +
//echo 'BLOB AFTER FETCH: '.$oCDMgr->RenderBlob();
 +
echo 'UPDATING BUYER -> BLOB<br>';
 +
$oCDMgr->UpdateBlob($oCD_Buyer);
 +
echo 'UPDATING RECIP -> BLOB<br>';
 +
$oCDMgr->UpdateBlob($oCD_Recip);
 +
echo 'STORING BLOB<br>';
 +
$oCDMgr->StoreBlob();
 +
//echo 'SHIPPING VALUES:'.fcArray::Render($this->Values());
 +
//echo 'SHIPPING UPDATE ARRAY:'.fcArray::Render($this->UpdateArray());
 +
 +
$rcCart = $this->GetCartRecord();
 +
$rcCart->Save();
 +
//echo 'SHIPPING SAVE SQL: ['.$this->sqlExec.']<br>';
 +
//die();
 +
    }
 +
   
 +
      // BILLING //
 +
   
 +
    public function RenderBillingPage() {
 +
echo 'GOT TO RenderBillingPage()<br>';
 +
//$oCDMgr = $this->FieldsManager();
 +
$oCDMgr = $this;
 +
$oCDMgr->FetchBlob(); // fetch blob data from db
 +
 +
$oCD_Buyer = $oCDMgr->GetBuyerObject();
 +
 +
$oHdr = new fcSectionHeader('Payment information:');
 +
 +
$out =
 +
  $oHdr->Render()
 +
  .$oCD_Buyer->RenderPayment(TRUE) // edit payment information
 +
  ;
 +
return $out;
 +
    }
 +
    public function CaptureBillingPage() {
 +
echo 'GOT TO CaptureBillingPage()<br>';
 +
//$oCDMgr = $this->FieldsManager();
 +
$oCDMgr = $this;
 +
$oCDMgr->FetchBlob();
 +
 +
$oCD_Buyer = $oCDMgr->GetBuyerObject();
 +
$oCD_Buyer->CapturePayment(); // card #/exp, and I *think* name/address
 +
$this->AddMissing($oCD_Buyer->GetMissingArray());
 +
 +
// calculate resulting blob
 +
$oCDMgr->UpdateBlob($oCD_Buyer);
 +
    }
 +
   
 +
      // PAYMENT TYPE //
 +
   
 +
    public function RenderPayTypeSection() {
 +
$oPage = $this->GetPageObject();
 +
//$oCDMgr = $this->FieldsManager();
 +
$oCDMgr = $this;
 +
$oCD_PayType = $oCDMgr->PayTypeObject();
 +
 +
$sWhere = __METHOD__.'() in '.__FILE__;
 +
$out =
 +
  "\n<!-- vv $sWhere vv -->"
 +
  .$oPage->SectionHeader('Payment type:')
 +
  ."\n<table class='form-block' id='pay-type'>"
 +
  ;
 +
 +
$isShipCardSame = $this->FieldsObject()->IsShipToCard();
 +
$htChecked = $isShipCardSame?' checked':'';
 +
 +
$out .= "\n<tr><td align=center>\n"
 +
  .$this->Skin()->RenderPaymentIcons()
 +
  ."<table><tr><td>"
 +
  .'<input name=payType value="'.KSF_CART_PTYP_CARD_HERE.'" type=radio checked disabled> Visa / MasterCard / Discover / American Express - pay here'
 +
  .'<br>&emsp;<input name="'.KSF_SHIP_IS_CARD.'" type=checkbox value=1'.$htChecked.'>billing address is same as shipping address above'
 +
  ."</td></tr></table>\n"
 +
  ."<hr>More payment options will be available soon.\n"
 +
  ."</td></tr>";
 +
 +
$out .=
 +
  "\n</table>"
 +
  .$this->Skin()->SectionFooter()
 +
  ."\n<!-- ^^ $sWhere ^^ -->"
 +
  ;
 +
return $out;
 +
    }
 +
   
 +
    // -- PAGES -- //
 
     // ++ FORM I/O ++ //
 
     // ++ FORM I/O ++ //
 
      
 
      
 
     /*----
 
     /*----
 
       ACTION:
 
       ACTION:
       NOTE: Call FetchBlob() before calling this, and StoreBlob() when done with all updates.
+
       USAGE: Call FetchBlob() before calling this, and StoreBlob() when done with all updates.
 
     */
 
     */
 
     public function UpdateBlob(vcCartDataFieldGroup $oData) {
 
     public function UpdateBlob(vcCartDataFieldGroup $oData) {
 
$arForm = $oData->GetFormArray();
 
$arForm = $oData->GetFormArray();
 +
echo 'FORM DATA FOR BLOB:'.fcArray::Render($arForm);
 
if (is_array($arForm)) {
 
if (is_array($arForm)) {
 
//     $this->SetBlobArray(array_merge($this->GetBlobArray(),$ar));
 
//     $this->SetBlobArray(array_merge($this->GetBlobArray(),$ar));
Line 130: Line 279:
 
    $oBlob->MergeArray($arForm);
 
    $oBlob->MergeArray($arForm);
 
}
 
}
 +
    }
 +
    /*----
 +
      ACTION: Call UpdateBlob() for any data groups that have been modified.
 +
For now, we just call it for both of them. Maybe this is more maintainable regardless.
 +
    */
 +
    public function UpdateBlobs() {
 +
$this->UpdateBlob($this->GetBuyerObject());
 +
$this->UpdateBlob($this->GetRecipObject());
 +
$this->StoreBlob();
 
     }
 
     }
 
      
 
      
 
     // -- FORM I/O -- //
 
     // -- FORM I/O -- //
    // ++ DEBUGGING ++ //
 
   
 
    public function RenderBlob() {
 
return $this->GetBlobObject()->Render();
 
    }
 
 
 
}
 
}
 
</source>
 
</source>

Revision as of 15:30, 7 April 2019

<?php
/*::::
  PURPOSE: manager class for Cart field groups
    Handles blob data collectively and per-field
  HISTORY:
    2016-06-16 split off from cart.data.fg.php (formerly cart.xdata.php)
*/
class vcCartDataManager {

    // ++ SETUP ++ //

//    public function __construct(vcrCart $rcCart, vcShipCountry $oZone) {
    public function __construct(vcrCart $rcCart, vcCartForm $oForm) {
	$this->SetCartRecord($rcCart);
	$this->SetFormObject($oForm);
	//$this->GetFormObject()->SetShipZone($oZone);
    }
    private $rcCart;
    protected function SetCartRecord(vcrCart $rcCart) {
	$this->rcCart = $rcCart;
    }
    protected function GetCartRecord() {
	return $this->rcCart;
    }
    private $oForm;
    protected function SetFormObject(vcCartForm $oForm) {
	$this->oForm = $oForm;
    }
    protected function GetFormObject() : vcCartForm {
	return $this->oForm;
    }
    
    // -- SETUP -- //
    // ++ INTERNAL OBJECTS ++ //
    
    protected function GetBlobObject() : fcBlobField {
	// GetFormObject() returns vcCartForm which inherits fcForm_blob
	return $this->GetFormObject()->GetBlobField();
    }
    protected function GetBlobString() {
    echo 'SERIAL BLOB:<pre>'.$this->GetCartRecord()->GetSerialBlob().'</pre>';
	return $this->GetCartRecord()->GetSerialBlob();
    }
    protected function SetBlobString($s) {
	$this->GetCartRecord()->SetSerialBlob($s);
    }
    /*----
      ACTION: unserialize the blob and store it locally as an array
      PUBLIC because... well, maybe there's a better way to do this,
	but I don't know what it is. Cart objects need to be able to
	update the blob...
      HISTORY:
	2016-06-06 I have no idea why everything but the first line was commented out. Uncommented.
	2019-03-08 Not sure why the entire function was commented out -- it's needed. Uncommented.
    */
    public function FetchBlob() {
	$sBlob = $this->GetBlobString();
	if (is_null($sBlob)) {
	    $this->SetBlobArray(array());	// no data yet
	} else {
	    $arBlob = unserialize($sBlob);
	    $this->SetBlobArray($arBlob);
	}
    }
    /*----
      ACTIONS:
	* serialize the local array
	* save serialized array back to the blob object
	* save serialized array back to recordset field
	(Don't update recordset; caller should do that.)
      PUBLIC because... see FetchBlob()
    */
    public function StoreBlob() {
	$sBlob = serialize($this->GetBlobArray());
	$this->SetBlobString($sBlob);
    }
//    private $arBlob;

    protected function GetBlobArray() {
	return $this->GetBlobObject()->GetArray();
    }
    /*----
      USED BY: $this->FetchBlob()
    */
    protected function SetBlobArray(array $ar) {
	$this->arBlob = $ar;
    }
    
    // -- INTERNAL OBJECTS -- //
    // ++ INTERNAL DATA ++ //

    private $arMissed;
    protected function AddMissing(array $arMissed) {
	$this->arMissed = array_merge($this->GetMissingArray(),$arMissed);
    }
    public function GetMissingArray() {
	if (empty($this->arMissed)) {
	    $this->arMissed = array();
	}
	return $this->arMissed;
    }

    // -- INTERNAL DATA -- //
    // ++ SUBSETS ++ //
    
    private $oBuyer = NULL;
    // PUBLIC so the shopping cart object can access
    public function GetBuyerObject() {
	if (is_null($this->oBuyer)) {
	    $this->oBuyer = new vcCartData_Buyer($this->GetFormObject());
	}
	return $this->oBuyer;
    }
    
    private $oRecip = NULL;
    // PUBLIC so the shopping cart object can access
    public function GetRecipObject() {
	if (is_null($this->oRecip)) {
	    $this->oRecip = new vcCartData_Recip($this->GetFormObject());
	}
	return $this->oRecip;
    }

    // -- SUBSETS -- //
    // ++ PAGES ++ //
    
    /*
      NOTES:
	* *rendering* a page includes loading any existing values so they can be displayed.
	* *capturing* a page includes loading any existing values so they are not overwritten by blanks.
	  This has to be done by the objects, though, since the field objects are not yet defined here.
    */
    
      // SHIPPING //
    
    public function RenderShippingPage() {
	echo 'GOT TO RenderShippingPage()<br>';
	//$oCDMgr = $this->FieldsManager();
	$oCDMgr = $this;
	$oCDMgr->FetchBlob();

	$oCD_Buyer = $oCDMgr->GetBuyerObject();
	$oCD_Recip = $oCDMgr->GetRecipObject();

	$out =
	  (new fcSectionHeader('Buyer information:'))->Render()
	  .$oCD_Buyer->RenderContact(TRUE)	// edit email/phone
	  .(new fcSectionHeader('Shipping information:'))->Render()
	  .$oCD_Recip->RenderShipping(TRUE)	// edit shipping address / instructions
	  ;
	return $out;
    }
    public function CaptureShippingPage() {
	echo 'GOT TO CaptureShippingPage()<br>';
	//$oCDMgr = $this->FieldsManager();
	$oCDMgr = $this;
	$oCDMgr->FetchBlob();
	
	$oCD_Buyer = $oCDMgr->GetBuyerObject();
	$oCD_Recip = $oCDMgr->GetRecipObject();
	
	$arStat = $this->GetFormObject()->Receive($_POST);

	$oCD_Buyer->CaptureContact($arStat);	// email/phone
	$oCD_Recip->CaptureShipping($arStat);	// shipping address / instructions

	$this->AddMissing($oCD_Buyer->GetMissingArray());
	$this->AddMissing($oCD_Recip->GetMissingArray());
	
	// calculate resulting blob
	//echo 'BLOB AFTER FETCH: '.$oCDMgr->RenderBlob();
	echo 'UPDATING BUYER -> BLOB<br>';
	$oCDMgr->UpdateBlob($oCD_Buyer);
	echo 'UPDATING RECIP -> BLOB<br>';
	$oCDMgr->UpdateBlob($oCD_Recip);
	echo 'STORING BLOB<br>';
	$oCDMgr->StoreBlob();
	//echo 'SHIPPING VALUES:'.fcArray::Render($this->Values());
	//echo 'SHIPPING UPDATE ARRAY:'.fcArray::Render($this->UpdateArray());
	
	$rcCart = $this->GetCartRecord();
	$rcCart->Save();
	//echo 'SHIPPING SAVE SQL: ['.$this->sqlExec.']<br>';
	//die();
    }
    
      // BILLING //
    
    public function RenderBillingPage() {
	echo 'GOT TO RenderBillingPage()<br>';
	//$oCDMgr = $this->FieldsManager();
	$oCDMgr = $this;
	$oCDMgr->FetchBlob();	// fetch blob data from db
	
	$oCD_Buyer = $oCDMgr->GetBuyerObject();
	
	$oHdr = new fcSectionHeader('Payment information:');
	
	$out =
	  $oHdr->Render()
	  .$oCD_Buyer->RenderPayment(TRUE)	// edit payment information
	  ;
	return $out;
    }
    public function CaptureBillingPage() {
	echo 'GOT TO CaptureBillingPage()<br>';
	//$oCDMgr = $this->FieldsManager();
	$oCDMgr = $this;
	$oCDMgr->FetchBlob();
	
	$oCD_Buyer = $oCDMgr->GetBuyerObject();
	$oCD_Buyer->CapturePayment();	// card #/exp, and I *think* name/address
	$this->AddMissing($oCD_Buyer->GetMissingArray());
	
	// calculate resulting blob
	$oCDMgr->UpdateBlob($oCD_Buyer);
    }
    
      // PAYMENT TYPE //
    
    public function RenderPayTypeSection() {
	$oPage = $this->GetPageObject();
	//$oCDMgr = $this->FieldsManager();
	$oCDMgr = $this;
	$oCD_PayType = $oCDMgr->PayTypeObject();
	
	$sWhere = __METHOD__.'() in '.__FILE__;
	$out =
	  "\n<!-- vv $sWhere vv -->"
	  .$oPage->SectionHeader('Payment type:')
	  ."\n<table class='form-block' id='pay-type'>"
	  ;

	$isShipCardSame = $this->FieldsObject()->IsShipToCard();
	$htChecked = $isShipCardSame?' checked':'';

	$out .= "\n<tr><td align=center>\n"
	  .$this->Skin()->RenderPaymentIcons()
	  ."<table><tr><td>"
	  .'<input name=payType value="'.KSF_CART_PTYP_CARD_HERE.'" type=radio checked disabled> Visa / MasterCard / Discover / American Express - pay here'
	  .'<br>&emsp;<input name="'.KSF_SHIP_IS_CARD.'" type=checkbox value=1'.$htChecked.'>billing address is same as shipping address above'
	  ."</td></tr></table>\n"
	  ."<hr>More payment options will be available soon.\n"
	  ."</td></tr>";

	$out .=
	  "\n</table>"
	  .$this->Skin()->SectionFooter()
	  ."\n<!-- ^^ $sWhere ^^ -->"
	  ;
	return $out;
    }
    
    // -- PAGES -- //
    // ++ FORM I/O ++ //
    
    /*----
      ACTION:
      USAGE: Call FetchBlob() before calling this, and StoreBlob() when done with all updates.
    */
    public function UpdateBlob(vcCartDataFieldGroup $oData) {
	$arForm = $oData->GetFormArray();
	echo 'FORM DATA FOR BLOB:'.fcArray::Render($arForm);
	if (is_array($arForm)) {
//	    $this->SetBlobArray(array_merge($this->GetBlobArray(),$ar));
	    $oBlob = $this->GetBlobObject();
	    $oBlob->MergeArray($arForm);
	}
    }
    /*----
      ACTION: Call UpdateBlob() for any data groups that have been modified.
	For now, we just call it for both of them. Maybe this is more maintainable regardless.
    */
    public function UpdateBlobs() {
	$this->UpdateBlob($this->GetBuyerObject());
	$this->UpdateBlob($this->GetRecipObject());
	$this->StoreBlob();
    }
    
    // -- FORM I/O -- //
}