Projet

Général

Profil

Evolution #1001

Mis à jour par Johan Cwiklinski il y a presque 6 ans

*Contexte*: https://github.com/trasher/galette/pull/1 
 J'ai modifié la class Adherent.php pour brider un peu les inscriptions et éviter ainsi que les adhérents puissent taper a date courante à la place de leur date de naissance. 
 Mon plugin a besoin d'une date de naissance valide pour calculer des tarifs. 
 J'ai donc fait un contrôle de l'age: 
 un age dépassant ]200ans..1an[ est invalide.  
 (idée: On pourrait laisser l'admin choisir le type de filtre ou le choix de l'activer ou non.) 
 * 
 détails du code:* 

 <pre><code class="php"> >case 'date_crea_adh': 
 case 'date_crea_adh': 
	 > 	 try { 
		 
 > 	 > 	 $d = \DateTime::createFromFormat(_T("Y-m-d"), $value); 
		
		 
 > 	 > 	
 > 	 > 	 if ( $d === false ) { 
			 
 > 	 > 	 > 	 //try with non localized date 
			 
 > 	 > 	 > 	 $d = \DateTime::createFromFormat("Y-m-d", $value); 
			 
 > 	 > 	 > 	 if ( $d === false ) { 
				 
 > 	 > 	 > 	 > 	 throw new \Exception('Incorrect format'); 
			 
 > 	 > 	 > 	 } 
		 
 > 	 > 	 } 
		 
 > 	 > 	 $this->$prop = $d->format('Y-m-d'); 
	 
 > 	 } catch (\Exception $e) { 
		 
 > 	 > 	 Analog::log( 
			 
 > 	 > 	 > 	 'Wrong date format. field: ' . $key . 
			 
 > 	 > 	 > 	 ', value: ' . $value . ', expected fmt: ' . 
			 
 > 	 > 	 > 	 _T("Y-m-d") . ' | ' . $e->getMessage(), 
			 
 > 	 > 	 > 	 Analog::INFO 
		 
 > 	 > 	 ); 
		 
 > 	 > 	 $errors[] = str_replace( 
			 
 > 	 > 	 > 	 array( 
				 
 > 	 > 	 > 	 > 	 '%date_format', 
				 
 > 	 > 	 > 	 > 	 '%field' 
			 
 > 	 > 	 > 	 ), 
			 
 > 	 > 	 > 	 array( 
				 
 > 	 > 	 > 	 > 	 _T("Y-m-d"), 
				 
 > 	 > 	 > 	 > 	 $this->_fields[$key]['label'] 
			 
 > 	 > 	 > 	 ), 
			 
 > 	 > 	 > 	 _T("- Wrong date format (%date_format) for %field!") 
		 
 > 	 > 	 ); 
	 
 > 	 } 
	 
 > 	 break; 
 case >case 'ddn_adh': 
	 
 > 	 try { 
		 
 > 	 > 	 $d = \DateTime::createFromFormat(_T("Y-m-d"), $value); 
		
		 
 > 	 > 	
 > 	 > 	 $birthdate= \DateTime::createFromFormat('j/m/Y',$value); 
		 
 > 	 > 	 $today= new \DateTime("now"); 
		 
 > 	 > 	 //echo('today:'); 
		 
 > 	 > 	 //var_dump($today); 
		 
 > 	 > 	 //echo('birthdate:'); 
		 
 > 	 > 	 //var_dump($birthdate); 
		 
 > 	 > 	 //echo('diff:'); 
		 
 > 	 > 	 $age=$birthdate->diff($today); 
		 
 > 	 > 	 //var_dump($age->format('%R%Y')); 

		 

 > 	 > 	 $age=$age->format('%Y'); 
		 
 > 	 > 	 if($age>200 || $age<1) 
			 
 > 	 > 	 > 	 { 
			 
 > 	 > 	 > 	 $d=false; 
			 
 > 	 > 	 > 	 } 
		
		 
 > 	 > 	 > 	 > 	
 > 	 > 	
 > 	 > 	 if ( $d === false ) { 
			 
 > 	 > 	 > 	 //try with non localized date 
			 
 > 	 > 	 > 	 $d = \DateTime::createFromFormat("Y-m-d", $value); 
			 
 > 	 > 	 > 	 if ( $d === false ) { 
				 
 > 	 > 	 > 	 > 	 throw new \Exception('Incorrect format'); 
			 
 > 	 > 	 > 	 } 
		 
 > 	 > 	 } 
		 
 > 	 > 	 $this->$prop = $d->format('Y-m-d'); 
	 
 > 	 } catch (\Exception $e) { 
		 
 > 	 > 	 Analog::log( 
			 
 > 	 > 	 > 	 'Wrong date format. field: ' . $key . 
			 
 > 	 > 	 > 	 ', value: ' . $value . ', expected fmt: ' . 
			 
 > 	 > 	 > 	 _T("Y-m-d") . ' | ' . $e->getMessage(), 
			 
 > 	 > 	 > 	 Analog::INFO 
		 
 > 	 > 	 ); 
		 
 > 	 > 	 $errors[] = str_replace( 
			 
 > 	 > 	 > 	 array( 
				 
 > 	 > 	 > 	 > 	 '%date_format', 
				 
 > 	 > 	 > 	 > 	 '%field' 
			 
 > 	 > 	 > 	 ), 
			 
 > 	 > 	 > 	 array( 
				 
 > 	 > 	 > 	 > 	 _T("Y-m-d"), 
				 
 > 	 > 	 > 	 > 	 $this->_fields[$key]['label'] 
			 
 > 	 > 	 > 	 ), 
			 
 > 	 > 	 > 	 _T("- Wrong date format (%date_format) for %field!") 
		 
 > 	 > 	 ); 
	 
 > 	 } 
	 
 > 	 break; 
 </code></pre>

Retour