Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][HTML] Uniwersalna funkcja do odczytu/zapisu danych z formularz
Forum PHP.pl > Forum > Przedszkole
dantekir
Witam,

Czy ktoś ma pomysł w jaki sposób napisać poniższy kod tak aby był jak najbardziej uniwersalny (dla dowolnego formularza)?

  1. <?php
  2. /*
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. try {
  7. require_once 'class/class.DataManager.php';
  8.  
  9. $issend=NULL;
  10. $id=NULL;
  11. $obj=FALSE;
  12. $issend=$_GET['issend'];
  13. $objname='Autor';
  14.  
  15. if(!$issend){
  16.    ////print('gdy formularz nie wysłany');
  17.    $id=$_GET['id'];
  18.    if(!$id){
  19.        ////print('gdy formularz dla nowej jednostki');
  20.    }else{
  21.        ////print('gdy formularz dla starej jednostki');
  22.        $obj=new $objname($id);
  23.        $getData['id']=$id;
  24.        $getData['nazwa']=$obj->nazwa;
  25.        $getData['pesel']=$obj->pesel;
  26.        $getData['nip']=$obj->nip;
  27.        $getData['nrdowodu']=$obj->nrdowodu;
  28.        foreach($obj->adresses as $key=>$adres){
  29.            $getData['adres|idadres']=$adres->idadres;
  30.            $getData['adres|ulica']=$adres->ulica;
  31.            $getData['adres|nrdomu']=$adres->nrdomu;
  32.            $getData['adres|nrlokalu']=$adres->nrlokalu;
  33.            $getData['adres|miejscowosc']=$adres->miejscowosc;
  34.            $getData['adres|kod']=$adres->kod;
  35.        }
  36.        foreach($obj->telephones as $key=>$telefon){
  37.            $getData['telefon|numer']=$telefon->numer;
  38.            $getData['telefon|idtelefon']=$telefon->idtelefon;
  39.        }
  40.        foreach($obj->emails as $key=>$email){
  41.            $getData['email|email']=$email->email;
  42.            $getData['email|idemail']=$email->idemail;
  43.        }
  44.        
  45.    }
  46.  
  47. }else{
  48.    ////print('gdy formularz wysłany');
  49.    $id=$_GET['id'];
  50.    unset($_GET['issend']);
  51.    unset($_GET['id']);
  52.    $getData=$_GET;
  53.    $obj=new $objname($id);
  54.    $adres=new Adres($getData['adres|idadres']);
  55.    $telephone=new Telefon($getData['telefon|idtelefon']);
  56.    $email=new Email($getData['email|idemail']);
  57.  
  58.    foreach(array_keys($getData) as $item){
  59.        if(!strstr($item,'|')){
  60.            $obj->$item=$getData[$item];
  61.        }else{
  62.  
  63.        }
  64.    }
  65.  
  66. $telephone->numer=$getData['telefon|numer'];
  67. $obj->addTelefon($telephone);
  68.  
  69. $email->email=$getData['email|email'];
  70. $obj->addEmail($email);
  71.  
  72. $adres->miejscowosc=$getData['adres|miejscowosc'];
  73. $adres->nrlokalu=$getData['adres|nrlokalu'];
  74. $adres->nrdomu=$getData['adres|nrdomu'];
  75. $adres->kod=$getData['adres|kod'];
  76. $adres->ulica=$getData['adres|ulica'];
  77. $obj->addAdres($adres);
  78.  
  79. if(!$id){
  80.    $obj->save();
  81.    $email->save($obj->idjednostka);
  82.    $adres->save($obj->idjednostka);
  83.    $telephone->save($obj->idjednostka);
  84. }else{
  85.    $obj->update();
  86.    $email->update();
  87.    $adres->update();
  88.    $telephone->update();
  89. }
  90.  
  91. print('<br><h3>Dane zostały pomyślnie wprowadzone do bazy</h3>');
  92.  
  93.  
  94. }
  95.  
  96.  
  97. ?>
  98. <br>
  99. <h3>Dodawanie nowego autora</h3>
  100.  
  101. <form id="newautor"  action="newautor.php" method="get" >
  102. Imię i nazwisko:<br>
  103.    <input type="hidden" name="issend" value="TRUE" />
  104.    <input type="hidden" name="id" value="<?echo isset($getData['id']) ? $getData['id'] : '';?>" />
  105.    <input type="text" name="nazwa" value="<?echo isset($getData['nazwa']) ? $getData['nazwa'] : '';?>" /><br><br>
  106.  
  107. Adres<br>
  108. Ulica (nr domu/ nr lokalu):<br>
  109.    <input type="hidden" name="adres|idadres" value="<?echo isset($getData['adres|idadres']) ? $getData['adres|idadres'] : '';?>" />
  110.    <input type="text" name="adres|ulica" value="<?echo isset($getData['adres|ulica']) ? $getData['adres|ulica'] : '';?>" />
  111.    <input type="text" name="adres|nrdomu" size="5" value="<?echo isset($getData['adres|nrdomu']) ? $getData['adres|nrdomu'] : '';?>" /> /
  112.    <input type="text" name="adres|nrlokalu" size="5" value="<?echo isset($getData['adres|nrlokalu']) ? $getData['adres|nrlokalu'] : '';?>" /><br>
  113. Miejscowość, kod pocztowy:<br>
  114.    <input type="text" name="adres|miejscowosc" value="<?echo isset($getData['adres|miejscowosc']) ? $getData['adres|miejscowosc'] : '';?>" />,
  115.    <input type="text" name="adres|kod" size="15" value="<?echo isset($getData['adres|kod']) ? $getData['adres|kod'] : '';?>" /><br><br>
  116.  
  117. Telefon:<br>
  118.    <input type="hidden" name="telefon|idtelefon" value="<?echo isset($getData['telefon|idtelefon']) ? $getData['telefon|idtelefon'] : '';?>" />
  119.    <input type="text" name="telefon|numer" value="<?echo isset($getData['telefon|numer']) ? $getData['telefon|numer'] : '';?>" /><br><br>
  120.  
  121.  
  122. Adres email:<br>
  123.    <input type="hidden" name="email|idemail" value="<?echo isset($getData['email|idemail']) ? $getData['email|idemail'] : '';?>" />
  124.    <input type="text" name="email|email" value="<?echo isset($getData['email|email']) ? $getData['email|email'] : '';?>" /><br><br>
  125.    
  126. NIP:<br>
  127.    <input type="text" name="pesel" value="<?echo isset($getData['pesel']) ? $getData['pesel'] : '';?>" /><br><br>
  128. PESEL:<br>
  129.    <input type="text" name="nip" value="<?echo isset($getData['nip']) ? $getData['nip'] : '';?>" /><br><br>
  130. NR Dowodu:<br>
  131.    <input type="text" name="nrdowodu" value="<?echo isset($getData['nrdowodu']) ? $getData['nrdowodu'] : '';?>" /><br><br>
  132.  
  133.    
  134.    <input type="submit" value="Zapisz"  />
  135. <?
  136. echo isset($getData['id']) ? '<input type="button" onClick="del('autor',''.$getData['id'].'','substance')" value="Usuń"  />' : '';
  137. ?>
  138.  
  139. </form>
  140. <br><br>
  141.  
  142. <?
  143. } catch (Exception $e) {
  144. echo 'wystąpił błąd <br> ',  $e->getMessage(), "\n";
  145. }
  146. ?>


Mam pełno formularzy tego typu i zmian wszystkich danych jest bardzo czasochłonna - za najbardziej uniwersalne rozwiązanie mogę odwdzięczyć się nagrodą.
Fifi209
Musiałbyś pokazać tą klasę.

Możesz w niej zwrócić tablicę i tylko przelecieć potem pętlą.
dantekir
no cóż klas jest dosyć dużo...
Autor:
  1. <?php
  2. require_once 'class.Osoba.php';
  3. /**
  4.  * Description of osoba
  5.  *
  6.  * @author daniel
  7.  */
  8. class Autor extends Osoba{
  9.    public function __construct($id=null) {
  10.        if(!$id){
  11.            parent::__construct(null);
  12.        } else {
  13.            $arData=DataManager::getData($id,'autor');
  14.            parent::__construct($arData);
  15.        }
  16.            $this->propertyTable['idautor']='idautor';
  17.            $this->idtyposoba=DataManager::getIdForTypOsoby('autor');
  18.    }
  19.    public function  __toString() {
  20.        return (string)$this->nazwa;
  21.    }
  22.    public function save(){
  23.        parent::save();
  24.        $newData = array();
  25.        $newData['idosoba']=$this->idosoba;
  26.        $this->idautor=DataManager::saveNewField('autor',$newData);
  27.    }
  28.    public function update(){
  29.        parent::update();
  30.        $newData = array();
  31.        $newData['idosoba']=$this->idosoba;
  32.        DataManager::saveField('autor',$newData,$this->idautor);
  33.    }
  34.    public function del(){
  35.        parent::del();
  36.    }
  37. }
  38. ?>


