Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Stars Field for Module Builder #31216

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions htdocs/core/class/commonobject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7422,6 +7422,10 @@ public function showInputField($val, $key, $value, $moreparam = '', $keysuffix =
} elseif (preg_match('/varchar/', $val['type'])) {
$param['options'] = array();
$type = 'varchar';
} elseif (preg_match('/stars\((\d+)\)/', $val['type'], $reg)) {
$param['options'] = array();
$type = 'stars';
$size = $reg[1];
} else {
$param['options'] = array();
$type = $this->fields[$key]['type'];
Expand Down Expand Up @@ -7613,6 +7617,44 @@ function handlemultiinputdisabling(htmlname){
$value = price($value);
}
$out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.$value.'" '.($moreparam ? $moreparam : '').'> '.$langs->getCurrencySymbol($conf->currency);
} elseif ($type == 'stars') {
$out = '<input type="hidden" class="flat '.$morecss.'" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.dol_escape_htmltag($value).'"'.($moreparam ? $moreparam : '').($autofocusoncreate ? ' autofocus' : '').'>';
$out .= '<div class="star-selection">';
$i = 1;
while ($i <= $size) {
$out .= '<span class="star" data-value="'.$i.'">'.img_picto('', 'fontawesome_star_fas').'</span>';
$i++;
}
$out .= '</div>';
$out .= '<script>
$(document).ready(function() {
let selectedStars = parseInt($("#'.$keyprefix.$key.$keysuffix.'").val()) || 0;
$(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStars);
});
$(".star").on("mouseover", function() {
let selectedStar = $(this).data("value");
$(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStar);
});
});
$(".star-selection").on("mouseout", function() {
$(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStars);
});
});
$(".star").on("click", function() {
selectedStars = $(this).data("value");
if (selectedStars == 1 && $("#'.$keyprefix.$key.$keysuffix.'").val() == 1) {
selectedStars = 0;
}
$("#'.$keyprefix.$key.$keysuffix.'").val(selectedStars);
$(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStars);
});
});
});
</script>';
} elseif (preg_match('/^double(\([0-9],[0-9]\)){0,1}/', (string) $type)) {
if (!empty($value)) { // $value in memory is a php numeric, we format it into user number format.
$value = price($value);
Expand Down Expand Up @@ -8278,6 +8320,10 @@ public function showOutputField($val, $key, $value, $moreparam = '', $keysuffix
} elseif (preg_match('/^chkbxlst:(.*)/i', $val['type'], $reg)) {
$param['options'] = array($reg[1] => 'N');
$type = 'chkbxlst';
} elseif (preg_match('/stars\((\d+)\)/', $val['type'], $reg)) {
$param['options'] = array();
$type = 'stars';
$size = $reg[1];
}

$langfile = empty($val['langfile']) ? '' : $val['langfile'];
Expand Down Expand Up @@ -8365,6 +8411,62 @@ public function showOutputField($val, $key, $value, $moreparam = '', $keysuffix
$value = dol_print_phone($value, '', 0, 0, '', '&nbsp;', 'phone');
} elseif ($type == 'ip') {
$value = dol_print_ip($value, 0);
} elseif ($type == 'stars') {
$value = '<input type="hidden" class="flat '.$morecss.'" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.$this->id.'" value="'.dol_escape_htmltag($value).'"'.($moreparam ? $moreparam : '').'>';
$value .= '<div class="star-selection" id="'.$keyprefix.$key.$keysuffix.$this->id.'_selection">';
$i = 1;
while ($i <= $size) {
$value .= '<span class="star" data-value="'.$i.'">'.img_picto('', 'fontawesome_star_fas').'</span>';
$i++;
}
$value .= '</div>';
$value .= '<script>
$(document).ready(function() {
let container = $("#'.$keyprefix.$key.$keysuffix.$this->id.'_selection");
let selectedStars = parseInt($("#'.$keyprefix.$key.$keysuffix.$this->id.'").val()) || 0;
container.find(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStars);
});
container.find(".star").on("mouseover", function() {
let selectedStar = $(this).data("value");
container.find(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStar);
});
});
container.on("mouseout", function() {
container.find(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStars);
});
});
container.find(".star").off("click").on("click", function() {
selectedStars = $(this).data("value");
if (selectedStars == 1 && $("#'.$keyprefix.$key.$keysuffix.$this->id.'").val() == 1) {
selectedStars = 0;
}
container.find("#'.$keyprefix.$key.$keysuffix.$this->id.'").val(selectedStars);
container.find(".star").each(function() {
$(this).toggleClass("active", $(this).data("value") <= selectedStars);
});
$.ajax({
url: "ajax/'.$this->element.'.php",
method: "POST",
data: {
objectId: "'.$this->id.'",
field: "'.$keyprefix.$key.$keysuffix.'",
value: selectedStars,
token: "'.newToken().'"
},
success: function(response) {
var res = JSON.parse(response);
console[res.status === "success" ? "log" : "error"](res.message);
},
error: function(xhr, status, error) {
console.log("Ajax request failed while updating '.$keyprefix.$key.$keysuffix.':", error);
}
});
});
});
</script>';
} elseif ($type == 'price') {
if (!is_null($value) && $value !== '') {
$value = price($value, 0, $langs, 0, 0, -1, $conf->currency);
Expand Down
1 change: 1 addition & 0 deletions htdocs/core/lib/functions.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4895,6 +4895,7 @@ function getPictoForType($key, $morecss = '')
'ip' => 'country',
'select' => 'list',
'sellist' => 'list',
'stars' => 'fontawesome_star_fas',
'radio' => 'check-circle',
'checkbox' => 'list',
'chkbxlst' => 'list',
Expand Down
11 changes: 10 additions & 1 deletion htdocs/core/lib/modulebuilder.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Label")), null, 'errors');
return -2;
}
if (!preg_match('/^(integer|price|sellist|varchar|double|text|html|duration)/', $addfieldentry['type'])
if (!preg_match('/^(integer|price|sellist|varchar|double|text|html|duration|stars)/', $addfieldentry['type'])
&& !preg_match('/^(boolean|smallint|real|date|datetime|timestamp|phone|mail|url|ip|password)$/', $addfieldentry['type'])) {
setEventMessages($langs->trans('BadValueForType', $addfieldentry['type']), null, 'errors');
return -2;
}
// Check for type stars(NumberOfStars), NumberOfStars must be an integer between 1 and 10
if (preg_match('/^stars\((.+)\)$/', $addfieldentry['type'], $matches)) {
if (!ctype_digit($matches[1]) || $matches[1] < 1 || $matches[1] > 10) {
setEventMessages($langs->trans('BadValueForType', $addfieldentry['type']), null, 'errors');
return -2;
}
}
}

$pathoffiletoeditsrc = $readdir.'/class/'.strtolower($objectname).'.class.php';
Expand Down Expand Up @@ -343,6 +350,8 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '
$type = 'integer';
} elseif ($type == 'mail') {
$type = 'varchar(128)';
} elseif (strpos($type, 'stars(') === 0) {
$type = 'integer';
} elseif ($type == 'phone') {
$type = 'varchar(20)';
} elseif ($type == 'ip') {
Expand Down
2 changes: 1 addition & 1 deletion htdocs/langs/en_US/modulebuilder.lang
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ CSSListClass=CSS for list
NotEditable=Not editable
ForeignKey=Foreign key
ForeignKeyDesc=If the value of this field must be guaranteed to exist into another table. Enter here a value matching syntax: tablename.parentfieldtocheck
TypeOfFieldsHelp=Example:<br>varchar(99)<br>email<br>phone<br>ip<br>url<br>password<br>double(24,8)<br>real<br>text<br>html<br>date<br>datetime<br>timestamp<br>integer<br>integer:ClassName:relativepath/to/classfile.class.php[:1[:filter]]<br><br>'1' means we add a + button after the combo to create the record<br>'filter' is an Universal Filter syntax condition, example: '((status:=:1) AND (fk_user:=:__USER_ID__) AND (entity:IN:(__SHARED_ENTITIES__))'
TypeOfFieldsHelp=Example:<br>varchar(99)<br>email<br>phone<br>ip<br>url<br>password<br>double(24,8)<br>real<br>text<br>html<br>date<br>datetime<br>timestamp<br>stars(NumberOfStars) NumberOfStars must be between 1 and 10.<br>integer<br>integer:ClassName:relativepath/to/classfile.class.php[:1[:filter]]<br><br>'1' means we add a + button after the combo to create the record<br>'filter' is an Universal Filter syntax condition, example: '((status:=:1) AND (fk_user:=:__USER_ID__) AND (entity:IN:(__SHARED_ENTITIES__))'
TypeOfFieldsHelpIntro=This is the type of the field/attribute.
AsciiToHtmlConverter=Ascii to HTML converter
AsciiToPdfConverter=Ascii to PDF converter
Expand Down
3 changes: 3 additions & 0 deletions htdocs/modulebuilder/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4083,6 +4083,7 @@ function getLicenceHeader($user, $langs, $now)
print '<option>date</option>';
print '<option>datetime</option>';
print '<option>integer</option>';
print '<option>stars(5)</option>';
print '<option>double(28,4)</option>';
print '<option>real</option>';
print '<option>integer:ClassName:RelativePath/To/ClassFile.class.php[:1[:FILTER]]</option>';
Expand Down Expand Up @@ -4650,6 +4651,8 @@ function cleanString( stringtoclean )
$pictoType = 'datetime';
} elseif (strpos($proptype, 'real') === 0) {
$pictoType = 'double';
} elseif (strpos($proptype, 'stars') === 0) {
$pictoType = 'stars';
}
print(!empty($pictoType) ? getPictoForType($pictoType) : getPictoForType($proptype)).'<span title="'.dol_escape_htmltag($proptype).'">'.dol_escape_htmltag($proptype).'</span>';
print '</td>';
Expand Down
12 changes: 12 additions & 0 deletions htdocs/theme/eldy/global.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5142,6 +5142,18 @@
padding: 16px;
}

.star-selection {
font-size: 1rem;
cursor: pointer;
display: flex;
}
.star {
color: #ccc;
transition: color 0.4s;
}
.star:hover, .star.active {
color: <?php echo $badgeWarning ?>;
}

/*
* Ok, Warning, Error
Expand Down
12 changes: 12 additions & 0 deletions htdocs/theme/md/style.css.php
Original file line number Diff line number Diff line change
Expand Up @@ -5154,6 +5154,18 @@
border: 1px solid #888;
padding: 16px;
}
.star-selection {
font-size: 1rem;
cursor: pointer;
display: flex;
}
.star {
color: #ccc;
transition: color 0.4s;
}
.star:hover, .star.active {
color: <?php echo $badgeWarning ?>;
}



Expand Down
Loading