1.0.8.20 - images directory

Open to all! Ask other shopowners for help.
User avatar
Kofod95
Senior Contributor
Posts: 748
Joined: Sat Feb 06, 2021 7:38 pm
Phoenix Version: 1.0.8.20
Has thanked: 99 times
Been thanked: 179 times

Re: 1.0.8.20 - images directory

Post by Kofod95 »

https://github.com/CE-PhoenixCart/Phoen ... action.php

The hook-calls connected to the actions are here. Like Zipur said; you can use postAction to do stuff after the action have happened (in this case insertProuctAction or updateProductAction). You still have all the posted values, so you can almost just recreate the code in a hook and then modify it as you wish.

//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
zipurman
Builder
Posts: 540
Joined: Tue Oct 13, 2020 5:20 pm
Phoenix Version: v
Has thanked: 93 times
Been thanked: 162 times

Re: 1.0.8.20 - images directory

Post by zipurman »

PiLLaO wrote: Tue Aug 01, 2023 11:51 am How can I know the products listener? Because if I search in files for listen_postAction I can't find anything.

I'm having problems to make It works, and I see that I don't know where need to put the code to make it works (listener are new for my).

The easyest way, will be modify the path here (set_destination)

Code: Select all

  $products_image = new upload('products_image');
  $products_image->set_destination(DIR_FS_CATALOG_IMAGES);
  if ($products_image->parse() && $products_image->save()) {
    $sql_data['products_image'] = Text::prepare($products_image->filename);
  }
Okay, so the file admin/includes/segments/process_action.php has a line:

Code: Select all

$admin_hooks->cat($hook_action);
This line "listens" for a hook with listen_[actionName] as a method.

So for example, if you created a file /includes/hooks/admin/catalog/updateProduct.php with the following content:

Code: Select all

<?php
	class hook_admin_catalog_updateProduct{
	
		public function listen_updateProductAction(){
		
			var_dump($_POST);
			die();
		
		}
	
	}

The above should show you a dump of all $_POST variables when you save a product.

You should have all of your data in the $_POST, so you can just do a SQL update as this is called after the action is done, so you'll just be overriding whatever changes you need to override after the initial action was already completed.

Once you have coded your update, removed the die() and the var_dump() and test. Add them back to test as needed.

Hope that helps ;)
zipurman
-----------
PiLLaO
Contributor
Posts: 107
Joined: Thu Nov 05, 2020 9:28 pm
Phoenix Version: v1.1.0.6
Has thanked: 53 times
Been thanked: 11 times

Re: 1.0.8.20 - images directory

Post by PiLLaO »

Thanks for both explains.

So, all hooks are called previous $admin_hooks->cat('postAction');

And them admin/includes/actions/catalog/ [insert_product.php, update_product.php] ??

Because, I'm confused.

Can I move the file before this piece of code in actions?

Code: Select all

  $products_image = new upload('products_image');
  $products_image->set_destination(DIR_FS_CATALOG_IMAGES);
  if ($products_image->parse() && $products_image->save()) {
    $sql_data['products_image'] = Text::prepare($products_image->filename);
  }
Because I can't move the file before save the image from $_FILES['tmp_name'], isn't it?
User avatar
zipurman
Builder
Posts: 540
Joined: Tue Oct 13, 2020 5:20 pm
Phoenix Version: v
Has thanked: 93 times
Been thanked: 162 times

Re: 1.0.8.20 - images directory

Post by zipurman »

I would recommend watching this if you havent already

https://youtu.be/bLd9m_uABYI
zipurman
-----------
PiLLaO
Contributor
Posts: 107
Joined: Thu Nov 05, 2020 9:28 pm
Phoenix Version: v1.1.0.6
Has thanked: 53 times
Been thanked: 11 times

Re: 1.0.8.20 - images directory

Post by PiLLaO »

I just view the video, it's very useful.

But I still need a little help, when I said before
So, all hooks are called previous $admin_hooks->cat('postAction');
I was talking about admin/catalog.php

Using the code that you post my previously

