We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47401
    • 295 Posts
    hi guys

    how can i get a php array of child documents from a specific parent document (including TV's)

    Basically each child document will have a group of TVs for the address. so what im looking at doing is using php to filter the array of data, for example, all documents that are grouped by either county, or postcode. then I will use google maps to determine its distance.
      • 47212
      • 40 Posts
      Hello,

      If I understand what you want, you can probably use something like this :
      // Getting child resources of  the current resource id
      $childResources = $modx->getCollection('modResource', array('parent' => $resource->get('id')));
      $countrySortedResources = array();
      $postcodeSortedResources = array();
      
      // Store resources in two array depending on their TV value
      if (!empty($childResources)) {
          foreach($childResources as $childDoc) {
              if ($childDoc->getTVValue('youCustomTV') === 'country') {
                  array_push($countrySortedResources, $childDoc);
              }
              else if ($childDoc->getTVValue('youCustomTV') === 'postcode') {
                  array_push($postcodeSortedResources, $childDoc);
              }
          }
      }
      
      // Then you can do what you want with the two arrays $countrySortedResources and $postcodeSortedResources
      


      Another solution could be using indirectly the getResources snippet. First install getResources from your package management.
      Then you can use the snippet in php that way :

      $modx->runSnippet('getResources', array('parents' => $resource->get('id'), 'debug' => '1', 'tvFilters' => 'yourTV==country'));
      


      Don't forget the debug value, it allows getResources to return an array instead of nothing if no template are given.
        • 3749
        • 24,544 Posts
        In terms of speed and efficiency, TV values really suck for searching and sorting. If it's a small site and you can cache the result pages, it may not be too bad, but otherwise you may want to consider extending the modResource object and moving what's now in the TVs into a custom table.

        See this blog post: Why Extend modResource?



          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
          • 50012
          • 12 Posts
          Hi comp_nerd26,

          i think i just added an extra with your needs. It is called GoogleStoreLocator and will sort Resources by Distance on getting the address via TVs and using the Google-API for calculations.

          Here is the link to the documentation: http://www.quadro-system.de/modx-extras/googlestorelocator.html

          I just submitted the extra to the MODX-Package-Manager and waiting now for the validation. So i think it will show up in the next days. If you already want to give it a try, just click the download-button on the doc-page.

          I hope this extra will fit your needs.

          PS: @BobRay thanks a lot for that nice extra "mycomponents". Makes building an extra very easy.

          best regards
          Jan [ed. note: quadro last edited this post 10 years, 10 months ago.]
            • 3749
            • 24,544 Posts
            Thanks. I'm glad MyComponent works for you. smiley
              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
              • 47401
              • 295 Posts
              Hi Guys

              Thank you for all your feedback, much appreciated. I will take a look at the googlestorelocator and give it ago smiley
                • 47401
                • 295 Posts
                Quote from: quadro at Nov 28, 2015, 06:30 AM
                Hi comp_nerd26,

                i think i just added an extra with your needs. It is called GoogleStoreLocator and will sort Resources by Distance on getting the address via TVs and using the Google-API for calculations.

                Here is the link to the documentation: http://www.quadro-system.de/modx-extras/googlestorelocator.html

                I just submitted the extra to the MODX-Package-Manager and waiting now for the validation. So i think it will show up in the next days. If you already want to give it a try, just click the download-button on the doc-page.

                I hope this extra will fit your needs.

                PS: @BobRay thanks a lot for that nice extra "mycomponents". Makes building an extra very easy.

                best regards
                Jan

                HI

                this is a great add-on for modx. just exactly what i was looking for. A couple of questions.

                1) can the select option values be changed from km to miles? if i change it, will the add-on convert from km to miles?
                2) On some of our static pages, we have for example lots of static pages 'locations in london', or 'locations in manchester' can the add-on be changed so that it can produce a list without the need of using the form, but as well other pages will also have the form.
                  • 47401
                  • 295 Posts
                  Quote from: comp_nerd26 at Nov 30, 2015, 05:42 AM
                  Quote from: quadro at Nov 28, 2015, 06:30 AM
                  Hi comp_nerd26,

                  i think i just added an extra with your needs. It is called GoogleStoreLocator and will sort Resources by Distance on getting the address via TVs and using the Google-API for calculations.

                  Here is the link to the documentation: http://www.quadro-system.de/modx-extras/googlestorelocator.html

                  I just submitted the extra to the MODX-Package-Manager and waiting now for the validation. So i think it will show up in the next days. If you already want to give it a try, just click the download-button on the doc-page.

                  I hope this extra will fit your needs.

                  PS: @BobRay thanks a lot for that nice extra "mycomponents". Makes building an extra very easy.

                  best regards
                  Jan

                  HI

                  this is a great add-on for modx. just exactly what i was looking for. A couple of questions.

                  1) can the select option values be changed from km to miles? if i change it, will the add-on convert from km to miles?
                  2) On some of our static pages, we have for example lots of static pages 'locations in london', or 'locations in manchester' can the add-on be changed so that it can produce a list without the need of using the form, but as well other pages will also have the form.

                  figured out 1. just not too sure if i can pre set a TV to select a list of results rather than using a form
                    • 47401
                    • 295 Posts
                    Hi again

                    Ok I have slightly amended the googleStoreLocator Snippet, I hope you dont mind. This is to allow the passing of:
                    &location=`City`

                    when calling the GoogleStoreLocator Snippet you will no longer require a form input from the user for a specific template. This is so that you dont have to use the form input for results to be published on a page. so for example I have a static page showing store locations in London. Also amended the code radius because it assumed that $_REQUEST['radius'] was all ways true.

                    find the following block of code line 170 // if search form is send - sort stores: and replace with the following block of code
                    if (!empty($_REQUEST['location']) || isset($location) && !empty($location)){
                        $address = (isset($_REQUEST['location']))?str_replace(' ', '+', $_REQUEST['location']):$location;
                        $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
                        $output = json_decode($geocode);
                        $lat = $output->results[0]->geometry->location->lat;
                        $lng = $output->results[0]->geometry->location->lng;
                        $radius = isset($_REQUEST['radius'])?(int) $_REQUEST['radius']:$defaultRadius;
                        
                        //Centering Map to Position
                        $lat_center = $lat;
                        $lng_center = $lng;
                        $zoom = $zoom_search;
                        
                        //Get Filtered Stores
                        $stores = filterStoresByDistance($stores, $radius, $lat, $lng, $unit);
                    }

                      • 47401
                      • 295 Posts
                      rather than use str_replace, you can use url_encode() which does exactly the same thing. i.e replace ' ' with +