<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Description of classCollectionIterator * * @author daniel */ class CollectionIterator implements Iterator { private $_collection; private $_currIndex=0; private $_keys; function __construct(Collection $objCol){ $this->_collection=$objCol; $this->_keys=$this->_collection->keys(); } $this->_currIndex=0; } function valid(){ return $this->_currIndex < $this->_collection->length(); } return $this->_keys[$this->_currIndex]; } return $this->_collection->getItem($this->_keys[$this->_currIndex]); } $this->_currIndex++; } } ?>
<?php require_once 'class.KeyInUseException.php'; require_once 'class.KeyInvalidException.php'; require_once 'class.CollectionIterator.php'; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * class Collection * uniwersalna klasa kolekcji danych * */ class Collection implements IteratorAggregate { /** * * @access private */ private $_onload; //funkcja zwrotna private $_isLoaded = false;//flaga określająca czy funkcja zwrotna została już wywołana public function addItem($obj, $key = NULL){ $this->_checkCallback();//czy zawartość kolekcji została załadowana if($key){ throw new KeyInUseException("Klucz \"$key\" jest już zajęty!"); } else { $this->_members[$key]=$obj; } } else { $this->_members[]=$obj; } } public function removeItem($key){ $this->_checkCallback();//czy zawartość kolekcji została załadowana } else { throw new KeyInvalidException("Błędny klucz \"$key\"!"); } } public function getItem($key){ $this->_checkCallback();//czy zawartość kolekcji została załadowana return $this->_members[$key]; } else { throw new KeyInvalidException("Błędny klucz \"$key\"!"); } } public function keys(){ $this->_checkCallback();//czy zawartość kolekcji została załadowana } public function exists($key){ $this->_checkCallback();//czy zawartość kolekcji została załadowana } public function length(){ $this->_checkCallback();//czy zawartość kolekcji została załadowana } /** * * @param <type> $functionName * @param <type> $objOrClass * @return <type> * Ta metoda pozwala na zdefiniowanie funkcji, * którą należy wywołać aby wypełnić kolekcję. * Jednym słowem parametrem tej funkcji powinna być kolekcja do wypełnienia */ public function setLoadCallback($functionName, $objOrClass = NULL){ if ($objOrClass){ } else { $callback = $functionName; } if (!is_callable($callback,false,$callableName)){ throw new Exception("Funkcja zwrotna $callableName nieprawidłowa!"); return false; } $this->_onload=$callback; } /** * sprawdzenie czy funkcja zwrotna została zdefiniowana * a jeśli tak czy została już wywołana * jeśli nie zostaje ona wywołana */ private function _checkCallback(){ $this->_isLoaded=true; call_user_func($this->_onload,$this); } } public function getIterator(){ $this->_checkCallback(); return new CollectionIterator(clone $this); } } ?>
<?php require_once 'class.PropertyObject.php'; require_once 'class.Collection.php'; require_once 'class.Telefon.php'; require_once 'class.Adres.php'; require_once 'class.Email.php'; require_once 'class.Organizator.php'; require_once 'class.Firma.php'; require_once 'class.TypOsoby.php'; /** * Description of classJednostka * * @author daniel */ abstract class Jednostka extends PropertyObject { //public $typjednostka; //public $nazwa; //public $idjednostka; // public $nip; public function __construct($arData) { if(!$arData){ parent::__construct(null); $this->telephones= new Collection(); $this->emails= new Collection(); $this->adresses= new Collection(); } else { $arData2=DataManager::getData($arData['idjednostka'],'jednostka'); parent::__construct($arData); $this->telephones= new Collection(); $this->telephones->setLoadCallback('_loadTelephones',$this); $this->emails= new Collection(); $this->emails->setLoadCallback('_loadEmails',$this); $this->adresses= new Collection(); $this->adresses->setLoadCallback('_loadAdresses',$this); } $this->propertyTable['nazwa']='name'; $this->propertyTable['idjednostka']='idjednostka'; $this->propertyTable['nip']='nip'; } public function _loadTelephones(Collection $col) { $this->telephones=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'telefon'); } // end of member function public function _loadEmails(Collection $col) { $this->emails=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'email'); } // end of member function public function _loadAdresses(Collection $col) { $this->adresses=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'adres'); } // end of member function public function addAdres(Adres $adresses){ $this->adresses->addItem($adresses); } public function addEmail(Email $email){ $this->emails->addItem($email); } public function addTelefon(Telefon $telefon){ $this->telephones->addItem($telefon); } public function save(){ $newData['name']=$this->nazwa; $newData['nip']=$this->nip; $this->idjednostka=DataManager::saveNewField('jednostka',$newData); } public function update(){ $newData['name']=$this->nazwa; $newData['nip']=$this->nip; DataManager::saveField('jednostka',$newData,$this->idjednostka); } public function del(){ //print ('ooooooooo'.$this->idjednostka); DataManager::delField($this->idjednostka,'jednostka'); } } ?>
<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ require_once 'class.PropertyObject.php'; /** * Description of classAddress * * @author daniel */ class Adres extends PropertyObject { //public $idadres; public function __construct($id=null) { if(!$id){ }else{ $arData=DataManager::getData($id,'adres'); parent::__construct($arData); } $this->propertyTable['miejscowosc']='miejscowosc'; $this->propertyTable['nrlokalu']='nrlokalu'; $this->propertyTable['nrdomu']='nrdomu'; $this->propertyTable['kod']='kod'; $this->propertyTable['ulica']='ulica'; $this->propertyTable['kraj']='kraj'; $this->propertyTable['idadres']='idadres'; $this->propertyTable['idjednostka']='idjednostka'; } public function __toString() { return (string)($this->ulica." ".$this->nrdomu." ".$this->nrlokalu." ".$this->miejscowosc." ".$this->kod." ".$this->kraj." "); } public function save($idjednostka){ $newData['miejscowosc']=$this->miejscowosc; $newData['nrlokalu']=$this->nrlokalu; $newData['nrdomu']=$this->nrdomu; $newData['kod']=$this->kod; $newData['ulica']=$this->ulica; $newData['kraj']=$this->kraj; $newData['idjednostka']=$idjednostka; $this->idadres=DataManager::saveNewField('adres',$newData); } public function update(){ $newData['miejscowosc']=$this->miejscowosc; $newData['nrlokalu']=$this->nrlokalu; $newData['nrdomu']=$this->nrdomu; $newData['kod']=$this->kod; $newData['ulica']=$this->ulica; $newData['kraj']=$this->kraj; $newData['idjednostka']=$idjednostka; DataManager::saveField('adres',$newData,$this->idadres); } } ?>
Chciałbym w klasie Jednostka funkcji save() zapisać wszystkie elementy kolekcji np. Addresses
Nie wiem jak to ugryźć...