Loading


Advertisement:

Author Topic: SOLVED How to notify the user via mail model when it's ad is unblocked?  (Read 99 times)

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
How can I hook the
email_contact_user
or
email_comment_validated (I don't use comments anyway)
email model to be sent when the admin unblocks (b_enabled=1) an item?

Thank you.
« Last Edit: July 18, 2017, 09:52:01 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #1 on: July 17, 2017, 12:17:13 am »
Sure, checkout the osclass helper functions, among of them there's an osclass send email function. As for the 'unblocked' status, you'd have to make a selection of all 'unblocked' ads first, run through each ad, collect items per user and then sent one email for all unblocked ads in 1 go. You could make it into a plugin that hooks into Osclass hourly cron so the whole thing would be automated. 8)

Aficionado, Your idea fits also my new functionality described here: https://forums.osclass.org/development/how-to-make-new-items-inactive-by-default-until-the-admin-activates-them/msg152642/#msg152642

Is there a way to send email to user after it's ad is moderated (unblocked) in admin area with the unblock option?

Can't I relate to the onclick action of admin in admin area, onto the activate button, either in table list or in edit section of the item?

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2371
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #2 on: July 17, 2017, 01:57:17 pm »
Well, I am not *that* php crafted but I guess that is also possible, using onclick means you need script to do ajax php send mail call or you use the submit somehow without the need for js.

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #3 on: July 17, 2017, 04:14:40 pm »
At this time I don't have a clue about how to do that. I know that Ajax can call php functions in javascript onclick actions, but never done it before.
Anyway, as the problem with the subject from send friend email, this is not a critical thing also, so if it never gets done, it's no big deal.

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2371
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #4 on: July 17, 2017, 09:30:51 pm »
Ajax is cool, simply Google for php and ajax and you get a world of simple samples.
If you understand what it does it is actually a cool way of client-side meeting up with server-side script and you can do anything.

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #5 on: July 18, 2017, 07:57:52 pm »
Ajax is cool, simply Google for php and ajax and you get a world of simple samples.
If you understand what it does it is actually a cool way of client-side meeting up with server-side script and you can do anything.

I think I found the first step.

In oc-admin/item.php

Code: [Select]
                                        if( $item['b_enabled'] ) {
                                            $actions[] = '<a class="btn float-left" href="'.osc_admin_base_url(true).'?page=items&amp;action=status&amp;id='.$item['pk_i_id'].'&amp;'.$csrf_token.'&amp;value=DISABLE">'.__('Block') .'</a>';
                                        } else {
                                            $actions[] = '<a class="btn btn-red float-left" href="'.osc_admin_base_url(true).'?page=items&amp;action=status&amp;id='.$item['pk_i_id'].'&amp;'.$csrf_token.'&amp;value=ENABLE">'.__('Unblock') .'</a>';

Here I could insert the onclick, and it makes sense to view the item in admin and from there to unblock it. I know this solution wouldn't have effect when unblocking item/s from the admin item index table view.


EDIT: a better solution would be to find the code where the b_enabled id changed from 0 to 1 in the db. From there I could rub the hook for the email_comment_validated for example.
« Last Edit: July 18, 2017, 08:22:26 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #6 on: July 18, 2017, 08:56:25 pm »
in oc-includes/osclass/ItemActions.php

I gues the s_enabled from the comment  is a typo. The correct value would be b_enabled. There is no s_enabled in the DB.

Code: [Select]
        /**
         * Enable an item
         * Set s_enabled value to 1, for a given item id
         *
         * @param int $id
         * @return bool
         */
        public function enable($id)
        {
            $result = $this->manager->update(
                array('b_enabled' => 1),
                array('pk_i_id' => $id)
            );

            // updated correctly
            if($result == 1) {
                osc_run_hook( 'enable_item', $id );
                $item = $this->manager->findByPrimaryKey($id);
                if($item['b_active']==1 && $item['b_spam']==0 && !osc_isExpired($item['dt_expiration']) ) {
                    $this->_increaseStats($item);
                }
                return true;
            }
            return false;
        }

Now that the place has been identified... any cons about inserting here the php code for the hook

Code: [Select]
osc_run_hook('hook_email_comment_validated', $aComment);

with the function

Code: [Select]
        function sendCommentActivated ($commentId)
        {
            $aComment = $this->itemCommentManager->findByPrimaryKey($commentId);
            $aItem    = Item::newInstance()->findByPrimaryKey($aComment['fk_i_item_id']);
            View::newInstance()->_exportVariableToView('item', $aItem);

            osc_run_hook('hook_email_comment_validated', $aComment);
        }

and the function for sending the mail

Code: [Select]
    function fn_email_comment_validated($aComment) {
        $mPages = new Page();
        $locale = osc_current_user_locale();
        $aPage = $mPages->findByInternalName('email_comment_validated');

        if(isset($aPage['locale'][$locale]['s_title'])) {
            $content = $aPage['locale'][$locale];
        } else {
            $content = current($aPage['locale']);
        }

        if (!is_null($content)) {
            $words   = array();
            $words[] = array(
                '{COMMENT_AUTHOR}',
                '{COMMENT_EMAIL}',
                '{COMMENT_TITLE}',
                '{COMMENT_BODY}',
                '{ITEM_URL}',
                '{ITEM_LINK}',
                '{ITEM_TITLE}'
            );
            $words[] = array(
                $aComment['s_author_name'],
                $aComment['s_author_email'],
                $aComment['s_title'],
                $aComment['s_body'],
                osc_item_url(),
                '<a href="' . osc_item_url() . '">' . osc_item_url() . '</a>',
                osc_item_title()
            );
            $title = osc_apply_filter('email_comment_validated_title_after', osc_mailBeauty(osc_apply_filter('email_title', osc_apply_filter('email_comment_validated_title', $content['s_title'], $aComment)), $words), $aComment);
            $body = osc_apply_filter('email_comment_validated_description_after', osc_mailBeauty(osc_apply_filter('email_description', osc_apply_filter('email_comment_validated_description', $content['s_text'], $aComment)), $words), $aComment);

            $emailParams = array(
                'subject'  => $title,
                'from'     => _osc_from_email_aux(),
                'to'       => $aComment['s_author_email'],
                'to_name'  => $aComment['s_author_name'],
                'body'     => $body,
                'alt_body' => $body
            );
            osc_sendMail($emailParams);
        }
    }
    osc_add_hook('hook_email_comment_validated', 'fn_email_comment_validated');


BUT! Could I use the id from there to call a new function that does not use an email model, in which to form the email with a simple url made from the item's id?
« Last Edit: July 18, 2017, 10:13:53 pm by marius-ciclistu »

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
Re: How to notify the user via mail model when it's ad is unblocked?
« Reply #7 on: July 18, 2017, 09:51:46 pm »
SOLVED
:)))))
Iuhuu.
In order to not make "damages" to the potential developers that have this functionality as a payed plugin, I'll publish the solution only if the admin gives his aproval.

