Info Pages Image

Open to all! Ask other shopowners for help.
Post Reply
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Info Pages Image

Post by heatherbell »

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:

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, '')];
    }
  }

}


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Info Pages Image

Post by ecartz »

heatherbell wrote: Sat Nov 20, 2021 8:55 am Undefined property: objectInfo::$pages_image in /includes/hooks/admin/info_pages/image.php on line 83
Change line 82 from

Code: Select all

    if (!Text::is_empty($pInfo->pages_image)) {
to

Code: Select all

    if (isset($pInfo->pages_image) && !Text::is_empty($pInfo->pages_image)) {
Same thing for line 35.

For the other problem, I suspect that it's just something silly. E.g.

1. The database column is pagesImage rather than pages_image.
2. The database column is on page rather than pages.
3. You are looking at a different database than the one that Phoenix is using.

Note that fixing the something silly may also have the effect of fixing the first problem without having to add the isset checks.

Sometimes it helps to copy the SQL from the error message into phpMyAdmin and run it there. If it works, then it's more likely to be something like the wrong database. If you get the same error, it's more likely to be a typo in a name.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Info Pages Image

Post by heatherbell »

Thanks so much for taking the time to help me.
D'oh, I am going blind!
Yes, I had inserted the table column in a different database.
The notices and issues above solved, as you suggest, without adding the isset checks.
However, now, after uploading the image and saving (I checked and the image was successfully saved to the DB) but it then shows a new fatal error on admin/info_pages.php:
Uncaught TypeError: Argument 2 passed to Image::__construct() must be of the type array, string given, called in /admin/includes/classes/admin.php on line 66 and defined in /includes/system/versioned/1.0.8.1/image.php:29 Stack trace: #0
/admin/includes/classes/admin.php(66): Image->__construct('pub/psi_logo.pn...', '')
#1
/includes/hooks/admin/info_pages/image.php(84): Admin::catalog_image('pub/psi_logo.pn...', '')
#2
/includes/system/versioned/1.0.8.1/hooks.php(150): hook_admin_info_pages_image->listen_infoBox(Array)
#3
/includes/system/versioned/1.0.8.1/hooks.php(144): hooks->cat('infoBox', Array)
#4
/t in /includes/system/versioned/1.0.8.1/image.php on line 29


and another fatal error when I open the info page:
Uncaught Error: Call to undefined method Input::set_parameter() in

Seems to point to this block of code:

Code: Select all

    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);
    }
It all seems to be working as expected besides these fatal errors and I've also managed to successfully create an Info module to display the image.
Any other help or pointers to solve those errors gratefully received.
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Info Pages Image

Post by ecartz »

It's

Code: Select all

      $parameters['contents'][] = ['class' => 'text-center', 'text' => $GLOBALS['Admin']->catalog_image('pub/' . $pInfo->pages_image, '')];
should be

Code: Select all

      $parameters['contents'][] = ['class' => 'text-center', 'text' => $GLOBALS['Admin']->catalog_image('pub/' . $pInfo->pages_image)];
The set_parameter line in the other block of code should just be set.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Info Pages Image

Post by heatherbell »

ecartz wrote: Sat Nov 20, 2021 2:39 pm It's
That's solved the first fatal error but still getting, when click on edit info page:

Fatal error: Uncaught Error: Call to undefined method Input::set_parameter() in /includes/hooks/admin/info_pages/image.php:38
where Line 38 is

Code: Select all

      $image_field->set_parameter('value', $pInfo->pages_image);
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Info Pages Image

Post by ecartz »

ecartz wrote: Sat Nov 20, 2021 2:39 pm The set_parameter line in the other block of code should just be set.

Code: Select all

      $image_field->set('value', $pInfo->pages_image);
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Info Pages Image

Post by heatherbell »

ecartz wrote: Sat Nov 20, 2021 2:39 pm The set_parameter line in the other block of code should just be set.
Sorry I didn't understand that comment but now works as expected with no errors.
Thanks again for taking the time to help and for your patience.
Next up to try is a hook to assign products to display on info pages :roll:
Please let me know if you know of an example of that already existing in core or supp codes.
User avatar
burt
Core Team
Posts: 4551
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Info Pages Image

Post by burt »

heatherbell wrote: Sat Nov 20, 2021 5:14 pm Next up to try is a hook to assign products to display on info pages :roll:
Please let me know if you know of an example of that already existing in core or supp codes.
The *inverse* (I think) of one of the modules in s05e04 (which I think you wrote if memory serves?)

Another gotcha for uploading images is to ensure the form has the correct enctype to allow file uploading.
Example: https://github.com/CE-PhoenixCart/Phoen ... go.php#L29
I am not here to build for you.
I am here to build with you. Let's help each other.
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Info Pages Image

Post by heatherbell »

burt wrote: Tue Nov 23, 2021 10:59 am The *inverse* (I think) of one of the modules in s05e04 (which I think you wrote if memory serves?)
I think it's my memory that's the problem :roll:
With so many thanks to you and ecartz for lighting the way I've managed to make a module for Info Pages working as it should on 1.0.8.7 that shows the products to which the Info Page is assigned with s05e04 and I feel good!
I'll pack it up and upload it in this forum after further testing.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply