1.
<?php
2.
3.
/*
4.
Généalogik
5.
6.
Class for table : documenttype.
7.
8.
07/03/2021 10:45
9.
10.
lvardon@laposte.net - 2021
11.
12.
Licence libre
13.
14.
Usage examples :
15.
16.
<?php
17.
18.
include_once("classdir/dbconnect.php);
19.
include_once("classdir/class_documenttype.php);
20.
21.
// Class instance
22.
$mydocumenttype = documenttype( $db );
23.
24.
// Select all reccords :
25.
$results = $mydocumenttype->selectAll();
26.
foreach($results as $row) {
27.
print_r($row);
28.
}
29.
30.
// Select reccord #12 :
31.
$id = 12;
32.
$row = $mydocumenttype->select($id);
33.
print_r($row);
34.
35.
?>
36.
37.
*/
38.
class _documenttype {
39.
40.
public $db;
41.
public $lasterror="";
42.
public $lastInsertId=-1;
43.
public $count = 0;
44.
45.
/*
46.
Initialise object with dbHanler.
47.
48.
*/
49.
function __construct( $db ) {
50.
$this->db = $db;
51.
}
52.
53.
/*
54.
Select one reccord from documenttype.
55.
56.
parameters : $id (reccord id).
57.
return : associative array of reccord.
58.
*/
59.
function select($id) {
60.
61.
$this->lasterror="";
62.
63.
$sqlSt="
64.
select
65.
a.id as 'a.id',
66.
a.name as 'a.name',
67.
a.code as 'a.code',
68.
a.description as 'a.description',
69.
a.icon as 'a.icon'
70.
71.
72.
from documenttype a
73.
74.
where a.id = :id;
75.
";
76.
77.
try {
78.
79.
$stmt = $this->db->prepare($sqlSt);
80.
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
81.
$stmt->execute();
82.
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
83.
return $results;
84.
85.
} catch (Exception $e) {
86.
$this->lasterror = $e->getMessage().' (<b>Id</b> = '.$idkeyname.')';
87.
return false;
88.
}
89.
}
90.
91.
function selectPrev($id) {
92.
93.
$this->lasterror="";
94.
95.
$sqlSt="
96.
select
97.
a.id as 'a.id',
98.
a.name as 'a.name',
99.
a.code as 'a.code',
100.
a.description as 'a.description',
101.
a.icon as 'a.icon'
102.
103.
104.
from documenttype a
105.
106.
where a.id = (SELECT id FROM documenttype WHERE id < :id ORDER BY id DESC LIMIT 1) ;
107.
";
108.
109.
try {
110.
111.
$stmt = $this->db->prepare($sqlSt);
112.
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
113.
$stmt->execute();
114.
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
115.
return $results;
116.
117.
} catch (Exception $e) {
118.
$this->lasterror = $e->getMessage().' (<b>Id</b> = '.$idkeyname.')';
119.
return false;
120.
}
121.
}
122.
123.
124.
function selectNext($id) {
125.
126.
$this->lasterror="";
127.
128.
$sqlSt="
129.
select
130.
a.id as 'a.id',
131.
a.name as 'a.name',
132.
a.code as 'a.code',
133.
a.description as 'a.description',
134.
a.icon as 'a.icon'
135.
136.
137.
from documenttype a
138.
139.
where a.id = (SELECT id FROM documenttype WHERE id > :id ORDER BY id ASC LIMIT 1) ;
140.
";
141.
142.
try {
143.
144.
$stmt = $this->db->prepare($sqlSt);
145.
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
146.
$stmt->execute();
147.
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
148.
return $results;
149.
150.
} catch (Exception $e) {
151.
$this->lasterror = $e->getMessage().' (<b>Id</b> = '.$idkeyname.')';
152.
return false;
153.
}
154.
}
155.
156.
/*
157.
Select all reccords from documenttype (with default order).
158.
159.
parameters : None
160.
return : associative array of reccords.
161.
*/
162.
function selectAll() {
163.
164.
$this->lasterror="";
165.
166.
$sqlSt="
167.
select
168.
a.id as 'a.id',
169.
a.name as 'a.name',
170.
a.code as 'a.code',
171.
a.description as 'a.description',
172.
a.icon as 'a.icon'
173.
174.
175.
from documenttype a
176.
177.
;
178.
";
179.
180.
try {
181.
182.
$stmt = $this->db->prepare($sqlSt);
183.
$stmt->execute();
184.
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
185.
return $results;
186.
187.
} catch (Exception $e) {
188.
$this->lasterror = $e->getMessage();
189.
$this->count = 0;
190.
return false;
191.
}
192.
}
193.
194.
/*
195.
Update documenttype
196.
197.
parameters : $id (reccord id) and fields list values.
198.
return : True if no error.
199.
*/
200.
function update( $id, $name,$code,$description,$icon ) {
201.
202.
$this->lasterror="";
203.
204.
if ($name == '') $name = null;
205.
if ($code == '') $code = null;
206.
if ($description == '') $description = null;
207.
if ($icon == '') $icon = null;
208.
209.
210.
if ($this->checkValue( array(
211.
$name => '',
212.
$code => '',
213.
$description => '',
214.
$icon => ''
215.
216.
) ) == false )
217.
{
218.
$this->lasterror="Check fields false";
219.
return false;
220.
}
221.
222.
$sqlSt="
223.
update documenttype set
224.
name = :name,
225.
code = :code,
226.
description = :description,
227.
icon = :icon
228.
229.
where id = :id;
230.
";
231.
232.
try {
233.
234.
$stmt = $this->db->prepare($sqlSt);
235.
236.
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
237.
238.
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
239.
$stmt->bindParam(':code', $code, PDO::PARAM_STR);
240.
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
241.
$stmt->bindParam(':icon', $icon, PDO::PARAM_STR);
242.
243.
$stmt->execute();
244.
245.
if ($stmt->rowCount() == 0) {
246.
$this->lasterror = 'Erreur Sql update '.' (<b>Id</b> = '.$id.'). <b>Sqlst</b> = ['.$sqlSt.'] values : '."<b>name</b> = [$name],<b>code</b> = [$code],<b>description</b> = [$description],<b>icon</b> = [$icon]";
247.
248.
return false;
249.
}
250.
251.
return true;
252.
253.
} catch (Exception $e) {
254.
$this->lasterror = $e->getMessage().' (<b>Id</b> = '.$id.'). <b>Sqlst</b> = ['.$sqlSt.'] values : '."<b>name</b> = [$name],<b>code</b> = [$code],<b>description</b> = [$description],<b>icon</b> = [$icon]";
255.
return false;
256.
}
257.
258.
}
259.
260.
/*
261.
Insert new reccord into documenttype.
262.
263.
Parameters : fields values list.
264.
return : True if no error.
265.
*/
266.
function insert( $name,$code,$description,$icon ) {
267.
268.
$this->lasterror="";
269.
$this->lastInsertId = -1;
270.
271.
if ($name == '') $name = null;
272.
if ($code == '') $code = null;
273.
if ($description == '') $description = null;
274.
if ($icon == '') $icon = null;
275.
276.
277.
if ($this->checkValue( array(
278.
$name => '',
279.
$code => '',
280.
$description => '',
281.
$icon => ''
282.
283.
) ) == false )
284.
{
285.
$this->lasterror="Check fields error";
286.
return false;
287.
}
288.
289.
$sqlSt="
290.
insert into documenttype (
291.
name,
292.
code,
293.
description,
294.
icon
295.
296.
)
297.
values (
298.
:name,
299.
:code,
300.
:description,
301.
:icon
302.
303.
);
304.
";
305.
306.
try {
307.
308.
$stmt = $this->db->prepare($sqlSt);
309.
310.
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
311.
$stmt->bindParam(':code', $code, PDO::PARAM_STR);
312.
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
313.
$stmt->bindParam(':icon', $icon, PDO::PARAM_STR);
314.
315.
316.
$stmt->execute();
317.
$this->lastInsertId = $this->db->lastInsertId();
318.
319.
if ($stmt->rowCount() == 0) {
320.
$this->lasterror = 'Erreur Sql insert'.' (<b>Id</b> = '.$id.'). <b>Sqlst</b> = ['.$sqlSt.'] values : '."<b>name</b> = [$name],<b>code</b> = [$code],<b>description</b> = [$description],<b>icon</b> = [$icon]";
321.
return false;
322.
}
323.
324.
return true;
325.
326.
} catch (Exception $e) {
327.
$this->lasterror = $e->getMessage().' (<b>Id</b> = '.$id.'). <b>Sqlst</b> = ['.$sqlSt.'] values : '."<b>name</b> = [$name],<b>code</b> = [$code],<b>description</b> = [$description],<b>icon</b> = [$icon]";
328.
return false;
329.
}
330.
}
331.
332.
/*
333.
Delete one reccord from documenttype.
334.
335.
parameters : $id (reccord id).
336.
return : True if no error.
337.
*/
338.
function delete($id) {
339.
340.
$this->lasterror="";
341.
342.
$sqlSt="
343.
delete from documenttype
344.
where id = :id;
345.
";
346.
347.
try {
348.
349.
$stmt = $this->db->prepare($sqlSt);
350.
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
351.
$stmt->execute();
352.
353.
if ($stmt->rowCount() == 0) {
354.
$this->lasterror = "Erreur Sql delete ".' (<b>Id</b> ='.$id.')';
355.
return false;
356.
}
357.
358.
} catch (Exception $e) {
359.
$this->lasterror = $e->getMessage().' (<b>Id</b>='.$id.')';
360.
return false;
361.
}
362.
return true;
363.
}
364.
365.
/*
366.
Get id min, id max, record count from documenttype
367.
368.
parameters : None
369.
return : count value or False in case of error
370.
*/
371.
function getCount() {
372.
373.
$this->lasterror="";
374.
375.
$sqlSt="
376.
select min(id) as minid, max(id) as maxid, count(*) as count
377.
from documenttype
378.
";
379.
380.
try {
381.
382.
$stmt = $this->db->prepare($sqlSt);
383.
$stmt->execute();
384.
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
385.
return $results[0];
386.
387.
} catch (Exception $e) {
388.
$this->lasterror = $e->getMessage();
389.
return array( 'minid' => -1, 'maxid' => -1, 'count' => -1);
390.
}
391.
392.
//return $this->count;
393.
return $results[0];
394.
}
395.
396.
397.
398.
399.
public $colmeta = [
400.
'a.id' => array (
401.
'metades' => "",
402.
'sortformat' => "{{sortformat}}",
403.
'listlen' => 4,
404.
'listlib' => 'id',
405.
'listlink' => 'on',
406.
'selectlen' => 4,
407.
'selectlib' => 'id',
408.
'selectlink' => 'on',
409.
'selectdisplay' => '',
410.
'selectdisplaywidth' => '0',
411.
'selectdisplayheight' => '0',
412.
'listdisplay' => '',
413.
'listdisplaywidth' => '0',
414.
'listdisplayheight' => '0'
415.
416.
),
417.
'a.name' => array (
418.
'metades' => "",
419.
'sortformat' => "",
420.
'listlen' => 12,
421.
'listlib' => 'name' ,
422.
'listlink' => '',
423.
'selectlen' => 12,
424.
'selectlib' => 'name' ,
425.
'selectlink' => '',
426.
'selectdisplay' => 'text',
427.
'selectdisplaywidth' => '0',
428.
'selectdisplayheight' => '0',
429.
'listdisplay' => 'text',
430.
'listdisplaywidth' => '0',
431.
'listdisplayheight' => '0'
432.
),
433.
'a.code' => array (
434.
'metades' => "",
435.
'sortformat' => "",
436.
'listlen' => 12,
437.
'listlib' => 'code' ,
438.
'listlink' => '',
439.
'selectlen' => 12,
440.
'selectlib' => 'code' ,
441.
'selectlink' => '',
442.
'selectdisplay' => 'text',
443.
'selectdisplaywidth' => '0',
444.
'selectdisplayheight' => '0',
445.
'listdisplay' => 'text',
446.
'listdisplaywidth' => '0',
447.
'listdisplayheight' => '0'
448.
),
449.
'a.description' => array (
450.
'metades' => "",
451.
'sortformat' => "",
452.
'listlen' => 12,
453.
'listlib' => 'description' ,
454.
'listlink' => '',
455.
'selectlen' => 12,
456.
'selectlib' => 'description' ,
457.
'selectlink' => '',
458.
'selectdisplay' => 'text',
459.
'selectdisplaywidth' => '0',
460.
'selectdisplayheight' => '0',
461.
'listdisplay' => 'text',
462.
'listdisplaywidth' => '0',
463.
'listdisplayheight' => '0'
464.
),
465.
'a.icon' => array (
466.
'metades' => "",
467.
'sortformat' => "",
468.
'listlen' => 12,
469.
'listlib' => 'icon' ,
470.
'listlink' => '',
471.
'selectlen' => 12,
472.
'selectlib' => 'icon' ,
473.
'selectlink' => '',
474.
'selectdisplay' => 'text',
475.
'selectdisplaywidth' => '0',
476.
'selectdisplayheight' => '0',
477.
'listdisplay' => 'text',
478.
'listdisplaywidth' => '0',
479.
'listdisplayheight' => '0'
480.
)
481.
482.
483.
];
484.
485.
/*
486.
Get field property value
487.
488.
Parameters : field name and property name.
489.
return : value of property.
490.
*/
491.
492.
function getMeta( $fieldkey , $metakey ) {
493.
return $this->colmeta [$fieldkey][$metakey];
494.
}
495.
496.
/*
497.
Get labels for list table header.
498.
499.
Parameters : None.
500.
return : String.
501.
*/
502.
503.
function getTableHeaderList() {
504.
505.
$labelsStr ='';
506.
507.
foreach ( $this->colmeta as $k => $v) {
508.
if ( $v['listlen'] > 0 )
509.
$labelsStr = $labelsStr.'<th title="'.$v['metades'].'" >'.$v['listlib'] .'</th>'."\n";
510.
}
511.
return $labelsStr;
512.
}
513.
514.
function getTableFieldLenList( $field ) {
515.
516.
return $this->colmeta[$field]['listlen'];
517.
}
518.
519.
function getTableFieldLinkList( $field ) {
520.
521.
return $this->colmeta[$field]['listlink'];
522.
}
523.
524.
/*
525.
Get labels for select table header.
526.
527.
Parameters : None.
528.
return : String.
529.
*/
530.
531.
function getTableHeaderSelect() {
532.
533.
$labelsStr ='';
534.
535.
foreach ( $this->colmeta as $k => $v) {
536.
if ( $v['selectlen'] > 0 )
537.
$labelsStr = $labelsStr.'<th title="'.$v['metades'].'" >'.$v['selectlib'] .'</th>'."\n";
538.
}
539.
return $labelsStr;
540.
}
541.
542.
function getTableFieldLenSelect( $field ) {
543.
544.
return $this->colmeta[$field]['selectlen'];
545.
}
546.
547.
function getTableFieldLinkSelect( $field ) {
548.
549.
return $this->colmeta[$field]['selectlink'];
550.
}
551.
552.
553.
/*
554.
Check user input values for documenttype.
555.
556.
Parameters : array of fields with regexp paterns. ex: array( $nom =>'^(?!\s*$).+' , $icone => '')
557.
return : False if at last one regexp does not match.
558.
*/
559.
function checkValue( $arrayCheck ) {
560.
561.
$indexfield=1;
562.
foreach ( $arrayCheck as $value => $pattern) {
563.
564.
if ($pattern=='') continue;
565.
566.
$pattern='/'.$pattern.'/';
567.
568.
if (!preg_match($pattern, $value)) {
569.
$this->lasterror = "Erreur champ #".$indexfield. '. Valeur:['.$value.']';
570.
return false;
571.
}
572.
573.
$indexfield+=1;
574.
575.
}
576.
577.
return true;
578.
}
579.
580.
} // end class
581.
582.
?>
583.