We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    Not sure, if it is completely right for your scenario,
    but something like that might work:

    <?php
    
    $location = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
    $levelType = $modx->getOption('leveltype', $scriptProperties, '');
    $outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, ',');
     
    // load migxresourcerelations class
    $packageName = 'migxresourcerelations';
    $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
    $modelpath = $packagepath . 'model/';
     
    $loaded = $modx->addPackage($packageName, $modelpath, $prefix);
    if (! $loaded) {return 'failed to add ' . $packageName;}
    $classname = 'rrResourceRelation';
     
     
    // determine current category and set featured TV
    $relName = 'clinics';
    $relTV = 67;
     
    // get ids of all sub-areas and their clinics
    $areas = $modx->getChildIds($location,5,array('context' => 'web'));
    $areas = array_unique($areas);
     
    // get clinic data
    $clinics = $clinicIDs = array();
    $criteria = $modx->newQuery('modResource');
    $criteria->distinct();
    $criteria->select('id','pagetitle','alias');
    $criteria->where(array('id:IN' => $areas, 'template' => '2'));
    $criteria->sortby('pagetitle','ASC');
    $collection = $modx->getCollection('modResource', $criteria);
    foreach ($collection as $clinic) {
        $clinics[$clinic->get('id')]['alias'] =  $clinic->get('alias');
        $clinics[$clinic->get('id')]['title'] =  $clinic->get('pagetitle');
        $clinicIDs[] = $clinic->get('id');
    }
    $clinicIDs = array_unique($clinicIDs);
    
    
    //get doctor data
    
    //$scriptProperties['parents'] = '213';
    $scriptProperties['return'] = 'data';
    //$scriptProperties['return'] = 'sql';
    $scriptProperties['includeTVs'] = 'resourcealbum';//<- put here your tv-names
    $scriptProperties['loadModels'] = 'migxresourcerelations';
    $scriptProperties['leftJoin'] = '[{"class":"rrResourceRelation","alias":"Source","on":"Source.source_id = modResource.id"}]';
    $scriptProperties['select'] = '{"modResource":"pagetitle","Source":"target_id AS Source_target_id,source_id AS Source_source_id"}';
    $scriptProperties['where'] = '{"Source.target_id:IN":['.implode(',',$clinicIDs).']}';
    $scriptProperties['limit'] = 0;
    
    /* @var pdoFetch $pdoFetch */
    $fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
    if (!$pdoClass = $modx->loadClass($fqn, '', false, true)) {return false;}
    $pdoFetch = new $pdoClass($modx, $scriptProperties);
    $pdoFetch->addTime('pdoTools loaded');
    $output = $pdoFetch->run();
    
    echo '<pre>' . print_r($output,1) . '</pre>';
    
    /* 
     
    //get doctor data
    $criteria = array();
    $criteria['rrResourceRelation.target_id:IN'] = $clinicIDs;
    $criteria['TemplateVarResources.tmplvarid:IN'] = array(44,41,40,$relTV);
    $criteria = $modx->newQuery('rrResourceRelation', $criteria);
    $items = $modx->getCollectionGraph('rrResourceRelation','{"Source":{"TemplateVarResources":{}}}',$criteria);
     
    //this is debug code to figure out why not all tvs are being returned
    // the actual code is commented below under 'prepare doctor output'
    $target = array();
    foreach ($items as $item) {
            $target[] = $item->get('source_id');
            $target[] = $item->Source->get('pagetitle');
            $tvs = array();
        $tvCount = 0;
        foreach($item->Source->TemplateVarResources as $tvr) {
                $tvs[$tvr->get('id')] = $tvr->get('value');
            $tvCount++;
            }
    }
    $target[] = $tvs;
     
    $doctorCount = $tvCount;
    $clinicOutput = $target;
    */
     
    /*
    // prepare doctor output
    $featuredCount= 0;
    $doctorCount = 0;
    $doctorOutput = '';
    if (!empty($items)) {
        foreach ($items as $item) {
            if (is_object($item)) {
                $doctorCount++;
                $clinicTitle = $item->Target->get('pagetitle');
                $clinicAlias = $item->Target->get('alias');
                $doctorName = $item->Source->get('pagetitle');
                $doctorID = $item->Source->get('id');
                $featured = '';
                $photo = '';
                // get template var values
                foreach ($item->Source->getMany('TemplateVarResources') as $tvr) {
                    switch ($tvr->get('tmplvarid')) {
                        case 40:
                            //
                            $photo = $tvr->get('value');
                            $photo = (! empty($photo) ? $photo : '[[++theme_base_dir]]images/gallery-img3.jpg');
                            break;
                        case $relTV:
                            // featured_in_...
                            if ($tvr->get('value') == 1) {
                                ++$featuredCount;
                                $featured = 'featured';
                            }
                    }
                }
                // generate output
                $doctorOutput .= '<article class="entry '.$clinicAlias.' '.$featured.'"><div class="gallery-thumb"><img src="'.$photo.'"></div><p><a href="[[~' .$doctorID. ']]">' . $doctorName . '</a></p></article>';
            }
        }
    }
     
     
    // prepare clinic filters
    $url = $modx->makeUrl($modx->resource->get('id'));
    $url = substr($url, 0, -1).'#';
     
    $clinicOutput = '<li><a href="'. $url .'featured" class="selected" data-filter=".featured">Featured</a></li>';
    foreach ($clinics as $clinic) {
        $clinicOutput .= '<li><a href="'.$url.$clinic['alias'].'" data-filter=".'.$clinic['alias'].'">'.$clinic['title'].'</a></li>';
    }
    */
     
    $modx->setPlaceholder('doctorcount', $doctorCount);
    $modx->setPlaceholder('filter', $clinicOutput);
    $modx->setPlaceholder('doctors', $doctorOutput);
     
    return '';
    
    [ed. note: Bruno17 last edited this post 12 years, 1 month ago.]
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 22448
      • 241 Posts
      Thanks Bruno,

      that was my next step to do the joins in a query instead of relying on getCollectionGraph. I'll give it a try.
        • 22448
        • 241 Posts
        Thanks Bruno,
        it worked. I've refactored all my snippets to use pdoTools pdoFetch instead of the getCollection and getCollectionGraph methods and the pages load much faster.

        pdoTools offers functionality to replace getResources, Wayfinder, GetField, and several others. Although it does take time to figure out how it works it's well worth it.

        I still need your help with the migxResourceRelations tv setup. The grid is not sortable nor filterable and i can't figure out why. Could you pm me please. Thanks.