<?
class frmObjects extends MForm
{
    public $group;
    public $user; 
    public $sector;
    public $person;
    public $student;
    public $test;
 
    public function __construct()
    {   
		parent::__construct('Persistence Sample - Persistent Objects');
        $this->setClose($this->manager->getActionURL('persistence','main'));
        $this->group   = $this->manager->getBusiness('persistence','group');
        $this->user    = $this->manager->getBusiness('persistence','user');
        $this->sector  = $this->manager->getBusiness('persistence','sector');
        $this->person  = $this->manager->getBusiness('persistence','person');
        $this->student = $this->manager->getBusiness('persistence','student');
        $this->test    = $this->manager->getBusiness('persistence','test');
        $this->defaultButton = false;
        $this->eventHandler();
	}

	function createFields()
    {
        $action = $this->manager->getCurrentURL();
        $array = array(
                 new MLinkButton('btnPost1','[User with given Id]',$action),
                 new MLinkButton('btnPost2','[User as Proxy]',$action),
                 new MLinkButton('btnPost3','[Sector]',$action),
                 new MLinkButton('btnPost4','[Group]',$action),
                 new MLinkButton('btnPost5','[Student - inheritance]',$action),
                 new MLinkButton('btnPost6','[Save Sector]',$action),
                 new MLinkButton('btnPost7','[Delete Sector]',$action),
        );
        $group1 = new MBaseGroup('lbGrp1','Using Objects',$array,'vertical');
        $group1->setShowLabel(false);

        $groups = array($group1);

        $divLeft  = new MDiv('divLeft',$groups);
            $divLeft->width = '45%';

        $divRight = new MContainer('divRight',array());
            $divRight->width = '50%';
            $divRight->float = 'left';

        $fields[] = array(
            $divLeft,
            $divRight
        );

        $this->setFields($fields);
	}

    public function btnPost1_click()
    {
        $this->user->getById(3);
        $text = "Login: {$this->user->login}<br>";
        $text .= "Password: {$this->user->password}<br>";
        $text .= "Nick: {$this->user->nick}<br>";
        $text .= "Name: " . $this->user->person->name ."<br>";
        $text .= "IdSetor: ".$this->user->idSector."<br>"; 
        $text .= "<br>retrieving association groups:<br>";
        $text .= "Groups:<br>";
        $this->user->retrieveAssociation('groups');
        for($i=0;$i<count($this->user->groups);$i++) 
        {
            $text .= "&nbsp;&nbsp;&nbsp;Group: ". $this->user->groups[$i]->group . "<br>";
        }
        $text .= "<br>retrieving association sector:<br>";
        $this->user->retrieveAssociation('sector');
        $text .= "&nbsp;&nbsp;&nbsp;Sector Name: ". $this->user->sector->name . "<br>";

        $this->divRight->setControls(array(new MThemeBox('[User]', $text)));
    }

    public function btnPost2_click()
    {
        $this->user->getProxyById(4);
        $text .= "password and nick are empty -> it is not a proxy attribute<br><br>";
        $text .= "Login: {$this->user->login}<br>";
        $text .= "Password: {$this->user->password}<br>";
        $text .= "Nick: {$this->user->nick}<br>";
        $text .= "Name: " . $this->user->person->name ."<br>";
        $this->user->retrieveAssociation('sector');
        $text .= "Setor: ".$this->user->sector->nick."<br>"; 
        $this->divRight->setControls(array(new MThemeBox('[User as Proxy]', $text)));
    }

    public function btnPost3_click()
    {
        $this->sector->getById(12);
        $text = "IdSector: {$this->sector->id}<br>";
        $text .= "Nick: {$this->sector->nick}<br>";
        $text .= "Name: {$this->sector->name}<br>";
        $text .= "<br>retrieving association users, using inverse=true<br>:<br>";
        $this->sector->retrieveAssociation('users');
        $text .= "Usuarios:<br>";
        for($i=0;$i<count($this->sector->users);$i++) 
        {
            $text .= "&nbsp;&nbsp;&nbsp;User login: ". $this->sector->users[$i]->login . "<br>";
        }
        $text .= "<br>retrieving association parent:<br>";
        $this->sector->retrieveAssociation('parent');
        $text .= "Parent Sector:<br>";
        $text .= "    IdSector: {$this->sector->parent->id}<br>";
        $text .= "    Nick: {$this->sector->parent->nick}<br>";
        $text .= "    Name: {$this->sector->parent->name}<br>";
        $this->divRight->setControls(array(new MThemeBox('[Sector]', $text)));
    }

    public function btnPost4_click()
    {
        $this->group->getById(7);
        $text = "IdGroup: {$this->group->idgroup}<br>";
        $text .= "Group: {$this->group->group}<br>";
        $text .= "<br>retrieving association users:<br>";
        $this->group->retrieveAssociation('users');
        $text .= "Usuarios:<br>";
        for($i=0;$i<count($this->group->users);$i++) 
        {
            $text .= "&nbsp;&nbsp;&nbsp;User login: ". $this->group->users[$i]->login . "<br>";
        }
        $this->divRight->setControls(array(new MThemeBox('[Group]', $text)));
    }

    public function btnPost5_click()
    {
        $this->student->getById(4);
        $text .= "get name automatically by inheritance<br><br>";
        $text .= "Number: {$this->student->number}<br>";
        $text .= "Situation: {$this->student->situation}<br>";
        $text .= "Idperson: {$this->student->idPerson}<br>";
        $text .= "Name: " . $this->student->name ."<br>";
        $this->divRight->setControls(array(new MThemeBox('[Student - inheritance]', $text)));
    }

    public function btnPost6_click()
    {
        try 
        {
            $sector = $this->manager->getBusiness('persistence','sector');
            $sector->nick = 'New Sector';
            $sector->name = 'A New Test Sector';
            $sector->save();
            $text = "creating a new sector<br><br>";
            $text .= "IdSector: {$sector->id}<br>";
            $text .= "Nick: {$sector->nick}<br>";
            $text .= "Name: {$sector->name}<br>";
        }
        catch (Exception $e)
        {
            $text = $e->getMessage();
        }
        $this->divRight->setControls(array(new MThemeBox('[Save Sector]', $text)));
    }

    public function btnPost7_click()
    {
        try 
        {
            $sector = $this->manager->getBusiness('persistence','sector');
            $sector->nick = 'New Sector';
            $sector->name = 'A New Test Sector';
            $sector->save();
            $text = "creating a new sector<br><br>";
            $text .= "IdSector: {$sector->id}<br>";
            $text .= "Nick: {$sector->nick}<br>";
            $text .= "Name: {$sector->name}<br><br>";
            $text .= "deleting this new sector...<br><br>";
            $sector->delete();
        }
        catch (Exception $e)
        {
            $text = $e->getMessage();
        }
        $this->divRight->setControls(array(new MThemeBox('[Delete Sector]', $text)));
    }
}
?>
