Skip to content

Commit 5529b80

Browse files
committed
Updates for plantcompanion
1 parent a1b0e46 commit 5529b80

File tree

6 files changed

+132
-54
lines changed

6 files changed

+132
-54
lines changed

core/Controller.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ public function log($msg, $logLevel = LOG_LEVEL) {
3737
$this->log->log('controllers', $this->className, $this->loggerBaseString . " " . $msg, Logger::GRAN_MONTH);
3838
}
3939

40-
public function getAction( $action ){
41-
$action = firstchartolower( $action );
42-
$this->actionName = $action;
40+
public function getAction( $_action ){
41+
$_action = firstchartolower( $_action );
42+
$this->actionName = $_action;
4343

4444
$subClass = get_class($this);
4545
$this->className = $subClass;
4646

4747
$simpleName = firstchartolower( str_replace( "Controller_", "", $subClass ) );
4848
$this->controllerName = $simpleName;
49-
$pathView = BPCF_ROOT."/view/".$simpleName."/".$action.".phtml";
49+
$pathView = BPCF_ROOT."/view/".$simpleName."/".$_action.".phtml";
5050

5151
$content = "";
5252

5353
try {
54-
if(!method_exists($subClass, $action)) {
55-
throw new Exception("l'action '$action' du controller '$simpleName' n'existe pas !");
54+
if(!method_exists($subClass, $_action)) {
55+
throw new Exception("l'action '$_action' du controller '$simpleName' n'existe pas !");
5656
}
57-
$content = $this->$action();
57+
$content = $this->$_action();
5858
}
5959
catch (Exception $e) {
6060
throw new Exception($e->getMessage(), 0, $e);
@@ -78,7 +78,7 @@ public function getAction( $action ){
7878
die();
7979
}
8080

81-
return '<div id="'.$simpleName.'-'.$action.'" >'.$content.'</div>';
81+
return '<div id="'.$simpleName.'-'.$_action.'" >'.$content.'</div>';
8282
}
8383

8484
public function getScripts() {

core/Entite.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ public function __call($func,$args) {
138138
}
139139
}
140140

141+
/**
142+
* hydrate une entité à partir d'un tableau associatif clé/valeur où la clé est le nom de l'attribut de l'entité et la valeur la valeur à setter.
143+
* @return array{key, old, new}[] for modified values
144+
*/
145+
public function hydrater($arr_values) {
146+
$modified = [];
147+
if (!is_array($arr_values)) {
148+
throw new Exception('Entite::hydrater doit prendre un array en entré');
149+
}
150+
foreach ($arr_values as $key => $value) {
151+
if (!array_key_exists($key, $this->DB_equiv)) {
152+
$this->log("tentative d'hydratation de la propriété '$key' avec la valeur '$value'");
153+
continue;
154+
}
155+
if ($this->$key != $value) {
156+
$modified[] = [$key, $this->$key, $value];
157+
$this->$key = $value;
158+
}
159+
}
160+
return $modified;
161+
}
162+
141163
/**
142164
* save the Entite in database
143165
* @param Array $toUpdate [optional] list the classMembers attributes to update in database. If null, all attributes will be updated
@@ -191,7 +213,7 @@ public function enregistrer($toUpdate = null) {
191213
}
192214
}
193215
//debug($column);debug($values);
194-
$requete .= '('.implode(', ',$column).') VALUES('.implode(', ',$values).')';
216+
$requete .= '(`'.implode('`, `',$column).'`) VALUES('.implode(', ',$values).')';
195217
$insertID = SQL::getInstance()->exec($requete);
196218
$this->id = $insertID;
197219
//update the Entite with default values from database if any

core/Gestionnaire.php

+69-41
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private function cleanOrderBy($orderby) {
4242
$parts = explode(',', $orderby);
4343
foreach($parts as $part) {
4444
$p_part = explode(' ', trim($part));
45-
$cleaned[] = '`'.trim($p_part[0]).'`'.(count($p_part)>1?' '.trim($p_part[1]):'');
45+
$cleaned[] = 'te.`'.trim($p_part[0]).'`'.(count($p_part)>1?' '.trim($p_part[1]):'');
4646
}
4747
return implode(', ', $cleaned);
4848
}
@@ -62,7 +62,21 @@ public function getOne($id) {
6262
}
6363
}
6464

65-
public function getAll($orderby = 'id', $desc = false) {
65+
/**
66+
*
67+
* @param int $page index de la page
68+
* @param int $length longueur de la page
69+
* @param string $orderby colone de tri
70+
* @param boolean $desc tri DESC ou pas
71+
* @param array $mixedConditions conditions de filtre [var: value] ou [var: [op, value]] (WHERE var = value AND ...)
72+
*/
73+
public function getPaginate($page = 1, $length = 25, $orderby = 'id', $desc = false, array $mixedConditions = [], $join = '') {
74+
$limit = $length;
75+
$offset = ($page -1) * $length;
76+
return $this->getOf($mixedConditions, $orderby, $desc, $offset, $limit, $join);
77+
}
78+
79+
public function getAll($orderby = 'id', $desc = false, $limit = null, $offset = 0) {
6680
$dbequiv = $this->class->getDBEquiv();
6781
if(!is_null($orderby) && !empty($orderby)) {
6882
$desc = $desc?' DESC':' ASC';
@@ -71,7 +85,11 @@ public function getAll($orderby = 'id', $desc = false) {
7185
else {
7286
$orderby = '';
7387
}
74-
$all = $this->getSQL('SELECT '.$dbequiv['id'].' FROM `'.TABLE_PREFIX.$this->class->DB_table.'`'.$orderby);
88+
$limitclause = '';
89+
if (!is_null($limit)) {
90+
$limitclause = " LIMIT $offset, $limit";
91+
}
92+
$all = $this->getSQL('SELECT '.$dbequiv['id'].' FROM `'.TABLE_PREFIX.$this->class->DB_table.'` te'.$orderby.$limitclause);
7593
return $all;
7694
}
7795

@@ -98,7 +116,7 @@ public function countAll() {
98116
* @param integer $limitUp [optional, default 0] si $limitUp > $limitDown alors (LIMIT $limitDown, $limitUp)
99117
* @return Array<Entite> ($this->class) ou false si pas de resultat
100118
*/
101-
public function getOf(array $mixedConditions, $orderby = 'id', $desc = false, $limitDown = 0, $limitUp = 0) {
119+
public function getOf(array $mixedConditions, $orderby = 'id', $desc = false, $offset = 0, $limit = 0, $join = '') {
102120
$dbequiv = $this->class->getDBEquiv();
103121
if(!is_null($orderby) && !empty($orderby)) {
104122
$desc = $desc?' DESC':' ASC';
@@ -107,29 +125,34 @@ public function getOf(array $mixedConditions, $orderby = 'id', $desc = false, $l
107125
else {
108126
$orderby = '';
109127
}
110-
if($limitUp > $limitDown){
111-
$limit = ' LIMIT '.$limitDown.', '.$limitUp;
128+
if($limit > 0){
129+
$limit = ' LIMIT '.$offset.', '.$limit;
112130
}
113131
else {
114132
$limit = '';
115133
}
116-
$cond = array();
134+
$conditions = '';
117135
$params = array();
118-
foreach ($mixedConditions as $var => $value){
119-
$pk = 'p_'.(count($params)+1);
120-
$val = $value;
121-
if( is_array( $value ) ) {
122-
//forme [var, [op, value]]
123-
$cond[] = '`'.$dbequiv[$var].'` '.$value[0].($value[1] !== null? ' \'{{'.$pk.'}}\'' : ' NULL');
124-
$val = $value[1];
125-
}
126-
else {
127-
//forme [var, value] === [var, ["=", value] ]
128-
$cond[] = '`'.$dbequiv[$var].'` = '.($value!==null?'\'{{'.$pk.'}}\'':' NULL');
129-
}
130-
$params[$pk] = $val;
131-
}
132-
$all = $this->getSQL( 'SELECT `'.$dbequiv['id'].'` FROM `'.TABLE_PREFIX.$this->class->DB_table.'` WHERE '.implode(' AND ',$cond).$orderby.$limit, $params );
136+
if (is_array($mixedConditions) && count($mixedConditions) > 0) {
137+
$cond = array();
138+
foreach ($mixedConditions as $var => $value){
139+
$pk = 'p_'.(count($params)+1);
140+
$val = $value;
141+
if( is_array( $value ) ) {
142+
//forme [var: [op, value]]
143+
$cond[] = 'te.`'.$dbequiv[$var].'` '.$value[0].($value[1] !== null? ' \'{{'.$pk.'}}\'' : ' NULL');
144+
$val = $value[1];
145+
}
146+
else {
147+
//forme [var: value] === [var: ["=", value] ]
148+
$cond[] = 'te.`'.$dbequiv[$var].'` = '.($value!==null?'\'{{'.$pk.'}}\'':' NULL');
149+
}
150+
$params[$pk] = $val;
151+
}
152+
$conditions = 'WHERE '.implode(' AND ',$cond);
153+
}
154+
$join = " $join ";
155+
$all = $this->getSQL( 'SELECT te.`'.$dbequiv['id'].'` FROM `'.TABLE_PREFIX.$this->class->DB_table.'` as te '.$join.$conditions.$orderby.$limit, $params );
133156
return $all;
134157
}
135158

@@ -138,8 +161,8 @@ public function getOf(array $mixedConditions, $orderby = 'id', $desc = false, $l
138161
* @param array $mixedConditions [var: value] or [var, [op, value]] (WHERE var = value AND ...)
139162
* @return Entite $this->class ou false si pas de r�sultat
140163
*/
141-
public function getOneOf(array $mixedConditions, $orderby = 'id', $desc = false){
142-
$ret = $this->getOf($mixedConditions, $orderby, $desc, 0, 1);
164+
public function getOneOf(array $mixedConditions, $orderby = 'id', $desc = false, $join = ''){
165+
$ret = $this->getOf($mixedConditions, $orderby, $desc, 0, 1, $join);
143166
if($ret !== false){
144167
return $ret[0];
145168
}
@@ -151,25 +174,30 @@ public function getOneOf(array $mixedConditions, $orderby = 'id', $desc = false)
151174
* @param array $mixedConditions [var: value] or [var : [op, value]] (WHERE var = value AND ...)
152175
* @return integer
153176
*/
154-
public function countOf(array $mixedConditions) {
177+
public function countOf(array $mixedConditions, $join = '') {
155178
$dbequiv = $this->class->getDBEquiv();
156-
$cond = array();
179+
$conditions = '';
157180
$params = array();
158-
foreach ($mixedConditions as $var => $value){
159-
$pk = 'p_'.(count($params)+1);
160-
$val = $value;
161-
if( is_array( $value ) ) {
162-
//forme [var, [op, value]]
163-
$cond[] = '`'.$dbequiv[$var].'` '.$value[0].($value[1] !== null? ' \'{{'.$pk.'}}\'' : ' NULL');
164-
$val = $value[1];
165-
}
166-
else {
167-
//forme [var, value] === [var, ["=", value] ]
168-
$cond[] = '`'.$dbequiv[$var].'` = '.($value!==null?'\'{{'.$pk.'}}\'':' NULL');
169-
}
170-
$params[$pk] = $val;
171-
}
172-
$res = SQL::getInstance()->exec('SELECT COUNT(*) as nombre FROM `'.TABLE_PREFIX.$this->class->DB_table.'` WHERE '.implode(' AND ',$cond), false, $params);
181+
if (is_array($mixedConditions) && count($mixedConditions) > 0) {
182+
$cond = array();
183+
foreach ($mixedConditions as $var => $value){
184+
$pk = 'p_'.(count($params)+1);
185+
$val = $value;
186+
if( is_array( $value ) ) {
187+
//forme [var, [op, value]]
188+
$cond[] = 'te.`'.$dbequiv[$var].'` '.$value[0].($value[1] !== null? ' \'{{'.$pk.'}}\'' : ' NULL');
189+
$val = $value[1];
190+
}
191+
else {
192+
//forme [var, value] === [var, ["=", value] ]
193+
$cond[] = 'te.`'.$dbequiv[$var].'` = '.($value!==null?'\'{{'.$pk.'}}\'':' NULL');
194+
}
195+
$params[$pk] = $val;
196+
}
197+
$conditions = 'WHERE '.implode(' AND ',$cond);
198+
}
199+
$join = " $join ";
200+
$res = SQL::getInstance()->exec('SELECT COUNT(*) as nombre FROM `'.TABLE_PREFIX.$this->class->DB_table.'` as te '.$join.$conditions, false, $params);
173201
if($res) { //cas ou aucun retour requete (retour FALSE)
174202
$all = 0;
175203
foreach ($res as $row) {

core/SQL.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ private function _exec($requete, $params = array()) {
130130
}
131131
//$this->log->log('sql', 'info_sql', '"' . $this->getLastQuery() . '"\n escaped in "' . $requete . '"', Logger::GRAN_MONTH);
132132
}
133-
133+
if (true) {
134+
$this->log->log('sql', 'debug_sql', $this->getLastQuery(), Logger::GRAN_MONTH);
135+
$this->log->log('sql', 'debug_sql', $requete, Logger::GRAN_MONTH);
136+
}
134137
//on fait la requete
135138
$rep = mysqli_query($this->mysql_c, $requete);
136139
//debug($rep);

core/Site.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function __construct( $controller = null, $action = null )
138138
}
139139
}
140140
catch (Exception $e) {
141-
throw new Exception("Une erreur est survenue : " . $e->getMessage());
141+
throw new Exception("Une erreur est survenue : " . $e->getMessage(), 500, $e);
142142
}
143143

144144
}
@@ -258,6 +258,14 @@ public function getMessageInfos($impl = '<br/>') {
258258
return implode($impl,$this->infos);
259259
}
260260

261+
/**
262+
* Ajoute du contenu dans le titre de la page
263+
* @param string|string[] contenu
264+
*/
265+
public function addTitle($content, $end=true) {
266+
$this->addElement('title', $content, $end);
267+
}
268+
261269
/**
262270
* Ajoute du contenu dans un element
263271
* @param string $var (title, head, content, foot, menu) l'element

functions.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ function toBytes($str){
237237
}
238238
return $val;
239239
}
240+
241+
function human_filesize($bytes, $decimals = 2) {
242+
$sz = ' KMGTP';
243+
$factor = floor((strlen($bytes) - 1) / 3);
244+
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor] . 'o';
245+
}
246+
240247
/**
241248
* Cr�e une nouvelle image redimentionn�e � partir de $urlImage
242249
* @param String $urlImage adresse locale de l'image d'origine
@@ -246,6 +253,14 @@ function toBytes($str){
246253
* @return String url de la nouvelle image, par defaut de la forme {basename($urlImage)}.min{resizeType}{resizeSize}.{extention} ou false en cas d'erreur
247254
*/
248255
function redimJPEG($urlImage,$resizeSize = 500, $resizeType = 'H', $newUrl = 'default'){
256+
if (exif_imagetype($urlImage) == IMAGETYPE_PNG) {
257+
$im = imagecreatefrompng($urlImage);
258+
$urlImage .= '.jpeg';
259+
imagejpeg($im, $urlImage);
260+
if ($newUrl != 'deafult') {
261+
$newUrl .= '.jpeg';
262+
}
263+
}
249264
if($imageOrigine = imagecreatefromjpeg($urlImage) or die('erreur')){
250265
$tailleOrigine = getimagesize($urlImage, $info);
251266
if($resizeType == 'L'){
@@ -414,7 +429,7 @@ function getBBCodeFromOneAlbum(Bdmap_Album $album, $photoEncapsuleUrl = false, $
414429
return $bbcode;
415430
}
416431

417-
function __autoload($className){
432+
function autoload($className){
418433
$class = str_replace('_', '/', $className);
419434
$classArray = explode("/", $class);
420435
$newClassArray = array();
@@ -446,9 +461,11 @@ function __autoload($className){
446461
elseif ( file_exists( BPCF.'/'.$classPath.'.php' ) ) {
447462
require_once BPCF.'/'.$classPath.'.php';
448463
}
449-
// echo $className . " imported<br/>";
464+
// echo $className . " imported from '$classPath'\n";
450465
}
451466

467+
spl_autoload_register('autoload');
468+
452469
function emu_getallheaders() {
453470
foreach ($_SERVER as $name => $value)
454471
{

0 commit comments

Comments
 (0)