We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14281
    • 120 Posts
    I hope this is the right forum to post this.

    I created a function for the DocumentParser class that you guys might find useful. It’s pretty simple; just a moderate modifation of the getActiveChildren class. It gets all of the active descendants of a page, and rather than placing them in some sort of hierarchical array, it just clumps them all together as if there were no hierarchy. This has been a necessary function for me on several occasions:

    function getActiveDescendants($id=0, $sort='menuindex', $dir='ASC', $fields='id, pagetitle, description, parent, alias, menutitle') {
    	$tblsc = $this->getFullTableName("site_content");
    	$tbldg = $this->getFullTableName("document_groups");
    	
    	// modify field names to use sc. table reference
    	$fieldsFormatted = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$fields)));
    	$sortFormatted = 'sc.'.implode(',sc.',preg_replace("/^\s/i","",explode(',',$sort)));
    	// get document groups for current user
    	if($docgrp = $this->getUserDocGroups()) $docgrp = implode(",",$docgrp);
    	// build query
    	$access = ($this->isFrontend() ? "sc.privateweb=0":"1='".$_SESSION['mgrRole']."' OR sc.privatemgr=0").
    	  (!$docgrp ? "":" OR dg.document_group IN ($docgrp)");
    	$sql = "SELECT DISTINCT $fieldsFormatted FROM $tblsc sc
    	  LEFT JOIN $tbldg dg on dg.document = sc.id
    	  WHERE sc.parent = '$id' AND sc.published=1 AND sc.deleted=0
    	  AND ($access)
    	  ORDER BY $sortFormatted $dir;";
    	$result = $this->dbQuery($sql);
    	$resourceArray = array();
    	for($i=0;$i<@$this->recordCount($result);$i++)  {
    	  array_push($resourceArray,@$this->fetchRow($result));
    	}
    	
    	if(count($resourceArray) > 0) {
    		foreach($resourceArray as $resource) {
    			$newresource = $this->getActiveDescendants($resource['id'],$sort,$dir,$fields);
    			$resourceArray = array_merge($newresource,$resourceArray);
    		}
    	}
    	
    	return $resourceArray;
      }


    I hope it helps. Also, let me know if there is an existing way to accomplish this that is simpler. Thanks alot.
      • 25663 MODX Staff
      • 12,272 Posts
      Out of curiosity, what was the application that made this necessary? Nice work and look forward to more feedback on this.
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 14281
        • 120 Posts
        I needed to pull a Random article out of a folder that was separated into category folders. On the front page, a snippet needed to pull a random article out of the entire collection of articles. Once you went to the articles section of the sent, the visitor needed to be allowed to browse the articles by categories.

        Clumping the Article folders descendants together into a 1-dimensional array using this function, and rewriting the ShowRandom snippet so that it referred to the getActiveDescendants function, made this possible.
          • 6726
          • 7,075 Posts
          Pretty interresting, I might have a use for it in the near future, I’ll report by then.
          Thanks !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id