Osoba:
  1. <?php
  2. require_once 'class.Jednostka.php';
  3. require_once 'class.TypOsoby.php';
  4. /**
  5.  * Description of osoba
  6.  *
  7.  * @author daniel
  8.  */
  9. abstract class Osoba extends Jednostka{
  10.    
  11.    public function __construct($arData) {
  12.        if(!$arData){          
  13.            parent::__construct(null);
  14.        } else {
  15.            $arData2=DataManager::getData($arData['idosoba'],'osoba');
  16.            $arData=array_merge($arData,$arData2);
  17.            parent::__construct($arData);
  18.        }
  19.            $this->propertyTable['pesel']='pesel';
  20.            $this->propertyTable['nrdowodu']='nrdowodu';
  21.            $this->propertyTable['idosoba']='idosoba';
  22.            $this->propertyTable['idfirma']='idfirma';
  23.            $this->propertyTable['idtyposoba']='idtyposoba';
  24.    }
  25.  
  26.    public function save(){
  27.  
  28.        parent::save();
  29.        $newData = array();
  30.        $newData['pesel']=$this->pesel;
  31.        $newData['nrdowodu']=$this->nrdowodu;
  32.        $newData['idfirma']=null;
  33.        $newData['idtyposoba']=$this->idtyposoba;
  34.        $newData['idjednostka']=$this->idjednostka;
  35.        $this->idosoba=DataManager::saveNewField('osoba',$newData);
  36.  
  37.    }
  38.    public function update(){
  39.  
  40.        parent::update();
  41.        $newData = array();
  42.        $newData['pesel']=$this->pesel;
  43.        $newData['nrdowodu']=$this->nrdowodu;
  44.        $newData['idfirma']=null;
  45.        $newData['idtyposoba']=$this->idtyposoba;
  46.        $newData['idjednostka']=$this->idjednostka;
  47.        DataManager::saveField('osoba',$newData,$this->idosoba);
  48.  
  49.    }
  50.    public function del(){
  51.        parent::del();
  52.    }
  53. }
  54. ?>


