We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: OpenGeek at Apr 24, 2010, 08:28 AM

    Hold on guys. Property Sets are applied through tags and are not intended to be used programatically in a snippet like that to get data for a Chunk tpl. TBH, I don’t see a scenario where using that is in any way beneficial. In fact, doing things that way could potentially create a mess of unnecessary dependencies, IMO. How does anyone know what Property Set you are hardcoding into your Snippet? How is that relationship maintained? If you’re not hardcoding it, then why not just call the property set in the tag as intended and simply pass the scriptProperties to the chunk?

    Point taken. I can imagine a snippet (or more likely a plugin) that would select the property set based on conditions at runtime or a utility that processed all property sets , but it’s a stretch.
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 5811
      • 1,717 Posts
      Can we coming back to my initial request ?
      Default configuration file - the better implementation ?

      1/ AjaxSearch have lot of default parameters. These parameters could be read from a default configuration (as with the current release) or loaded during the build process as a default properties of the snippet (This is what I had understood until now)

      2/ with the ajax mode, most of these parameters + custom parameters should be used by the php script launched by the ajax request. I think that 2 options exist to do this:
      a/ add all these parameters (default and custom) to the page as js parameters and then transmit these parameters with an ajax request to the Php script
      b/ add only the "non default" parameters (the parameters of the $scriptProperties array that are different than the default parameters of the snippet)

      My choice is to minimize the weight of a document, so I choose the option b/. This option is implemented today with the release 1.8.5 of AjaxSearch with MODx evolution.
      To do it, I need, to retrieve the "default" parameters. By doing, programmatically, an array_difference between the $scriptProperties array and the initial default properties (new properties and properties with different values), I know which properties, I need to transmit to js for the appropriate ajax request.

      I’m confused; getProperties is used internally; that is not really a function that needs to be called explicitly, ever.
      If $snippet->getProperties() is not available as a public method, Ok,I will come back to the "old" but efficient inclusion of a default configuration file and avoid to use properties set loaded with the build process (useless in my case).

      But I think that it’s a pity to not have a public method to retrieve the default properties set loaded with the build process.
      And IMO this default properties set mechanism (I don’t speak of properties set linked with tags), without a public method, is partially exploited.

        • 22303 MODX Staff
        • 10,725 Posts
        I’m sorry, but I’m lost on what your issue is still coroico. I don’t even understand what you are saying here. External configuration files should simply not be used. We have everything we need to handle configuration through Properties, Property Sets, and Settings (System, Context, and User). Standardizing around this is very important to be able to keep things more tightly coupled; something a leaky abstraction like MODx requires to keep component functionality useful and consistent when interacting with the core.

        Default properties should be defined for a Snippet where appropriate, and namespaced Settings should be used where appropriate. It’s a pretty simple approach and exemplified very well by the components we have already created for Revolution.

        Re: getProperties(), why would you need to use these in the build process? Example?
          • 5811
          • 1,717 Posts
          getProperties(), why would you need to use these in the build process? Example?
          For the build process, I use only $snippets->setProperties($properties); Not getProperties().

          I’m confused; getProperties is used internally; that is not really a function that needs to be called explicitly, ever.
          But I would like to use $snippets->getProperties($properties); (or an equivalent) in the snippet code.

          My initial idea, for the default configuration of the ajaxSearch snippet, was to use Default properties .
          For this I have defined a property set inside a _build/data/properties/properties.ajaxSearch.php file like this:
          <?php
          /**
           * @package ajaxSearch
           * @subpackage build
           */
          $properties = array(
              array(
                  'name' => 'debug',
                  'desc' => 'Debug option.',
                  'type' => 'textfield',
                  'options' => '',
                  'value' => '0',
              ),
              array(
                  'name' => 'grabMax',
                  'desc' => 'The maximum number of search results displayed.',
                  'type' => 'textfield',
                  'options' => '',
                  'value' => '6',
              ),
              array(
                  'name' => 'tplResult',
                  'desc' => 'The template used to display a result.',
                  'type' => 'list',
                  'options' => '',
                  'value' => 'result',
              ),
              
              ... and so on for all the default properties for ajaxSearch
          
          );
          return $properties;
          ?>
          And these properties are loaded as default properties inside a transport.snippet.php file:
          $properties = include $sources['data'].'properties/properties.ajaxSearch.php';
          $snippets[1]->setProperties($properties);


          So with a document with [!AjaxSearch!] as snippet call, I retrieve the default parameters in the $scriptProperties array.
          Until here all is correct. tongue

          Now, I would like create an another (hidden) document to handle the ajax request with [!AjaxSearch!], when the ajax mode is used.
          In this case, with [!AjaxSearch!], I retrieve in the $scriptProperties array the default parameters as on the main snippet call.

          Now with
          [!AjaxSearch? &debug=`1` &tplResult=`myTplResult`!]
          as snippet call on the main document. I get in the $scriptProperties array, the properties and the changed properties: debug and tplResult

          On the second document where the snippet call is still [!AjaxSearch!], I get ONLY the default properties.

          So to retrieve the correct &debug and &tplResult properties, I need to pass these properties, thru the ajax request. For this I need to pass these two custom properties thru a javascript script like this:
          <script type="text/javascript">
          //<![CDATA[
          asvar=new Array();asvar[0]='{"cfg":"&debug=`1` &tplResult=`myTplResult` "}';
          //]]>
          </script>
          
          To do this, I need,in the main document, to differentiate the default properties of the snippet from the custom properties added by the developper.

          By using:
          $snippet = $modx->getObject('modSnippet',array('name'=>'Doodles'));
          $properties = $snippet->getProperties();
          I could retrieve these default properties. And by doing an array_difference between $properties and $scriptProperties, I determine which properties, I should pass to javascript to set up the appropriate call to the second document which handle the ajax request.
          In this case, I simply pass a json structure with all the custom parameters:
          {"cfg":"&debug=`1` &tplResult=`myTplResult` "}


          This is possible by using $snippet->getProperties().
          But if this method is not public and not allowed externally, How could I retrieve these properties ?
          If I can’t retrieve the default properties, I can’t do an array_difference with the $scriptProperties array and I don’t know which are the custom properties embarrassed

          Obviously, I could pass to javascript all the properties of the $scriptProperties array. But this increase unecessarily the weight of the first page.

          So, this is not an issue, but a change request. Does it possible to have in the futur a public non-internal method to retrieve the default properties linked to a snippet, like the $snippet->getProperties() method ?


            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: coroico at Apr 26, 2010, 11:43 AM

            So, this is not an issue, but a change request. Does it possible to have in the futur a public non-internal method to retrieve the default properties linked to a snippet, like the $snippet->getProperties() method ?
            Change request denied; this is the whole purpose of Property Sets, so you can pass a set of common, customized parameters to multiple Elements that make up a Component.
            [[AjaxSearch@configuration1]]
            would simply be used in all locations and all Elements would see the same properties. Issue solved.
              • 5811
              • 1,717 Posts

              [[AjaxSearch@configuration1]]

              [[snippet@defaultConfiguration]]
              This change a bit the way to call a snippet !

              [Edit] Rather to use [[AjaxSearch@defaultConfiguration]] in each snippet call, I definitively prefer to read a config file, before to take into account the user (not default) property set
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: coroico at Apr 27, 2010, 10:51 AM

                [[AjaxSearch@configuration1]]

                [[snippet@defaultConfiguration]]
                This change a bit the way to call a snippet !

                [Edit] Rather to use [[AjaxSearch@defaultConfiguration]] in each snippet call, I definitively prefer to read a config file, before to take into account the user (not default) property set
                Well, that is not going to have my support then; making a script go to a file to get default properties is unnecessary (everything you need to handle default and user customizable properties is already handled for you), a wasteful use of file I/O, and inconvenient for the user when there is a UI already in place to manage the properties.
                  • 25663 MODX Staff
                  • 12,272 Posts
                  Quote from: coroico at Apr 27, 2010, 10:51 AM
                  Rather to use [[AjaxSearch@defaultConfiguration]] in each snippet call, I definitively prefer to read a config file, before to take into account the user (not default) property set

                  The way property sets are handled in Revo makes mistakes far less possible due to syntax, and exposes full configuration options to the users without having to dig through code. It’s very helpful. Are you asking how to pass the values of a property set in a "master" snippet to other helper snippets?
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                    • 5811
                    • 1,717 Posts
                    To illustrate my previous messages, find attached two versions of a simple implementation of an ajax request: AjaxDemo

                    This snippet display its current properties and a button "show/hide properties" display the same properties in an ajax window.
                    My objective is to retrieve all the properties in the ajax part. AjaxDemo 1.0 can’t. AjaxDemo 2.0 do it.


                    On the main document, add [[AjaxDemo? &ajaxId=`xxx` &proper2=`new2` &proper4=`new4`]]
                    Where xxx is the id of the document which handle the ajax request. On this document simply add [[AjaxDemo]].
                    This document should be set up with an empty template and with a text content type.

                    These two versions of snippets have both a default properties set (15 properties named properN=``valN`

                    With the first version (AjaxDemo 1.0), the Ajax windows shows the initial default values. &proper2=`new2` &proper4=`new4` are not taken into account. Which is normal as [[AjaxDemo]] is used on the ajax handler document.

                    With the second version, to retreive the same results as the non ajax mode, &proper2=`new2` &proper4=`new4` should be passed to javascript (look at the code of the page).
                    The snippet code of this version is the following:
                    <?php
                    /**  
                     * @package ajaxDemo  
                     */ 
                    if (!isset($_POST['ajax'])) {
                        $output = '<h3>AjaxDemo simply display the snippet properties on the current page.' . "</h3><br />\n\r";
                        foreach($scriptProperties as $key=>$value) {
                            $output .= "$key = $value, " . "<br>\n\r";
                        }
                        $output .= '<br /><h3>Use the show/hide buttons to display the properties thru an ajax request.' . "</h3><br />\n\r";
                        $output .= '<form id="ajaxDemo"><input id="show_button" type="button" value="Show properties" />';
                        $output .= '<input id="hide_button" type="button" value="Hide properties" /></form>';
                        $output .= '<br /><div id="resArea"></div>';
                    
                        $jsInclude = $modx->getOption['core'] . 'assets/components/ajaxDemo/js/ajaxDemo.js';
                        $modx->regClientStartupScript($jsInclude);
                    
                        // Pass to javascript only the changed properties
                        $snippet = $modx->getObject('modSnippet',array('name'=>'AjaxDemo'));
                        $properties = $snippet->getProperties();
                        $modifiedProperties = array_diff_assoc($scriptProperties,$properties);
                    
                        $json= json_encode($modifiedProperties,true); 
                        $line = "newpp='{$json}'";
                        $jsVars = <<<EOD
                    <!-- start AjaxDemo header -->
                    <script type="text/javascript">
                    //<![CDATA[
                    {$line}
                    //]]>
                    </script>
                    <!-- end AjaxDemo header -->
                    EOD;
                        $modx->regClientStartupScript($jsVars);
                    }
                    else {
                        // display only properties (ajax mode)
                        if (isset($_POST['newpp'])) {
                            $newProperties = json_decode($_POST['newpp'],true);
                        }
                        else $newProperties = array();
                        $finalProperties = array_merge($scriptProperties, $newProperties); // new properties added to the default.
                        
                        $output = '';
                        foreach($finalProperties as $key=>$value) {
                            $output .= "$key = $value, ";
                        }
                    }
                    return $output;
                    ?>

                    Note the lines:
                    // Pass to javascript only the changed properties
                    $snippet = $modx->getObject(’modSnippet’,array(’name’=>’AjaxDemo’));
                    $properties = $snippet->getProperties();
                    $modifiedProperties = array_diff_assoc($scriptProperties,$properties);
                    Without using $snippet->getProperties(); I don’t know how I could determine what are the properties changed by the user.
                    To avoid to use this method, the only another way I see, will be to get from the file used to set up the default properties, which is not so elegant than used $snippet->getProperties();

                    Other suggestions to solve this topic (retrieve all the properties in the ajax window) are welcome.
                    Simply I don’t want pass to javascript all the properties of $scriptProperties array and only use [[AjaxDemo]] on the document which handle the ajax request.
                      • 22303 MODX Staff
                      • 10,725 Posts
                      I still don’t understand why you have to pass any of the properties via AJAX if the snippet can get it automatically from the default properties, or be simply overridden by a property set that is shared between the AJAX request handler snippet and the form rendering snippet?