Code: Select all

	class hook_admin_catalog_updateProduct{

		// public function listen_updateProductAction(){
		public function listen_postAction(){
    global $products_id;

    	echo 'updateProductAction<br><br>';
    	echo '$products_id ' . $products_id . '<br>';
			echo '<pre>';
			var_dump($_FILES);
			echo '</pre>';
			echo '<pre>';
			var_dump($_POST);
			echo '</pre>';
			die();

		}

	}
If I use listen_postAction() I get empty $_FILES and $_POST, if I use listen_updateProductAction() I have all the data.

So I don't understand the order than happens hooks, insert/update_product, post_action to make work the code
User avatar
zipurman
Builder
Posts: 540
Joined: Tue Oct 13, 2020 5:20 pm
Phoenix Version: v
Has thanked: 93 times
Been thanked: 162 times

Re: 1.0.8.20 - images directory

Post by zipurman »

Forget about "postAction" as it is called to late. The problem is that in admin/includes/segments/process_action.php, There is a Href::redirect before "postAction", so the "postAction" is never called as the page re-directs before it has a chance.

listen_updateProductAction however is called before the Href::redirect, so you will want to use that to code in your changes and updates with the $_POST data and $_FILES data.
zipurman
-----------
PiLLaO
Contributor
Posts: 107
Joined: Thu Nov 05, 2020 9:28 pm
Phoenix Version: v1.1.0.6
Has thanked: 53 times
Been thanked: 11 times

Re: 1.0.8.20 - images directory

Post by PiLLaO »

I think I'm missing something.

I use this piece of code in catalog/test_upload.php and works like a charm

Code: Select all

        $temp_file = $_FILES["products_image"]["tmp_name"];
        $dest_path = '/home/public_html/catalog/images/productos/' . $_FILES["products_image"]["name"];

        if (move_uploaded_file($temp_file , $dest_path )) {
            echo "sucess: " . $ruta_destino;
        } else {
            echo "error.";
        }
But If I use in hook, allways shows error

Code: Select all

<?php
  class hook_admin_catalog_updateProduct{
    public function listen_updateProductAction() {
        $temp_file = $_FILES["products_image"]["tmp_name"];
        $dest_path = '/home/public_html/catalog/images/productos/' . $_FILES["products_image"]["name"];

        if (move_uploaded_file($temp_file , $dest_path )) {
            echo "sucess: " . $ruta_destino;
        } else {
            echo "error.";
        }
      }
    }
	    
I use var_dump($_FILE) to see if data it's ok, and looks correctly but don't works.

Also I use

Code: Select all

  $products_image = new upload('products_image');
  $products_image->set_destination(DIR_FS_CATALOG_IMAGES);
  if ($products_image->parse() && $products_image->save()) {
//    $sql_data['products_image'] = Text::prepare($products_image->filename);
  }
And use var_dump($products_image) $products_image['filename'] and $products_image['temp_filename'] are NULL

I don't know how make it works :?
User avatar
zipurman
Builder
Posts: 540
Joined: Tue Oct 13, 2020 5:20 pm
Phoenix Version: v
Has thanked: 93 times
Been thanked: 162 times

Re: 1.0.8.20 - images directory

Post by zipurman »

Keep in mind that the listen_updateProductAction is being called AFTER the actual product update has already happened. This means any $_FILES have already been dealt with and are no longer available. However, you still have access to the $_FILES array, so you could do the following:

1. Check if the file exists in '/home/public_html/catalog/images/' . $_FILES["products_image"]["name"]
2. If the file does exist in the root images directory, move it to the /productos/ directory
3. If the file was moved, update the product sql to reflect the change.
zipurman
-----------
PiLLaO
Contributor
Posts: 107
Joined: Thu Nov 05, 2020 9:28 pm
Phoenix Version: v1.1.0.6
Has thanked: 53 times
Been thanked: 11 times

Re: 1.0.8.20 - images directory

Post by PiLLaO »

That's the problem!

File exists but move_uploaded_file() still show error, I think because "the actual product update has already happened" so I use rename() to move it.

I think now I can make it works, many thanks for your answers :)


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