Jednostka:
  1. <?php
  2. require_once 'class.PropertyObject.php';
  3. require_once 'class.Collection.php';
  4. require_once 'class.Telefon.php';
  5. require_once 'class.Adres.php';
  6. require_once 'class.Email.php';
  7. require_once 'class.Organizator.php';
  8. require_once 'class.Firma.php';
  9. require_once 'class.TypOsoby.php';
  10. /**
  11.  * Description of classJednostka
  12.  *
  13.  * @author daniel
  14.  */
  15. abstract class Jednostka extends PropertyObject
  16. {
  17.    public $telephones = array();
  18.    public $emails = array();
  19.    public $adresses = array();
  20.    //public $typjednostka;
  21.    //public $nazwa;
  22.    //public $idjednostka;
  23.   // public $nip;
  24.  
  25.    public function __construct($arData) {
  26.        if(!$arData){
  27.            parent::__construct(null);
  28.            $this->telephones= new Collection();
  29.            $this->emails= new Collection();
  30.            $this->adresses= new Collection();
  31.        } else {
  32.            $arData2=DataManager::getData($arData['idjednostka'],'jednostka');
  33.            $arData=array_merge($arData,$arData2);
  34.            parent::__construct($arData);
  35.            $this->telephones= new Collection();
  36.            $this->telephones->setLoadCallback('_loadTelephones',$this);
  37.            $this->emails= new Collection();
  38.            $this->emails->setLoadCallback('_loadEmails',$this);
  39.            $this->adresses= new Collection();
  40.            $this->adresses->setLoadCallback('_loadAdresses',$this);
  41.  
  42.        }
  43.            $this->propertyTable['nazwa']='name';
  44.            $this->propertyTable['idjednostka']='idjednostka';
  45.            $this->propertyTable['nip']='nip';
  46.    }
  47.    public function _loadTelephones(Collection $col) {
  48.        $this->telephones=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'telefon');
  49.    } // end of member function
  50.    public function _loadEmails(Collection $col) {
  51.        $this->emails=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'email');
  52.    } // end of member function
  53.    public function _loadAdresses(Collection $col) {
  54.        $this->adresses=DataManager::getColectionObjectsForEntity($this->idjednostka, $col, 'adres');
  55.    } // end of member function
  56.    public function addAdres(Adres $adresses){
  57.        $this->adresses->addItem($adresses);
  58.    }
  59.    public function addEmail(Email $email){
  60.        $this->emails->addItem($email);
  61.    }
  62.    public function addTelefon(Telefon $telefon){
  63.        $this->telephones->addItem($telefon);
  64.    }
  65.    public function save(){
  66.        $newData = array();
  67.        $newData['name']=$this->nazwa;
  68.        $newData['nip']=$this->nip;
  69.        $this->idjednostka=DataManager::saveNewField('jednostka',$newData);
  70.    }
  71.    public function update(){
  72.        $newData = array();
  73.        $newData['name']=$this->nazwa;
  74.        $newData['nip']=$this->nip;
  75.        DataManager::saveField('jednostka',$newData,$this->idjednostka);
  76.    }
  77.    public function del(){
  78.        DataManager::delField($this->idjednostka,'jednostka');
  79.      
  80.    }
  81. }
  82. ?>


Adres:
  1. <?php
  2. /*
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. require_once 'class.PropertyObject.php';
  7. /**
  8.  * Description of classAddress
  9.  *
  10.  * @author daniel
  11.  */
  12. class Adres extends PropertyObject
  13. {
  14.  
  15.  
  16.    public function __construct($id=null) {
  17.        if(!$id){
  18.        }else{
  19.            $arData=DataManager::getData($id,'adres');
  20.            parent::__construct($arData);
  21.  
  22.        }
  23.            $this->propertyTable['miejscowosc']='miejscowosc';
  24.            $this->propertyTable['nrlokalu']='nrlokalu';
  25.            $this->propertyTable['nrdomu']='nrdomu';
  26.            $this->propertyTable['kod']='kod';
  27.            $this->propertyTable['ulica']='ulica';
  28.            $this->propertyTable['kraj']='kraj';
  29.            $this->propertyTable['idadres']='idadres';
  30.            $this->propertyTable['idjednostka']='idjednostka';
  31.    }
  32.    public function  __toString() {
  33.        return (string)($this->ulica." ".$this->nrdomu." ".$this->nrlokalu." ".$this->miejscowosc." ".$this->kod." ".$this->kraj." ");
  34.    }
  35.    public function save($idjednostka){
  36.        $newData = array();
  37.        $newData['miejscowosc']=$this->miejscowosc;
  38.        $newData['nrlokalu']=$this->nrlokalu;
  39.        $newData['nrdomu']=$this->nrdomu;
  40.        $newData['kod']=$this->kod;
  41.        $newData['ulica']=$this->ulica;
  42.        $newData['kraj']=$this->kraj;
  43.        $newData['idjednostka']=$idjednostka;
  44.        $this->idadres=DataManager::saveNewField('adres',$newData);
  45.    }
  46.    public function update(){
  47.        $newData = array();
  48.        $newData['miejscowosc']=$this->miejscowosc;
  49.        $newData['nrlokalu']=$this->nrlokalu;
  50.        $newData['nrdomu']=$this->nrdomu;
  51.        $newData['kod']=$this->kod;
  52.        $newData['ulica']=$this->ulica;
  53.        $newData['kraj']=$this->kraj;
  54.        $newData['idjednostka']=$this->idjednostka;
  55.        DataManager::saveField('adres',$newData,$this->idadres);
  56.    }
  57. }
  58. ?>


