CB AutoAction and data attributes

4 months 3 weeks ago #329395 by activha
CB AutoAction and data attributes was created by activha
Hello

I have the following example input field in frontend
<input id="company" name="activ" type="text" class="form-control shadow-none border-0 w-100 streamInput" placeholder="quel est le nom de ton activation ?" data-company-id="5dddc305f0f0a8b36bf19b23" data-registration-number="undefined">

and I'd like to save the data attributes in the params.

for the input field I use :
$variables['var3']->params()->set( 'activ', htmlspecialchars($input->getString( 'activ' )) );
which is working fine

for the attribute field I tried :
$variables['var3']->params()->set( 'data-company-id', htmlspecialchars($input->getAttribute( 'data-company-id' )) );
but this fails.

Do you have a php function in CB or CB autoaction that I can use to retrieve the data attributes and save them ?

If not can you suggest something to achieve this ?

Thanks a lot in advance

Please Log in to join the conversation.

4 months 3 weeks ago - 4 months 3 weeks ago #329398 by krileon
Replied by krileon on topic CB AutoAction and data attributes
HTML element attributes are not sent with request data. They only exist in the DOM and their purpose is for JS/CSS. You'll need to add a hidden input type with that value so you can pull it from the request data. Example as follows.

<input name="company-id" type="hidden" value="5dddc305f0f0a8b36bf19b23" />


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

4 months 3 weeks ago #329424 by activha
Replied by activha on topic CB AutoAction and data attributes
Thanks a lot !

I have another question concerning Cb Autoactions.

We have a joomla system plugin with ajax requesst to add autocompletion to CB Activity posting area.

However I'd like to limit this plugin to CB use only for CB Activity pages instead of having it an all pages system plugin, but we still need the ajax functionality.

How could we trigger the Ajax requests from a CB Autoaction code (or link to a file) ? Do you have the same functionality within CB as in Joomla ?

We call
index.php?option=com_ajax&plugin=Activation&format=json

and the plugin code is
<?php

/**
 * @package     Joomla.Plugin
 * @author      Robert Mittl <info@mittl-medien.de>
 * @copyright   Copyright © 2019 Robert Mittl. All rights reserved.
 * @license     GNU General Public License version 3 or later; see LICENSE.txt
 * @link        https://mittl-medien.de
 */

 // No direct access
defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Filesystem\File;
use Joomla\Registry\Registry;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Uri\Uri as CMSUri;



class PlgSystemActivation extends CMSPlugin
{
    /**
     * Affects constructor behavior. If true, language files will be loaded automatically.
     *
     * @var    boolean
     * @since  1.0
     */
    protected $autoloadLanguage = true;
    
    /**
     * Application object
     *
     * @var    CMSApplication
     * @since  3.2
     */
    protected $app;

    public function __construct($subject, $params)
    {
        parent::__construct($subject, $params);
        //$this->loadLanguage();
    }

    //Frontend

          public function onBeforeCompileHead()
    {
        $app = JFactory::getApplication();
        $doc =\JFactory::getDocument();
        $doc->addScript('/media/plg_system_activation/js/main_activation.js');
       
    }

    public static function onAjaxActivation()
    {
        $app = JFactory::getApplication();
        $array = $app->input->getArray();
        $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';        
        if ($contentType === "application/json") {

            //Receive the RAW post data.
            $content = trim(file_get_contents("php://input"));
            $receivedData = json_decode($content, true);
           
            $folderPath = explode("/", JPATH_ROOT);
            $removeLastElement = array_pop($folderPath);
            $securePath = implode('/', $folderPath);

             $api_key = 'xxxx';

            if ($receivedData['task'] === 'getAutoCompleteLocationData') {
                $curl = curl_init();
                        
                $query = $receivedData["query"];
                curl_setopt_array($curl, array(
                    CURLOPT_URL => "https://societeinfo.com/app/rest/api/v2/places.json/autocomplete?query={$query}&key={$api_key}",                    
                
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => '',
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => 'GET',
                ));

                $response_location = curl_exec($curl);
                curl_close($curl);
                //TODO: Split the company name if the result false
                $location = $response_location;

                return json_decode($response_location);
                // Save the result
            }

                if ($receivedData['task'] === 'getAutoCompleteCompanyData') {
                    $curl = curl_init();
                        
                    $query = $receivedData["query"];
                    $locationId = $receivedData["location_id"];
                    curl_setopt_array($curl, array(
                    			//correcte url for free autocomplete
                                CURLOPT_URL => "https://societeinfo.com/app/rest/api/v2/companies.json/autocomplete?SearchMode=autocomplete&withHighlight=true&placeId=${locationId}&query={$query}&limit=10&key={$api_key}",
                            
                                CURLOPT_RETURNTRANSFER => true,
                                CURLOPT_ENCODING => '',
                                CURLOPT_MAXREDIRS => 10,
                                CURLOPT_TIMEOUT => 0,
                                CURLOPT_FOLLOWLOCATION => true,
                                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                                CURLOPT_CUSTOMREQUEST => 'GET',
                            ));

                    $response_company = curl_exec($curl);
                    curl_close($curl);
                        
                    //TODO: Split the company name if the result false
                    $company = $response_company;
                    return json_decode($response_company);
                }
            }
        }
}

My idea would have been to move this plugin code to an autoaction but how would we call the ajax after then with CB ?

Thanks for the hint

Please Log in to join the conversation.

4 months 3 weeks ago #329427 by krileon
Replied by krileon on topic CB AutoAction and data attributes
You can use a Code action to execute whatever PHP you like. Then under Output set Display to JSON. Leave Triggers as None. You now have a JSON endpoint you can use wherever you like. You'd call its URL (found under the Global tab) the same way you're doing now with your current ajax URL.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

4 months 3 weeks ago #329436 by activha
Replied by activha on topic CB AutoAction and data attributes
Doing this way does not display the json but seems to integrate the file within the joomla page with the itemid. Removing the itemid does not change.

I remember having this issue a couple of years ago but cannot retrieve how to fix it to only get the json result

The PHP file has been rewritten to be limited to the function onAjaxActivation() so I should only get the json_decode($response_company) ?

I have set :
no triggers
Allow direct Access
No conditions
Action : PHP with include to the file
Output : json (no layout)
Parameters : translate and substitutions only, and references var3 and var6 as I'll use it on CB activity

Any idea of what's wrong ?

Please Log in to join the conversation.

4 months 3 weeks ago #329438 by krileon
Replied by krileon on topic CB AutoAction and data attributes
You're not going to be able to just dump your existing Joomla plugin code 1 for 1 into it like that. It's not going to run your Joomla plugin or have the Joomla variables you're accessing. Access the auto action with &format=raw if you don't want Joomla to output its template and rendering behavior.

Below is an example of it returning a JSON response with a JSON header.

Action
Method: PHP
Code:
return array( 'My JSON Data' );
Output
Display: JSON

So you'd need the code in your onAjaxActivation function, adjust your $app->input usages, etc..


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.214 seconds

Facebook Twitter LinkedIn