We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4510
    • 0 Posts
    I’ve been trying to figure out while my custom snippet doesn’t work for a while now. Anyone else that’s able to shed light on this?
    I have created all necessary files for a custom xPDO package. However, it results in "could not load class" when trying to add it.

    The purpose if this snippet is to search a custom database for sport events.

    Full snippet code is included here: https://gist.github.com/c4be8ed04e094d62d0c5 and also included below

    <?php
    $debug = isset($_REQUEST['company_debug']) ? true : false;
    
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);
    $modx->setLogTarget('ECHO');
    
    // # Add package #####################
    // if (true)
    // {
    //     $packagename = 'project1trips';
    //     $basePath = $modx->getOption($packagename.'.core_path',$config,$modx->getOption('core_path').'components/'.$packagename.'/');
    //     $assetsUrl = $modx->getOption($packagename.'.assets_url',$config,$modx->getOption('assets_url').'components/'.$packagename.'/');
    //     $config = array();
    //     $config = array_merge(array(
    //         'basePath' => $basePath,
    //         'corePath' => $basePath,
    //         'modelPath' => $basePath.'model/',
    //         'processorsPath' => $basePath.'processors/',
    //         'chunksPath' => $basePath.'elements/chunks/',
    //         'jsUrl' => $assetsUrl.'js/',
    //         'cssUrl' => $assetsUrl.'css/',
    //         'assetsUrl' => $assetsUrl,
    //         'connectorUrl' => $assetsUrl.'connector.php',
    //     ),$config);
    //     var_dump($config);
    //     $modx->addPackage($packagename,$basePath.'model/'); //added table prefix
    // } else {
    //     $config = array();
    //     $packagename = 'project1trips';
    //     $basePath = $modx->getOption($packagename.'.core_path',$config,$modx->getOption('core_path').'components/'.$packagename.'/');
    //     require_once($basePath.'model/project1trips/project1trips.class.php');
    //     $trip = new project1Trips($modx);
    // }
    //
    // $cp = $modx->getOption('core_path').'components/'.$packagename.'/model/';
    // echo $cp."<br/>";
    // $res = $modx->addPackage('project1trips',$cp); //added table prefix
    // if (!$res)
    //     echo "failed to load pack";
    // else
    //     echo "loaded pack";
    
    
    $packagename = 'project1trips';
    $basePath = $modx->getOption($packagename.'.core_path',$config,$modx->getOption('core_path').'components/'.$packagename.'/');
    $assetsUrl = $modx->getOption($packagename.'.assets_url',$config,$modx->getOption('assets_url').'components/'.$packagename.'/');
    
    echo $basePath;
    
    // $this->config = array_merge(array(
    $config = array();
    $config = array_merge(array(
        'basePath' => $basePath,
        'corePath' => $basePath,
        'modelPath' => $basePath.'model/',
        'processorsPath' => $basePath.'processors/',
        'connectorUrl' => $assetsUrl.'connector.php',
    ),$config);
    $modx->addPackage($packagename,$config['modelPath'],'modx_'); //added table prefix
    var_dump($config);
    
    
    $c = $modx->newQuery('project1trips');
    // $quotes = $modx->getCollection('project1trips', $c);
    // echo '<p>Total: '. count($quotes) . '</p>';
    
    echo "<hr />";
    // $stores = $modx->getCollection('project1trips');
    // echo 'Total: '.count($stores);
    
    # Prepare output  #####################
    $o = array();
    $o['classes'] = array();
    $o['categories'] = array();
    $o['countries'] = array();
    
    
    # Get nice values
    function generate_form_data($modx, $collection, $return_as_array = false)
    {
        # Helper function to trim input values #####################
        function trim_value(&$value)
        {
            $value = trim($value);
        }
    
        # Generate output
        foreach($collection as $col)
        {
            # Explode concatenated rows
            $values = explode('##', $col->get('value'));  //classes, category, country
            if($col->get('name') !== 'classes') continue; // or if(count($values) < 3) continue;
    
            # Split concatenated inner values and do some preprocessing
            $classes    = explode(',', $values[0]); array_walk($classes,'trim_value');
            $categories = explode('||', $values[1]);
            $country    = intval($values[2]);
    
            # Add every class into output array and effectivily removing duplicates
            foreach($classes as $class)
                $o['classes'][$class] = $class;
    
            # Add new categories into output array and effectivily removing duplicates
            foreach($categories as $category)
                if (!isset($o['categories'][intval($category)]))
                    $o['categories'][intval($category)] = $modx->getObject('modResource', $category)->get('pagetitle');;
    
            # Add new countries to output array and also get its pagetitle
            if (!isset($o['countries'][$country]))
                $o['countries'][$country] = $modx->getObject('modResource', $country)->get('pagetitle');
        }
    
        if ($return_as_array == true)
        {
            return $o;
        }
        else
        {
            # Generate category dropdown
            $tmp = "";
            foreach($o['categories'] as $key=>$val)
                $tmp .= '<option value="'.$key.'">'.$val.'</option>';
            $modx->setPlaceholder('search.categories',$tmp);
    
            # Generate countries dropdown
            $tmp = "";
            foreach($o['countries'] as $key=>$val)
                $tmp .= '<option value="'.$key.'">'.$val.'</option>';
            $modx->setPlaceholder('search.countries',$tmp);
    
            # Generate
            $tmp = "";
            $i = 1;
            foreach($o['classes'] as $key=>$val)
            {
                $tmp .= '<span>'.$val.'</span>';
                $i++;
            }
            $modx->setPlaceholder('search.classes',$tmp);
        }
        return $o;
    }
    
    
    if (!empty($_POST))
    {
        echo "<h1>POSTDATA</h1>";
        var_dump($_POST);
    
        # Prepare database query #####################
        $c = $modx->newQuery('modResource');
        $c->select(array(
          'pagetitle',
          'alias',
          'modResource.id',
          'name',
          'group_concat(tcv.value separator \'##\') value'#,
          #'fromdate',
          #'todate'
        ));
        $c->innerJoin('modTemplateVarResource','tcv',array('modResource.id=tcv.contentid'));
        $c->innerJoin('modTemplateVar','tv',array('tv.id=tcv.tmplvarid'));
        $c->innerJoin('project1trips','project1trips',array('project1trips.resource=modResource.id'));
    
        $and_values = array();
        $or_values  = array();
    
        if (isset($_POST['sport']) && $_POST['sport'] != 'null')
            $and_values[] = $_POST['sport'];
        else
            $or_values[] = 'sportcategory';
    
        if (isset($_POST['country']) && $_POST['country'] != 'null')
            $and_values[] = $_POST['country'];
        else
            $or_values[] = 'sportcountry';
    
        if (isset($_POST['checkbox-group']) && $_POST['checkbox-group'] != 'null')
            var_dump($_POST['checkbox-group']); //$and_values[] = $_POST['country'];
        else
            $or_values[] = 'classes';
    
        $c->where(array(
            'tv.name:in' => array('classes','sportcategory','sportcountry'),
            'AND:tcv.value:IN' => $and_values,
            'OR:tv.name:IN' => $or_values
        ));
    
    
        $c->groupBy('modResource.id');
        $c->sortby('modResource.id');
    
        # Debug? #####################
        // if ($debug)
        {
            $c->prepare();
            var_dump($c->toSQL());
            echo($c->toSQL());
        }
    
        # Execute query #####################
        $collection = $modx->getCollection('modResource', $c);
        foreach($collection as $col)
        {
            $values = explode('##', $col->get('value'));  //classes, category, country
            if($col->get('name') !== 'classes') continue; // or if(count($values) < 3) continue;
            var_dump($col->toArray());
        }
    
    }
    else
    {
        # Prepare database query #####################
        $c = $modx->newQuery('modResource');
        $c->select(array(
          'pagetitle',
          'alias',
          'modResource.id',
          'name',
          'group_concat(tcv.value separator \'##\') value'));
        $c->innerJoin('modTemplateVarResource','tcv',array('modResource.id=tcv.contentid'));
        $c->innerJoin('modTemplateVar','tv',array('tv.id=tcv.tmplvarid'));
        $c->innerJoin('project1trips','project1trips',array('project1trips.resource=modResource.id'));
        $c->where(array('tv.name:in' => array('classes','sportcategory','sportcountry')));
        $c->groupBy('modResource.id');
        $c->sortby('modResource.id');
    
        # Debug? #####################
        if ($debug)
        {
            $c->prepare();
            echo($c->toSQL());
            $start_process = (float) array_sum(explode(' ',microtime()));
        }
    
        # Execute query #####################
        $collection = $modx->getCollection('modResource', $c);
    
        $modx->setPlaceholder('search.output','normal'); // tell page that no form has been posted
    
        $o = generate_form_data($modx, $collection);
    // var_dump($o);
    }
    
    if ($debug)
    {
        $end_process = (float) array_sum(explode(' ',microtime()));
        echo "<h1>Execution time: ". sprintf("%.4f", ($end_process-$start_process))." seconds" . "</h1>";
    }
    return;
    ?>
    



    The custom database structure has been constructed using the excellent modExtra.
      • 21257 MODX Staff
      • 730 Posts
      If the path is right, and the name of the class is right, and the class file is named correctly, then I can’t see why this would fail.
      What’s the package name, and what’s the path and name of the class?
        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell