We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6094
    • 60 Posts
    So in a recent post (http://modxcms.com/forums/index.php/topic,59504.0.html), I requested help for CSV output from a custom database. Got great help from Bob, many kudos to him.

    I’m now at a point where I would like to add this file as a custom manager page. I’ve followed this tutorial(http://rtfm.modx.com/display/revolution20/Custom+Manager+Pages+Tutorial) and recreated this solution with no issue. I’m now trying to use my page created to export the CSV, but I’m running into issues. Here are my steps to this point.


    Here is the code located in index.php

    <?php
    $path = MODX_CORE_PATH . 'components/brochure/';
    $result = $modx->addPackage('brochure',$path . 'model/','stonemill_');
    $filename ="excelreport.csv";
    $brochures = $modx->getCollection('Brochure'); 
    
    
    foreach($brochures as $brochure) {
    
    $output .= implode(',', $brochure->toArray()) . "\n";
    
    }
    
    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename='.$filename);
    return $output;
    
    ?>


    I’m getting the following error:

    Fatal error: Call to a member function getCollection() on a non-object in /home/ctownsend/christownsend.net/core/components/brochure/index.php on line 4
    I’m figuring the xPDO features do not work in this setting, but I’m not sure how to achieve this otherwise.

    Any direction would be helpful. Thanks.
      • 28215
      • 4,149 Posts
      Have you built the classes and maps for your custom tables? What’s your schema file look like?
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 6094
        • 60 Posts
        Yes, everything has been done per Bob’s Guide on creating a Custom DB Table.

        The schema file looks like this:

        <?xml version="1.0" encoding="UTF-8"?>
        <model package="brochure" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
        	<object class="Brochure" table="brochure" extends="xPDOSimpleObject">
        		<field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
        		<field key="street" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
        		<field key="city" dbtype="varchar" precision="40" phptype="string" null="false" default="" />
        		<field key="state" dbtype="varchar" precision="2" phptype="string" null="false" default="" />
        		<field key="zip" dbtype="varchar" precision="20" phptype="string" null="false" default="" index="index" />
        		<field key="email" dbtype="varchar" precision="100" phptype="string" null="false" default="" index="index" />
        
        		<index alias="email" name="email" primary="false" unique="false" type="BTREE" >
        			<column key="email" length="" collation="A" null="false" />
        		</index>
        		<index alias="zip" name="zip" primary="false" unique="false" type="BTREE" >
        			<column key="zip" length="" collation="A" null="false" />
        		</index>
        		<index alias="name" name="name" primary="false" unique="false" type="FULLTEXT" >
        			<column key="name" length="" collation="" null="false" />
        			<column key="street" length="" collation="" null="false" />
        			<column key="city" length="" collation="" null="false" />
        			<column key="state" length="" collation="" null="false" />
        			<column key="zip" length="" collation="" null="false" />
        			<column key="email" length="" collation="" null="false" />
        		</index>
        	</object>
        </model>


        It’s located here "core/components/brochure/schema"

        Originally, the following code was just loaded in a resource from a snippet call and it worked fine:

        <?php
        $path = MODX_CORE_PATH . 'components/brochure/';
        $result = $modx->addPackage('brochure',$path . 'model/','stonemill_');
        $filename ="excelreport.csv";
        $brochures = $modx->getCollection('Brochure'); 
        
        
        foreach($brochures as $brochure) {
        
        $output .= implode(',', $brochure->toArray()) . "\n";
        
        }
        
        header('Content-type: application/ms-excel');
        header('Content-Disposition: attachment; filename='.$filename);
        return $output;
        
        ?>


        However, I pulled the code from the snippet and loaded it into index.php and now it’s throwing the error when I load http://www.christownsend.net/core/components/brochure/index.php.
          • 4172
          • 5,888 Posts
          Loaded http://christownsend.net/core/components/brochure/index.php into the browser only to receive an error

          Of course you cannot run this script directly without initialising modx.
          Did you create an action and did you try to run that from manager?

          There is also an easy way to create CMPs from snippets.
          See my signature.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 6094
            • 60 Posts
            I’ve tried your suggestion in using the CMPs from snippets code and that’s not really working. I see that it might, but instead of loading the CSV file with just the data from the snippet as it did before it loads a lot of HTML code in the page.
            						
            <div id="mymoduls-panel" class=" x-tab-panel modx-tabs">						
                <div class="x-tab-panel-header x-unselectable x-tab-panel-header-plain" id="mymoduls-panel-header" style="-moz-user-select: none;">						
                    <div class="x-tab-strip-wrap" id="mymoduls-tab-container" style="width: 931px;">						
                        <ul class="x-tab-strip x-tab-strip-top" id="ext-gen66">						
                            						
                            <li class="x-tab-strip-active">						
                                <a href="?a=72&runmymodul=ShowQuotes" class="x-tab-right"><em class="x-tab-left"><span class="x-tab-strip-inner"><span class="x-tab-strip-text ">ShowQuotes</span></span></em></a>						
                            </li>						
            "	"						
                            <li class="x-tab-edge" id="ext-gen67">						
                            </li>						
                            <div class="x-clear" id="ext-gen68">						
                            </div>						
                        </ul>						
                    </div>						
                    <div class="x-tab-strip-spacer" id="ext-gen65">						
                    </div>						
                </div>						
                <div class="x-tab-panel-bwrap" id="ext-gen62">						
                    <div class="x-tab-panel-body x-tab-panel-body-top" id="ext-gen63" style="padding: 1em; height: auto;">						
                        <div id="ext-comp-1025" class=" x-panel x-panel-noborder" style="width: 929px;">						
                            <div class="x-panel-bwrap" id="ext-gen69">						
                                <div class="x-panel-body x-panel-body-noheader x-panel-body-noborder" id="ext-gen70" style="height: auto; width: 929px;">						
                                    <div id="ext-comp-1026" class=" x-panel x-panel-noborder">						
                                        <div class="x-panel-bwrap" id="ext-gen71">						
                                            <div class="x-panel-body x-panel-body-noheader x-panel-body-noborder" id="ext-gen72">						
            1	Chris Test	111 Test Dr.	Knoxville	TN	37922	[email protected]
            2	Joe Jones	111 Test Street	Knoxville	TN	37922	[email protected]
            						
                                                <br>						
                                            </div>						
                                        </div>						
                                    </div>						
                                </div>						
                            </div>						
                        </div>						
                    </div>						
                </div>						
            </div>						
            


            I really just need the CSV file to output the following:
            1	Chris Test	111 Test Dr.	Knoxville	TN	37922	[email protected]
            2	Joe Jones	111 Test Street	Knoxville	TN	37922	[email protected]


            The snippet that I am calling works fine doing this, but not when it’s called from your code.
              • 6094
              • 60 Posts
              Ok, little bit of work but I managed to get it to work with your code once I looked at it a bit more. I needed to strip all of the HTML code you had placed and just have it output the snippet code. I modified the code to this:

              <?php
              
              /* setup default properties */
              $isLimit = !empty($scriptProperties['limit']);
              $start = $modx->getOption('start',$scriptProperties,0);
              $limit = $modx->getOption('limit',$scriptProperties,20);
              $sort = $modx->getOption('sort',$scriptProperties,'name');
              $dir = $modx->getOption('dir',$scriptProperties,'ASC');
              
              $modulcategory='mymoduls';
              $action=$_REQUEST['a'];
              
              /* query for snippets in modulcategory */
              $c = $modx->newQuery('modSnippet');
              $c->select('modSnippet.id,modSnippet.name');
              $c->sortby($sort,$dir);
              $c->leftJoin('modCategory','modCategory','`modCategory`.`id` = `modSnippet`.`category`');
              $c->where('`modCategory`.`category`="'.$modulcategory.'"');
              if ($isLimit) $c->limit($limit,$start);
              //$c->prepare();
              //echo $c->toSql();
              $snippets = $modx->getCollection('modSnippet',$c);
              $count = $modx->getCount('modSnippet');
              
              /* iterate through snippets */
              
              $snippetnames = array();
              foreach ($snippets as $snippet) {
                  $snippet=$snippet->toArray();
              	$snippetnames[]=$snippet['name'];
              
              }
              if (isset($_REQUEST['runmymodul'])&& in_array($_REQUEST['runmymodul'],$snippetnames)){
              $modulname=$_REQUEST['runmymodul'];
              $moduloutput=$modx->runsnippet($modulname);	
              }
              
              if (count ($snippetnames)>0){
              foreach ($snippetnames as $name) {
              	$class= $name==$modulname ?'x-tab-strip-active':'';
              	$tabs.='
                              <li class="'.$class.'">
                                  <a href="?a='.$action.'&runmymodul='.$name.'" class="x-tab-right"><em class="x-tab-left"><span class="x-tab-strip-inner"><span class="x-tab-strip-text ">'.$name.'</span></span></em></a>
                              </li>
              	';		
              }	
              }
              $output.= $moduloutput;
              
              
              return $output;
              
              ?>


              Seems to be working now.
              Thanks for your help.