<?
class frmAssociation extends MForm
{
    public $group;
    public $user; 
    public $sector;
    public $person;
    public $student;
    public $test;
 
    public function __construct()
    {   
		parent::__construct('Persistence Sample - Using Associations');
        $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();
	}

	public 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','[Group: Delete Association with User  - N:N]',$action),
                 new MLinkButton('btnPost7','[Sector: Save Association with User]',$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 with given Id]', $text)));
    }

    public function btnPost2_click()
    {
        $this->user->getProxyById(4);
        $text .= "password and nick are empty -> they are not proxy attributes<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->getByNick('SS6');
        $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><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)));
    }

    function btnPost6_click()
    {  
        // Group: Delete Association with User  - N:N
        try 
        {


// create a new group
            $group = $this->manager->getBusiness('persistence','group');
            $group->group = 'A Group for Test';
            $group->save();

// create a new sector
            $sector = $this->manager->getBusiness('persistence','sector');
            $sector->nick = 'New Sector';
            $sector->name = 'A New Test Sector';
            $sector->save();

// create 3 users
            $user1 = $this->manager->getBusiness('persistence','user');
            $user1->person = $this->person->getById(1);
            $user1->idSector = $sector->id;
            $user1->login = 'user1';
            $user1->password = 'TEST';
            $user1->save();

            $user2 = $this->manager->getBusiness('persistence','user');
            $user2->person = $this->person->getById(2);
            $user2->idSector = $sector->id;
            $user2->login =  'user2';
            $user2->password = 'TEST';
            $user2->save();

            $user3 = $this->manager->getBusiness('persistence','user');
            $user3->person = $this->person->getById(3);
            $user3->idSector = $sector->id;
            $user3->login =  'user3';
            $user3->password = 'TEST';
            $user3->save();

// associate users <-> group
            $group->addUser($user1);
            $group->addUser($user2);
            $group->addUser($user3);
            $group->save();

// list group users
            $text .= "Users from Group '{$group->group}':<br>";
            $group->retrieveAssociation('users');
            for($i=0; $i<count($group->users);$i++) 
            {
                $text .= "&nbsp;&nbsp;&nbsp;User login: ". $group->users[$i]->login . "<br>";
            }

// delete association with users
            $text .= "<br>Removing association from group '{$group->group}' with user {$user1->login}.<br>";
            $group->deleteAssociation('users', $user1);
            $text .= "<br>Removing association from group '{$group->group}' with user {$user3->login}.<br>";
            $group->deleteAssociation('users', $user3);

// again, list group users
            $text .= "<br>Again, Users from Group '{$group->group}':<br>";
            $group->retrieveAssociation('users');
            for($i=0; $i<count($group->users);$i++) 
            {
                $text .= "&nbsp;&nbsp;&nbsp;User login: ". $group->users[$i]->login . "<br>";
            }
        }
        catch (Exception $e)
        {
            $text = $e->getMessage();
        }
        $this->divRight->setControls(array(new MThemeBox('[Group: Delete Association with User  - N:N]', $text)));
    }

    function btnPost7_click()
    {   
        // Sector: Save Association with User
        try 
        {

// create/retrieve sector 9999
            $sector = $this->manager->getBusiness('persistence','sector');
            $sector->getById(9999);
            if ($sector->name == '') // hum...sector doesn't exist, let's go to create it
            { 
               $sector->nick = 'New Sector';
               $sector->name = 'A new sector for tests';
               $sector->save();
            }

// create 3 users
            $user1 = $this->manager->getBusiness('persistence','user');
            $user1->person = $this->person->getById(1);
            $user1->idSector = $sector->id;
            $user1->login = 'user1';
            $user1->password = 'TEST';
            $user1->save();

            $user2 = $this->manager->getBusiness('persistence','user');
            $user2->person = $this->person->getById(2);
            $user2->idSector = $sector->id;
            $user2->login = 'user2';
            $user2->password = 'TEST';
            $user2->save();

            $user3 = $this->manager->getBusiness('persistence','user');
            $user3->person = $this->person->getById(3);
            $user3->idSector = $sector->id;
            $user3->login = 'user3';
            $user3->password = 'TEST';
            $user3->save();

// create a new sector

            $sector = $this->manager->getBusiness('persistence','sector');
            $sector->nick = 'A New Sector';
            $sector->name = 'A new sector for tests';
            $sector->save();

// list users from new sector (must be empty...)
            $text .= "Users from Sector {$sector->id}:<br>";
            $sector->retrieveAssociation('users');
            if (is_array($sector->users))
            {
                foreach($sector->users as $user)
                {
                    if (strlen($user->login) > 7) $text .= $user->login . '<br>';
                }
            }

// Associate users with new sector
            $text .= "<br>Adding users to sector {$sector->id}...<br>";
            $sector->addUser($user1);
            $sector->addUser($user2);
            $sector->addUser($user3);
            $text .= "<br>Saving the association....<br>";
            $sector->saveAssociation('users');

// again, list users from sector
            $text .= "<br>Users from Sector {$sector->id}:<br>";
            $sector->retrieveAssociation('users');
            if (is_array($sector->users))
            {
                foreach($sector->users as $user)
                {
                    if (strlen($user->login) > 7) $text .= $user->login . '<br>';
                }
            }
        }
        catch (Exception $e)
        {
            $text = $e->getMessage();
        }
        $this->divRight->setControls(array(new MThemeBox('[Sector: Save Association with User]', $text)));
    }

}
?>