Skip to content
This repository has been archived by the owner on Oct 12, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabea David committed Feb 21, 2017
2 parents eea4a17 + de3e9f5 commit 16cd385
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions ImageExtra.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* See README.md for usage instructions.
*
* @author Tabea David <[email protected]>
* @version 1.0.2
* @version 1.0.3
* @copyright Copyright (c) 2017
* @see https://github.com/justonestep/processwire-imageextra
* @see http://www.processwire.com
Expand All @@ -29,7 +29,7 @@ class ImageExtra extends WireData implements Module {
return array(
'title' => 'Image Extra',
'summary' => 'Adds custom fields to image fields (including multi-language support)',
'version' => 102,
'version' => 103,
'href' => 'https://github.com/justonestep/processwire-imageextra',
'singular' => true,
'autoload' => true,
Expand Down Expand Up @@ -86,7 +86,8 @@ class ImageExtra extends WireData implements Module {

if (in_array($this->page->name, array('edit', 'field', 'module', 'users')) || $backend === false) {
$this->addHookAfter('InputfieldImage::getConfigInputfields', $this, 'addExtraFields');
$this->addHookAfter('InputfieldImage::renderItem', $this, 'renderExtraFields');
$this->addHookAfter('InputfieldImage::renderItem', $this, 'manipulateDescriptionField');
$this->addHookAfter('InputfieldImage::renderAdditionalFields', $this, 'renderExtraFields');
$this->addHookBefore('ProcessField::executeSave', $this, 'checkTableColumnsAndParseSettings');

if (wire('page')->template->name === 'admin' && $this->input->get->id || wire('page')->template->name != 'admin') {
Expand Down Expand Up @@ -1038,60 +1039,68 @@ class ImageExtra extends WireData implements Module {
return $settings;
}

/**
* Manipulate descriotion field, add label
*
* @param HookEvent $event
*/
public function manipulateDescriptionField(HookEvent $event) {
if (!$event->object instanceof InputfieldImage) return;
$pagefile = $event->arguments[0];
$id = $event->arguments[1];
$n = $event->arguments[2];
$value = $event->object->attributes['value'];
$name = $value->field->name;
$data = $this->mergeData($name);
$out = $event->return;

// add label for description
// only if descriptionRows > 0 and multi-language site
if ($data['descriptionRows'] && !is_null($this->languages)) {
$label = "<strong>{$this->_('Description')}</strong>";
$out = preg_replace('/(<div\sclass=["\']InputfieldImageEdit__core["\']>)/', "$1{$label}", $out, 1);
}

// remove class detail, add font-weight bold
$out = preg_replace('/(<label\sfor\=["\']tags_.*["\']\s)(class\=["\']detail)/', '$1style="font-weight:bold;"', $out);

$event->return = $out;
}

/**
* render extra fields
*
* @param HookEvent $event
*/
public function renderExtraFields(HookEvent $event) {
if (!$event->object instanceof InputfieldImage) return;

$pagefile = $event->arguments[0];

$id = $event->arguments[1];
$n = $event->arguments[2];
$out = $event->return;
$value = $event->object->attributes['value'];
$name = $value->field->name;
$outAdditional = '';
$out = '';
$noLang = $event->object->noLang;
$settings = $this->getOtherFieldSettings($value->field);

$data = $this->mergeData($name);

if (!empty($this->additionalFields['other'])) {
if (array_key_exists($name, $this->additionalFields['other'])) {
foreach ($this->additionalFields['other'][$name] as $field) {
$outAdditional .= $this->renderInputItemField($pagefile, $id, $n, $field, $noLang, $settings);
$out .= $this->renderInputItemField($pagefile, $id, $n, $field, $noLang, $settings);
}
}
}

if ((int)$data['orientationField'] > 0 && !empty($data['orientationValues'])) {
$outAdditional .= $this->renderSelectItemField($pagefile, $id, $n, 'orientation', $data);
$out .= $this->renderSelectItemField($pagefile, $id, $n, 'orientation', $data);
}

if ((int)$data['linkField'] > 0) {
$outAdditional .= $this->renderLinkItemField($pagefile, $id, $n, 'link');
}

$splitOut = preg_split("/[\t]+/", $out);
$position = count($splitOut) - 2;

// add label for description
// only if descriptionRows > 0 and multi-language site
if ($data['descriptionRows'] && !is_null($this->languages)) {
$label = "<strong>{$this->_('Description')}</strong>";
$out = preg_replace('/(<div\sclass=["\']InputfieldImageEdit__core["\']>)/', "$1{$label}", $out, 1);
$out .= $this->renderLinkItemField($pagefile, $id, $n, 'link');
}

// remove class detail, add font-weight bold
$out = preg_replace('/(<label\sfor\=["\']tags_.*["\']\s)(class\=["\']detail)/', '$1style="font-weight:bold;"', $out);

// insert additional output
$out = preg_replace('/(<input\sclass=["\']InputfieldFileSort["\'])/', $this->escape_backreference($outAdditional) . '$1', $out);

$event->return = $out;
$event->return = $this->escape_backreference($out);
}

/**
Expand Down Expand Up @@ -1256,7 +1265,8 @@ class ImageExtra extends WireData implements Module {
$custom = is_null($userLanguage) || $userLanguage->isDefault() ? $current : $current . '__' . $userLanguage->id;
$labelPerLanguage = isset($settings->cf_label) && isset($settings->cf_label->{"cf_label__$custom"}) ? $settings->cf_label->{"cf_label__$custom"} : '';
$label = $labelPerLanguage ? $labelPerLanguage : ($labelDefault ? $labelDefault : $labelFallback);
$out = "<strong>{$label}</strong>{$field->render()}";
$name = strtolower($current);
$out = "<div class='InputfieldImageEdit__additional--{$name}'><strong>{$label}</strong>{$field->render()}</div>";

return $out;
}
Expand All @@ -1272,25 +1282,25 @@ class ImageExtra extends WireData implements Module {
*/
protected function renderSelectItemField(Pagefile $pagefile, $id, $n, $current, $data) {
$fieldName = $current . '_' . $id;
$name = strtolower($current);

$options = '';
foreach (explode(',', preg_replace('/\s+/', '', $data['orientationValues'])) as $option) {
$selected = ($pagefile->orientation === $option) ? 'selected' : '';
$options .= "<option value='$option' $selected>$option</option>";
}

$out = "<hr>" .
"<div class='InputfieldContent'>" .
$out = "<div class='InputfieldImageEdit__additional--{$name}'><hr />" .
"<label for='$fieldName' style='font-weight:bold;'>" . ucfirst($current) . "</label>" .
"<select id='$fieldName' name='$fieldName' size='1'>" . $options . "</select>" .
"</div>" .
"<script type='text/javascript'>
$('#$fieldName').on('focus, click', () => {
$('#$fieldName').closest('.gridImages').sortable('disable'); // disable sortable on focus
}).on('blur', () => {
$('#$fieldName').closest('.gridImages').sortable('enable'); // enable sortable on blur
});
</script>";
</script>" .
"</div>";

return $out;
}
Expand All @@ -1305,13 +1315,18 @@ class ImageExtra extends WireData implements Module {
*/
protected function renderLinkItemField(Pagefile $pagefile, $id, $n, $current) {
$fieldName = $current . '_' . $id;
$name = strtolower($current);
$field = $this->modules->get('InputfieldPageListSelect');
$field->setAttribute('name+id', $fieldName);
$field->startLabel = $this->_('Choose Page Link');
$field->columnWidth = 50;
$field->attr('value', (int)$pagefile->link);

return "<hr><div class='InputfieldContent'><label for='$fieldName' style='font-weight:bold;'>{$field->startLabel}</label>" . $field->render() . "</div>";
$out = "<div class='InputfieldImageEdit__additional--{$name}'><hr />" .
"<label for='$fieldName' style='font-weight:bold;'>{$field->startLabel}</label>" .
$field->render() . "</div>";

return $out;
}

}

0 comments on commit 16cd385

Please sign in to comment.