added event onFieldDisplaying on the cards

This commit is contained in:
Alessandro Ferro 2022-03-09 12:10:56 +01:00
parent 7315c942a5
commit b76a67562e
3 changed files with 48 additions and 6 deletions

View File

@ -49,9 +49,18 @@
$card2->addElementToList("of various sentences");
$card2->addLink("Link1", "#");
$card2->addLink("Link2", "#");
$card2->onFieldDisplaying("onFieldDisplaying");
$card2->draw();
Card::endCardGroupLayout();
function onFieldDisplaying(&$field , &$value){
if($field == "Inhabitants"){
$value .= " (census 2019)";
}
}
?>
</body>
</html>

View File

@ -2,6 +2,8 @@
include "include.php";
class Card{
// Structure
private $imageSource;
private $title;
private $subtitle;
@ -12,6 +14,11 @@
private $list;
private $linksArray;
private $button;
// Events
private $onFieldDisplayingFunctionName;
// Layout
private $borderColor;
private $cardColor;
private $textColor;
@ -140,10 +147,14 @@
echo '<div class="card-body">';
echo '<h5 class="card-title">'. $this->title .'</h5>'; // title
echo '<h6 class="card-subtitle mb-2 text-muted">'. $this->subtitle .'</h6>'; // subtitle
echo '<p class="card-text">'. $this->innerText . '</p>'; // text
echo '<p class="card-text">'. $this->innerText . '</p>'; // innertext
if(isset($this->fieldsArray)){ // fields
// fields
if(isset($this->fieldsArray)){
foreach($this->fieldsArray as $field => $value){
if(isset($this->onFieldDisplayingFunctionName)){
call_user_func_array($this -> onFieldDisplayingFunctionName , array(&$field, &$value));
}
echo '<p class="card-text"><b>' . $field . ': </b>' . $value . '</p>';
}
}
@ -174,6 +185,23 @@
echo '</div></div>';
}
/*
** The Event "onFieldDisplaying" gets fired just before the displaying of a field within
** a card, so you can display your own value.
** The function you have to provide must have this signature:
**
** function myFunction(&$field , &$value){...}
**
** You can change "field" snd "value" to whichever name you want.
**
*/
function onFieldDisplaying($functionName){
if(!is_callable($functionName))
throw new InvalidArgumentException("Couldn't call $functionName. You must provide the name of a function.");
$this -> onFieldDisplayingFunctionName = $functionName;
}
/*
** Using this overrides the card's width (known error from Bootstrap), so you should specify a width.
** It always has to be closed with endCardGroupLayout()

View File

@ -2,6 +2,7 @@
include "include.php";
class Table{
// Structure
private $dataSource;
private $columnCaptions;
@ -57,17 +58,21 @@
}
/*
** Datasource can be either an array of object(s) or
** an array of array(s).
*/
function setDataSource(Array $dataSource){
if(empty($dataSource))
throw new InvalidArgumentException('Parameter cannot be empty.');
$this->dataSource = $dataSource;
// if array of objects provided
// Set captions when array of objects provided
if(is_object($dataSource[0]))
$this->columnCaptions = array_keys(get_object_vars($this->dataSource[0]));
// if array of arrays provided
// Set captione when array of arrays provided
else if(is_array($dataSource[0]))
$this->columnCaptions = array_keys($this->dataSource[0]);
}
@ -107,12 +112,12 @@
/*
** The Event "onValueDisplaying" gets fired just before the displaying of a value within
** a table body, so you can display your own value if you want to.
** a table body, so you can display your own value.
** The function you have to provide must have this signature:
**
** function myFunction($caption, &$value, $row){...}
**
** You can change "caption" and "value" to whichever name you want.
** You can change "caption", "value" and "row" to whichever name you want.
**
** caption: the ACTUAL fieldname of the dataSource whose value belongs to
** value: the value you want to display.