SmaRTeY

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2371
You're allowed to post your own code changes Marius, wouldn't worry too much about plugin creators. 8)

Plugin creators including myself also use code tips posted here in forums as well as ideas/questions. The truth is that many plugin buyers do not have the skills like you have to make core changes by themselves AND maintain them at the same time. The plugins are meant for users who want ease of use and maintenance but most of all stability of their system and still be able to update to new Osclass versions without many possible issues. And finally, everybody is allowed to simply post code changes they make and tip others, don't hold back! ;)

marius-ciclistu

  • issues
  • Sr. Member
  • *
  • Posts: 313
Ok. I share code only because other share also and I'm able to learn from them. I consider this my "pay" for their services.
So:
1.oc-includes/osclass/ItemActions.php

put this:

Code: [Select]
        /**
         * Enable an item
         * Set s_enabled value to 1, for a given item id
         *
         * @param int $id
         * @return bool
         */
        public function enable($id)
        {
            $result = $this->manager->update(
                array('b_enabled' => 1),
                array('pk_i_id' => $id)
            );

            // updated correctly
            if($result == 1) {
                osc_run_hook( 'enable_item', $id );
                $item = $this->manager->findByPrimaryKey($id);
                if($item['b_active']==1 && $item['b_spam']==0 && !osc_isExpired($item['dt_expiration']) ) {
                    $this->_increaseStats($item);
                }
                if($item['b_enabled']==1) {
                  osc_run_hook('hook_email_item_unlocked', $item);
                }
                return true;
            }
            return false;
        }

instead of this

Code: [Select]
        /**
         * Enable an item
         * Set s_enabled value to 1, for a given item id
         *
         * @param int $id
         * @return bool
         */
        public function enable($id)
        {
            $result = $this->manager->update(
                array('b_enabled' => 1),
                array('pk_i_id' => $id)
            );

            // updated correctly
            if($result == 1) {
                osc_run_hook( 'enable_item', $id );
                $item = $this->manager->findByPrimaryKey($id);
                if($item['b_active']==1 && $item['b_spam']==0 && !osc_isExpired($item['dt_expiration']) ) {
                    $this->_increaseStats($item);
                }
                return true;
            }
            return false;
        }

2.
oc-includes/osclass/emails.php
After:
Code: [Select]
    osc_add_hook('hook_email_item_inquiry', 'fn_email_item_inquiry');
add

Code: [Select]
//send email to user when the admin unlocks the item b_enabled=1.

function fn_email_item_unlocked($aItem) {
        $id         = $aItem['pk_i_id'];

        $item = Item::newInstance()->findByPrimaryKey( $id );
        View::newInstance()->_exportVariableToView('item', $item);

        $mPages = new Page();
        $aPage  = $mPages->findByInternalName('email_item_inquiry');
        $locale = osc_current_user_locale();

        if( isset($aPage['locale'][$locale]['s_title']) ) {
            $content = $aPage['locale'][$locale];
        } else {
            $content = current($aPage['locale']);
        }

        $item_url = osc_item_url();
        $item_link = '<a href="' . $item_url . '" >' . $item['s_title'] . '</a>';



        $title = "Ad aproved: " . $item['s_title'] . " - your site name";
        $body  = '<p>Hello ' . $item['s_contact_name'] . ',</p>
<p>Your ad ' . $item_link . ' was aproved and can be viewed on the website now.</p>';

        $from      = osc_contact_email();
        $from_name = osc_page_title();

        $emailParams = array(
            'from'      => $from,
            'from_name' => $from_name,
            'subject'   => $title,
            'to'        => $item['s_contact_email'],
            'to_name'   => $item['s_contact_name'],
            'body'      => $body,
            'alt_body'  => $body
        );


        osc_sendMail($emailParams);

    }
    osc_add_hook('hook_email_item_unlocked', 'fn_email_item_unlocked');

This is working with this mod: https://forums.osclass.org/development/how-to-make-new-items-inactive-by-default-until-the-admin-activates-them/msg152642/#msg152642
« Last Edit: July 18, 2017, 11:15:39 pm by marius-ciclistu »