Friday, March 13, 2009

Past data between a popup and mask while popup is open

i was able to simulate the digicel page that sends text messages to your contacts. on clicking on one of your contacts, it passes that name to a specific area. if you try to add the same name, it gives a javascript alert that lets you know that the contact was already added to the area to be sent a text message.

the code is below:

class popup_horses extends P4A_Mask {

var $selected_horses;
var $p4a;

function popup_horses(){

parent::p4a_mask();

$this->selected_horses = array();
$this->p4a = & p4a::singleton();

$this->checkIfHorsesWerePreviouslyAdded();

$page = & $this->showPage();

$this->intercept($this->Tbl->rows, 'afterClick', 'validatedSelectedHorse');
$this->display('main', $page);
}

function addPreviouslySelectedHorseNamesToArray(){

$horse_names = $this->getAllPreviouslyAddedHorseNames();
$temp_array = explode(',', $horse_names);

foreach ($temp_array as $temp){
array_push($this->selected_horses, trim($temp));
}
}

function checkIfHorsesWerePreviouslyAdded(){

if($this->isHorseAreasEmpty() == false){
$this->addPreviouslySelectedHorseNamesToArray();
}
}

function getAllPreviouslyAddedHorseNames(){
return $this->p4a->masks->parent_mask->taHorses->getNewValue();
}

function addSelectedHorseToArray($horse_name){
array_push($this->selected_horses, $horse_name);
}

function addSingleHorseToArea($horse_name){
$p4a = & p4a::singleton();
$p4a->masks->youtube_channel_archive->taHorses->setNewValue($horse_name);
}

function addHorseToAreaWithSeparator($horse_name){

$p4a = & p4a::singleton();
$horse_area_value = & $p4a->masks->parent_mask->taHorses->getNewValue();
$horse_area_value .= ", $horse_name";
$p4a->masks->parent_mask->taHorses->setNewValue($horse_area_value);
}

function validatedSelectedHorse(){

if(in_array($this->fields->horse_name->getNewValue(),$this->selected_horses)){
$this->showMessage("Horse already added", "warning");
} else {
$this->addSelectedHorseToArray($this->fields->horse_name->getNewValue());
if($this->isHorseAreasEmpty()){
$this->addSingleHorseToArea($this->fields->horse_name->getNewValue());
} else {
$this->addHorseToAreaWithSeparator($this->fields->horse_name->getNewValue());
}
}
}

function isHorseAreasEmpty(){

$p4a = & p4a::singleton();
if($p4a->masks->parent_mask->taHorses->getNewValue() == ""){
return true;
} else {
return false;
}
}

function validateForm(){

if(!p4a_validate::notempty($this->fields->distance->getNewValue())){
$errorMsg = "Please enter distance for race
";
}

if(!empty($errorMsg)){
$this->showMessage($errorMsg, 'warning');
} else {
$this->updateTableData();
}
}

function updateTableData(){

$track_record = & p4a::singleton('tract_record');

$this->saveRow();
if($this->setLastUpdated()){
$track_record->closePopup();
} else {
//$this->showMessage("Table data NOT modified!", "error");
}
}

function setLastUpdated(){

$dbObj = & p4a_db::singleton();
$today = & date('Y-m-d h:i:s');
$record_id = & $this->fields->distance_id->getNewValue();

//echo
$update = "
update race_distance
set date_modified = '$today'
where distance_id = $record_id;
";

if($dbObj->query($update)){
return true;
} else {
return false;
}
}

function createTable() {

$Tbl = & $this->build('p4a_table', 'Tbl');
$Tbl->setSource($this->createDBSource());
$Tbl->setVisibleCols(array('horse_name'));
$Tbl->showNavigationBar();
$Tbl->setWidth(400);
$Tbl->setTitle('Horse Name');
return $Tbl;
}

function createDBSource() {

$limit = 13;
$Src = & $this->build('p4a_db_source', 'Src');
$Src->setFields(array(
'horse.*'=>'*'
));
$Src->setTable('horse');
$Src->setWhere("");
$Src->setPk('horse_id');
$Src->addOrder('horse_name');
$Src->setPageLimit($limit);
$Src->load();
$this->setSource($Src);
return $Src;
}

function doReloadParentMask(){
$p4a = & p4a::singleton();
$parent = & p4a::singleton('track_record');
$parent->destroy();
$p4a->openMask('track_record');
}

function createEditor(){

$distance = & $this->fields->distance;

$btnAdd = & $this->build('p4a_button', 'btnAdd');
$btnAdd->setLabel('Add distance');
$this->intercept($btnAdd, 'onClick', 'validateForm');

$fsEditor = & $this->build('p4a_fieldset', 'fsEditor');
$fsEditor->anchor($distance);
$fsEditor->anchor($btnAdd);
$fsEditor->setWidth(350);
$fsEditor->setTitle('Race Distance Information');

return $fsEditor;
}

function doRefreshOriginalPage(){
$p4a = & p4a::singleton();
$this->destroy();
$p4a->openPopup($this->getName());
}

function showMessage($message, $icon){
$this->message->setValue($message);
$this->message->setIcon($icon);
$this->message->setWidth(500);
}

function showPage(){

$message = & $this->build('p4a_message', 'message');

$this->createDBSource();
$tableData = & $this->createTable();

$leftSide = & $this->build('p4a_frame', 'frmLeft');
$leftSide->anchor($tableData);

$frmPage = & $this->build('p4a_frame', 'frmPage');
$frmPage->anchorCenter($message);
$frmPage->anchor($leftSide);
$frmPage->setWidth(450);
return $frmPage;
}
}
?>

hope someone was able to understand this and it was what someone wanted to do what they wanted to do.

No comments: