Info Pages Image
Posted: Sat Nov 20, 2021 8:55 am
Using 1.0.8.7
Trying to make a hook that would allow upload of image on admin/info_pages.php
With my little knowledge, I thought that a copy/clone of the hook in s05e05 Testimonials Image would work.
Using a modernised version of that hook.
It *nearly* does i.e. it does create an input on info_pages.php to upload image but just blindly copy/cloning wasn't the answer and I'm probably missing something fundamental as there are a couple of issues.
It shows the same notice for different lines like
Undefined property: objectInfo::$pages_image in /includes/hooks/admin/info_pages/image.php on line 83
and gives a fatal error when attempting to upload an image
Unknown column 'pages_image' in 'field list'
although that column is in pages table in database.
Any help, hints or pointers gratefully received.
This is the hook file:
Trying to make a hook that would allow upload of image on admin/info_pages.php
With my little knowledge, I thought that a copy/clone of the hook in s05e05 Testimonials Image would work.
Using a modernised version of that hook.
It *nearly* does i.e. it does create an input on info_pages.php to upload image but just blindly copy/cloning wasn't the answer and I'm probably missing something fundamental as there are a couple of issues.
It shows the same notice for different lines like
Undefined property: objectInfo::$pages_image in /includes/hooks/admin/info_pages/image.php on line 83
and gives a fatal error when attempting to upload an image
Unknown column 'pages_image' in 'field list'
although that column is in pages table in database.
Any help, hints or pointers gratefully received.
This is the hook file:
Code: Select all
<?php
class hook_admin_info_pages_image {
public function listen_formNew() {
$pages_image = ENTRY_PAGE_IMAGE;
$pages_label = ENTRY_PAGE_LABEL;
$image_field = new Input('pages_image', ['id' => 'inputImage', 'class' => 'custom-file-input'], 'file');
$output = <<<EOD
<div class="form-group row">
<div class="col-form-label col-sm-3 text-left text-sm-right">{$pages_image}</div>
<div class="col-sm-9">
<div class="custom-file mb-2">
{$image_field}
<label class="custom-file-label" for="inputImage">{$pages_label}</label>
</div>
</div>
</div>
<script>
$(document).on('change', '#inputImage', function (event) {
$(this).next('.custom-file-label').html(event.target.files[0].name);
});
</script>
EOD;
return $output;
}
public function listen_formEdit() {
global $pInfo;
$image_field = new Input('pages_image', ['id' => 'inputImage', 'class' => 'custom-file-input'], 'file');
$pages_image = ENTRY_PAGE_IMAGE;
if (!Text::is_empty($pInfo->pages_image)) {
$pages_image .= '<br>' . $GLOBALS['Admin']->catalog_image('pub/' . $pInfo->pages_image, [], '', 30, 30);
$image_field->set_parameter('value', $pInfo->pages_image);
}
$page_label = $pInfo->pages_image ?? ENTRY_PAGE_LABEL;
$output = <<<EOD
<div class="form-group row">
<div class="col-form-label col-sm-3 text-left text-sm-right">{$pages_image}</div>
<div class="col-sm-9">
<div class="custom-file mb-2">
{$image_field}
<label class="custom-file-label" for="inputImage">{$page_label}</label>
</div>
</div>
</div>
<script>
$(document).on('change', '#inputImage', function (event) {
$(this).next('.custom-file-label').html(event.target.files[0].name);
});
</script>
EOD;
return $output;
}
protected function upload($pages_id) {
$pages_image = new upload('pages_image');
$pages_image->set_extensions(['png', 'gif', 'jpg', 'svg', 'webp']);
$pages_image->set_destination(DIR_FS_CATALOG . 'pub/');
if ($pages_image->parse() && $pages_image->save()) {
$GLOBALS['db']->query("UPDATE pages SET pages_image = '" . $GLOBALS['db']->escape($pages_image->filename) . "' WHERE pages_id = " . (int)$pages_id);
}
}
public function listen_addNewAction() {
$this->upload($GLOBALS['insert_id']);
}
public function listen_updateAction() {
$this->upload($GLOBALS['pages_id']);
}
public function listen_infoBox($parameters) {
global $pInfo;
if (!Text::is_empty($pInfo->pages_image)) {
$parameters['contents'][] = ['class' => 'text-center', 'text' => $GLOBALS['Admin']->catalog_image('pub/' . $pInfo->pages_image, '')];
}
}
}