Property:
  1. <?
  2. require_once 'interface.Validate.php';
  3. /**
  4.  * class PropertyObject
  5.  *
  6.  */
  7. abstract class PropertyObject implements Validate
  8. {
  9.  
  10.  /** Aggregations: */
  11.  
  12.  /** Compositions: */
  13.  
  14.   /*** Attributes: ***/
  15.  
  16.  /**
  17.    *
  18.    * @access protected
  19.    */
  20.  
  21.  protected $propertyTable =array ();
  22.  protected $changedProperties= array();
  23.  protected $data;
  24.  protected $errors = array();
  25.  
  26.  
  27.  public function __construct($arData){
  28.      $this->data=$arData;
  29.  }
  30.  
  31.  function __get($propertyName){
  32.      if(!array_key_exists($propertyName, $this->propertyTable))
  33.            throw new Exception("Błędna własność \"$propertyName\"!  ");
  34.  
  35.      if(method_exists($this, 'get'.$propertyName)){
  36.          return call_user_func(array($this, 'get'.$propertyName));
  37.      } else {
  38.  
  39.          return $this->data[$this->propertyTable[$propertyName]];
  40.      }
  41.  }
  42.  
  43.  function __set($propertyName, $value){
  44.      if(!array_key_exists($propertyName, $this->propertyTable))
  45.        throw new Exception("Błędna własność \"$propertyName\"  ");
  46.      if(method_exists($this, 'set'.$propertyName)){
  47.          return call_user_func(
  48.              array($this,'set'.$propertyName),
  49.              $value
  50.          );
  51.      } else {
  52.          //jeżeli wartość własności uległa zmianie i nie ma jej
  53.          //jeszcze w tabeli chengedProperties, zostanie do niej dołączona
  54.          if($this->propertyTable[$propertyName]!=$value &&
  55.              !in_array($propertyName,$this->changedProperties)){
  56.                $this->changedProperties[]=$propertyName;
  57.              }
  58.          $this->data[$this->propertyTable[$propertyName]] = $value;
  59.      }
  60.  }
  61.  
  62.  
  63.  /**
  64.    *
  65.    *
  66.    * @return
  67.    * @access public
  68.    */
  69.  
  70.  function validate( ) {
  71.      
  72.  } // end of member function validate
  73.  
  74.  function errormsg(){
  75.      foreach ($this->errors as $value) {
  76.           print ('!!!!'.$value.'!!!!');
  77.      }
  78.  }
  79.  
  80. } // end of PropertyObject
  81. ?>
Fifi209
O.o nie przypuszczałem iż tego jest tak dużo. Jako iż "brzydko" napisane są klasy (m.in. polskie nazwy mieszane są z angielskimi) nie mam ochoty czytać całości.

Powiedz lepiej co do czego jest i jak działa. :__) No i oczywiście dokładnie co chcesz zrobić.
dantekir
No tak, mieszanie z angielskiego z polskim to moja specjalność - nie wiem skąd to u mnie ;]
Klasa jednostka posiada kolekcje adres, telefony i maile,
tą klasę dziedziczą takie klasy jak osoba, firma
Z osoby natomiast dziedziczą takie klasy jak Autor, Pilot itp.
Szczytem mojego zadowolenia byłby konstrukcja funkcji save() w klasie jednostka w taki sposób aby oprócz danych normalnych zapisywane były do bazy też wszystkie kolekcje (adres, email, telefon), ale to wzasadzie inny problem...

Zadowolę się natomiast rozwiązaniem które będzie zbierało do tablicy wszystkie możliwe parametry danej klasy (Autor, Pilot itp) a już poza klasą przy obsłudze wysłanego formularza bedzie sprawdzało czy dany formularz zawiera w parametrze znak | i tak tworzyło odpowiednie obiekty [obiekt|parametrobiektu] i przypisało im dane.

  1. <?php
  2. //...
  3.    foreach(array_keys($getData) as $item){
  4.        if(!strstr($item,'|')){
  5.            $obj->$item=$getData[$item];
  6.        }else{
  7.            //tutaj pobrać dane dla $item tworzyć [obiekt|parametr] i przypisać wartość $getData[$item]
  8.            //sprawdzić czy dany obiekt został już wcześniej utworzny
  9.  
  10.        }
  11.    }
  12. //...
  13. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.