Skip to content

Commit

Permalink
Merge branch 'better-detection-of-creation-mode' into doryphore-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
J9rem committed Jul 13, 2022
2 parents 5c7fe74 + 0d633e8 commit 33b6843
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tools/attach/handlers/UpdateHandler__.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function extractImages(array $page): bool
$updated = false;
foreach ($form['prepared'] as $field) {
if ($field instanceof TextareaField && !empty($entry[$field->getPropertyName()])) {
$newValue = $field->formatValuesBeforeSave($entry);
$newValue = $field->formatValuesBeforeSaveIfEditable($entry);
if (isset($newValue[$field->getPropertyName()])) {
$oldValue = json_encode($entry[$field->getPropertyName()]);
$newValue = json_encode($newValue[$field->getPropertyName()]);
Expand Down
12 changes: 9 additions & 3 deletions tools/bazar/fields/BazarField.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ public function renderStaticIfPermitted($entry, ?string $userNameForRendering =
public function renderInputIfPermitted($entry)
{
// Safety checks, must be run before every renderInput
if (!$this->canEdit($entry)) {
if (!$this->canEdit($entry, !$entry)) {
return '';
}

return $this->renderInput($entry);
}

public function formatValuesBeforeSaveIfEditable($entry, bool $isCreation = false)
{
// this method is defined to check $this->canEdit with $isCreation
// without changing signature of formatValuesBeforeSave()
return $this->formatValuesBeforeSave($entry);
}

// Format input values before save
public function formatValuesBeforeSave($entry)
{
Expand Down Expand Up @@ -145,10 +152,9 @@ public function canRead($entry, ?string $userNameForRendering = null)
}

/* Return true if we are if editing is allowed for the field */
public function canEdit($entry)
public function canEdit($entry, bool $isCreation = false)
{
$writeAcl = empty($this->writeAccess) ? '' : $this->writeAccess;
$isCreation = !$entry;
return empty($writeAcl) || $this->getService(AclService::class)->check($writeAcl, null, true, $isCreation ? '' : $entry['id_fiche'], $isCreation ? 'creation' : 'edit');
}

Expand Down
7 changes: 6 additions & 1 deletion tools/bazar/fields/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ public function getValues($entry)

public function formatValuesBeforeSave($entry)
{
if ($this->canEdit($entry)) {
return $this->formatValuesBeforeSaveIfEditable($entry, false);
}

public function formatValuesBeforeSaveIfEditable($entry, bool $isCreation = false)
{
if ($this->canEdit($entry, $isCreation)) {
// get value
$checkboxField = $entry[$this->propertyName] ?? null ;
// detect if from Form to check if clean field
Expand Down
8 changes: 6 additions & 2 deletions tools/bazar/fields/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,14 @@ function geocodedmarkerRefresh( point )
'longitude' => is_array($value) && !empty($value[$this->getLongitudeField()]) ? $value[$this->getLongitudeField()] : null
]);
}

public function formatValuesBeforeSave($entry)
{
if (!$this->canEdit($entry)) {
return $this->formatValuesBeforeSaveIfEditable($entry, false);
}

public function formatValuesBeforeSaveIfEditable($entry, bool $isCreation = false)
{
if (!$this->canEdit($entry, $isCreation)) {
// retrieve value from value because redefined with right value
$values = $this->getValue($entry);
if (empty($values)) {
Expand Down
7 changes: 6 additions & 1 deletion tools/bazar/fields/PasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public function __construct(array $values, ContainerInterface $services)
}

public function formatValuesBeforeSave($entry)
{
return $this->formatValuesBeforeSaveIfEditable($entry, false);
}

public function formatValuesBeforeSaveIfEditable($entry, bool $isCreation = false)
{
$value = $this->getValue($entry);
if ($this->canEdit($entry)) {
if ($this->canEdit($entry, $isCreation)) {
if (!empty($value)) {
// If a new password has been set, encode it
return [$this->propertyName => md5($value),
Expand Down
2 changes: 1 addition & 1 deletion tools/bazar/fields/TitleField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function formatValuesBeforeSave($entry)
$fieldValue = $field->getValue($entry);
if ($field instanceof CheckboxField) {
// get first value instead of keys
$formattedValue = $field->formatValuesBeforeSave($entry)[$field->getPropertyName()];
$formattedValue = $field->formatValuesBeforeSaveIfEditable($entry)[$field->getPropertyName()];
$fieldValues = $field->getValues([$field->getPropertyName() => $formattedValue]);
$replacement = $field->getOptions()[$fieldValues[0] ?? null] ?? '';
} elseif ($field instanceof TagsField) {
Expand Down
2 changes: 1 addition & 1 deletion tools/bazar/handlers/UpdateHandler__.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function extractOldCarto(array $entry): bool
if ($field instanceof MapField) {
// update location
$entry = array_merge($entry, $this->getMapFieldValue($field, $entry));
$tab = $field->formatValuesBeforeSave($entry);
$tab = $field->formatValuesBeforeSaveIfEditable($entry);
if (is_array($tab)) {
if (isset($tab['fields-to-remove']) and is_array($tab['fields-to-remove'])) {
foreach ($tab['fields-to-remove'] as $fieldName) {
Expand Down
13 changes: 7 additions & 6 deletions tools/bazar/services/EntryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function create($formId, $data, $semantic = false, $sourceUrl = null)

$this->validate($data);

$data = $this->formatDataBeforeSave($data);
$data = $this->formatDataBeforeSave($data, true);

// on change provisoirement d'utilisateur
if (isset($GLOBALS['utilisateur_wikini'])) {
Expand Down Expand Up @@ -610,7 +610,7 @@ public function update($tag, $data, $semantic = false, $replace = false)

$this->validate($data);

$data = $this->formatDataBeforeSave($data);
$data = $this->formatDataBeforeSave($data, false);

// get the sendmail and remove it before saving
$sendmail = $this->removeSendmail($data);
Expand Down Expand Up @@ -648,7 +648,7 @@ protected function assignRestrictedFields(array $data, array $previousData, arra
// be carefull : BazarField's objects, that do not save data (as ACL, Label, Hidden), do not have propertyName
// see BazarField->formatValuesBeforeSave() for details
// so do not save the previous data even if existing
if (!empty($propName) && !$field->canEdit($data)) {
if (!empty($propName) && !$field->canEdit($data, false)) {
$restrictedFields[] = $propName;
}
}
Expand Down Expand Up @@ -761,18 +761,19 @@ public function decode($body)
* prepare la requete d'insertion ou de MAJ de la fiche en supprimant
* de la valeur POST les valeurs inadequates et en formattant les champs.
* @param $data
* @param bool $isCreation
* @return array
* @throws Exception
*/
public function formatDataBeforeSave($data)
public function formatDataBeforeSave($data, bool $isCreation = false)
{
// not possible to init the formManager in the constructor because of circular reference problem
$form = $this->wiki->services->get(FormManager::class)->getOne($data['id_typeannonce']);

// If there is a title field, compute the entry's title
foreach ($form['prepared'] as $field) {
if ($field instanceof TitleField) {
$data = array_merge($data, $field->formatValuesBeforeSave($data));
$data = array_merge($data, $field->formatValuesBeforeSaveIfEditable($data, $isCreation));
}
}

Expand Down Expand Up @@ -803,7 +804,7 @@ public function formatDataBeforeSave($data)

foreach ($form['prepared'] as $bazarField) {
if ($bazarField instanceof BazarField) {
$tab = $bazarField->formatValuesBeforeSave($data);
$tab = $bazarField->formatValuesBeforeSaveIfEditable($data, $isCreation);
}

if (is_array($tab)) {
Expand Down

0 comments on commit 33b6843

Please sign in to comment.