<?php global $modx; $getResourcesParams = array(); if (isset($startID)) { $parents = $startID; } if (isset($parents)) { $getResourcesParams['parents'] = $parents; } if (isset($sortBy) || isset($sortDir)) { $sortDir = isset($sortDir) ? strtoupper($sortDir) : 'DESC'; $sortBy = isset($sortBy) ? $sortBy : "createdon"; $getResourcesParams['sortdir'] = $sortDir; $getResourcesParams['sortby'] = $sortBy; } if (isset($filter)) { // only works for exactly two filter conditions OR'd, using filter comparisons 1 (unequal) or 2 (equals), eg, filter=`id,328,2|template,8,1` OR filter=`createdby,2,1|template,5,1` // extract the strings as so: `createdby,2,1|` => $filter_field,$filter_id,$filter_cmp $filter_comma1 = strpos($filter, ",", 0); $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1); $filter_divider = strpos($filter, "|", $filter_comma2 + 1); $filter_field = substr($filter, 0, $filter_comma1); $filter_id = substr($filter, $filter_comma1, $filter_comma2 - $filter_comma1); $filter_cmp = substr($filter, $filter_comma2, $filter_divider - $filter_comma2); // Ditto's filter excludes, whereas getResources's where filter includes, so we reverse the boolean operators // ie, Ditto &filter=`id,328,2|template,8,1` means exclude where id=328 OR template!=8, equivalent to INCLUDE where id!=328 AND template=8, equivalent to &where=`{"id:!=":328, "AND:template:=":8}` // This builds the first half, eg, translates the string `id,328,2|` to `{"id:!=":328, "AND:` $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '='; $where = '{"' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id . ', "AND:'; $filter_comma1 = strpos($filter, ",", $filter_divider); $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1); $filter_field = substr($filter, $filter_divider + 1, $filter_comma1 - $filter_divider); $filter_id = substr($filter, $filter_comma1, $filter_comma2 - $filter_comma1); $filter_cmp = substr($filter, $filter_comma2); $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '='; $where = $where . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id . '}'; $getResourcesParams['where'] = $where; } if (isset($tagData)) { $tvFilters = $tagData . '==%' . $tags . '%'; $getResourcesParams['tvPrefix'] = ''; $getResourcesParams['tvFilters'] = $tvFilters; } if (isset($total)) { $getResourcesParams['limit'] = $total; } if (isset($depth)) { $getResourcesParams['depth'] = $depth; } if (isset($tpl)) { $getResourcesParams['tpl'] = $tpl; } if (isset($tplLast)) { $getResourcesParams['tplLast'] = $tplLast; } if (isset($hideFolders)) { $getResourcesParams['hideContainers'] = $hideFolders; } if (isset($paginate)) { $getResourcesParams['element'] = 'getResources'; if (isset($display)) { $getResourcesParams['limit'] = $display; } return $modx->runSnippet('getPage', $getResourcesParams); } else { return $modx->runSnippet('getResources',$getResourcesParams); }
global $modx; $getResourcesParams = array(); if (isset($startID)) { $parents = $startID; } if (isset($parents)) { $getResourcesParams['parents'] = $parents; } else { // Ditto defaults to current document's children $getResourcesParams['parents'] = $modx->resource->get('id'); } if (isset($sortBy) || isset($sortDir)) { $sortDir = isset($sortDir) ? strtoupper($sortDir) : 'DESC'; $sortBy = isset($sortBy) ? $sortBy : "createdon"; $getResourcesParams['sortdir'] = $sortDir; $getResourcesParams['sortby'] = $sortBy; } if (isset($filter)) { // only works for exactly two filter conditions OR'd, using filter comparisons 1 (unequal) or 2 (equals), eg, filter=`id,328,2|template,8,1` OR filter=`createdby,2,1|template,5,1` // extract the strings as so: `createdby,2,1|` => $filter_field,$filter_id,$filter_cmp $filter_comma1 = strpos($filter, ",", 0); $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1); $filter_divider = strpos($filter, "|", $filter_comma2 + 1); $filter_field = substr($filter, 0, $filter_comma1); $filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1); $filter_cmp = substr($filter, $filter_comma2 + 1, $filter_divider - $filter_comma2 - 1); // Ditto's filter excludes, whereas getResources's where filter includes, so we reverse the boolean operators // ie, Ditto &filter=`id,328,2|template,8,1` means exclude where id=328 OR template!=8, equivalent to INCLUDE where id!=328 AND template=8, equivalent to &where=`{"id:!=":328, "AND:template:=":8}` // This builds the first half, eg, translates the string `id,328,2|` to `{"id:!=":328, "AND:` $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '='; $where = '{"' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id; if ($filter_divider) { $filter_comma1 = strpos($filter, ",", $filter_divider); $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1); $filter_field = substr($filter, $filter_divider + 1, $filter_comma1 - $filter_divider - 1); $filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1); $filter_cmp = substr($filter, $filter_comma2 + 1); $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '='; $where = $where . ', "AND:' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id . '}'; } else { $where = $where . '}'; } $getResourcesParams['where'] = $where; } if (isset($tagData)) { $tvFilters = $tagData . '==%' . $tags . '%'; $getResourcesParams['tvPrefix'] = ''; $getResourcesParams['tvFilters'] = $tvFilters; } if (isset($total)) { $getResourcesParams['limit'] = $total; } if (isset($depth)) { if ($depth == 0) { // Ditto depth==0 means unlimited depth $getResourcesParams['depth'] = 200; } else { $getResourcesParams['depth'] = $depth; } } if (isset($tpl)) { $getResourcesParams['tpl'] = $tpl; } if (isset($tplLast)) { $getResourcesParams['tplLast'] = $tplLast; } if (isset($hideFolders)) { $getResourcesParams['hideContainers'] = $hideFolders; } if (isset($paginate)) { $getResourcesParams['elementClass'] = 'modSnippet'; $getResourcesParams['element'] = 'getResources'; $getResourcesParams['pageVarKey'] = 'page'; $getResourcesParams['includeContent'] = '1'; $getResourcesParams['includeTVs'] = '1'; if (isset($display)) { $getResourcesParams['limit'] = $display; } return $modx->runSnippet('getPage', $getResourcesParams); } else { return $modx->runSnippet('getResources',$getResourcesParams); }
Very nice. Thanks for the contribution.
I think with a little preg_replace action, it could change the Ditto tags to getResources tags in the DB and avoid the extra overhead of calling runSnippet().
Where do you envisage the preg_replaces being? Do you mean as part of the Provisioner import process?
<?php global $modx; //$modx->log(modX::LOG_LEVEL_DEBUG, 'sortDir ' . $sortDir); //$modx->log(modX::LOG_LEVEL_DEBUG, 'sortBy ' . $sortBy); //$modx->log(modX::LOG_LEVEL_DEBUG, 'startID ' . $startID); //$modx->log(modX::LOG_LEVEL_DEBUG, 'filter ' . $filter); //$modx->log(modX::LOG_LEVEL_DEBUG, 'tagData ' . $tagData); //$modx->log(modX::LOG_LEVEL_DEBUG, 'tags ' . $tags); //$modx->log(modX::LOG_LEVEL_DEBUG, 'display ' . $display); //$modx->log(modX::LOG_LEVEL_DEBUG, 'parents ' . $parents); //$modx->log(modX::LOG_LEVEL_DEBUG, 'total ' . $total); //$modx->log(modX::LOG_LEVEL_DEBUG, 'depth ' . $depth); //$modx->log(modX::LOG_LEVEL_DEBUG, 'tpl ' . $tpl); //$modx->log(modX::LOG_LEVEL_DEBUG, 'hideFolders ' . $hideFolders); //$modx->log(modX::LOG_LEVEL_DEBUG, 'tplLast ' . $tplLast); //$modx->log(modX::LOG_LEVEL_DEBUG, 'paginate ' . $paginate); $getResourcesParams = array(); if (isset($startID)) { $parents = $startID; } if (isset($parents)) { $getResourcesParams['parents'] = $parents; } else { // Ditto defaults to current document's children $getResourcesParams['parents'] = $modx->resource->get('id'); } if (isset($sortBy) || isset($sortDir)) { $sortDir = isset($sortDir) ? strtoupper($sortDir) : 'DESC'; $sortBy = isset($sortBy) ? $sortBy : "createdon"; $getResourcesParams['sortdir'] = $sortDir; $getResourcesParams['sortby'] = $sortBy; } if (isset($filter)) { // only works for exactly two filter conditions OR'd, using filter comparisons 1 (unequal) or 2 (equals), eg, filter=`id,328,2|template,8,1` OR filter=`createdby,2,1|template,5,1` // extract the strings as so: `createdby,2,1|` => $filter_field,$filter_id,$filter_cmp $filter_comma1 = strpos($filter, ",", 0); $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1); $filter_divider = strpos($filter, "|", $filter_comma2 + 1); $filter_field = substr($filter, 0, $filter_comma1); $filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1); $filter_cmp = substr($filter, $filter_comma2 + 1, $filter_divider - $filter_comma2 - 1); // Ditto's filter excludes, whereas getResources's where filter includes, so we reverse the boolean operators // ie, Ditto &filter=`id,328,2|template,8,1` means exclude where id=328 OR template!=8, equivalent to INCLUDE where id!=328 AND template=8, equivalent to &where=`{"id:!=":328, "AND:template:=":8}` // This builds the first half, eg, translates the string `id,328,2|` to `{"id:!=":328 $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '='; $where = '{"' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id; if ($filter_divider) { $filter_comma1 = strpos($filter, ",", $filter_divider); $filter_comma2 = strpos($filter, ",", $filter_comma1 + 1); $filter_field = substr($filter, $filter_divider + 1, $filter_comma1 - $filter_divider - 1); $filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1); $filter_cmp = substr($filter, $filter_comma2 + 1); $filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '='; $where = $where . ', "AND:' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id . '}'; } else { $where = $where . '}'; } $getResourcesParams['where'] = $where; } if (isset($tagData)) { $tvFilters = $tagData . '==%' . $tags . '%'; $getResourcesParams['tvPrefix'] = ''; $getResourcesParams['tvFilters'] = $tvFilters; } if (isset($total)) { $getResourcesParams['limit'] = $total; } if (isset($depth)) { if ($depth == 0) { // Ditto depth==0 means unlimited depth $getResourcesParams['depth'] = 200; } else { $getResourcesParams['depth'] = $depth; } } if (isset($tpl)) { $getResourcesParams['tpl'] = $tpl; } if (isset($tplLast)) { $getResourcesParams['tplLast'] = $tplLast; } if (isset($hideFolders)) { $getResourcesParams['hideContainers'] = $hideFolders; } //foreach ($getResourcesParams as $key => $value) //{ // $modx->log(modX::LOG_LEVEL_DEBUG, 'getResourceParams [' . $key .'] = '. $value); //} $getResourcesParams['includeContent'] = '1'; $getResourcesParams['includeTVs'] = '1'; $getResourcesParams['prepareTVs'] = '1'; $getResourcesParams['processTVs'] = '1'; $getResourcesParams['tvPrefix'] = ''; if (isset($paginate)) { $getResourcesParams['elementClass'] = 'modSnippet'; $getResourcesParams['element'] = 'getResources'; $getResourcesParams['pageVarKey'] = 'page'; $getResourcesParams['pageLimit'] = '10'; if (isset($display)) { $getResourcesParams['limit'] = $display; } return $modx->runSnippet('getPage', $getResourcesParams); } else { return $modx->runSnippet('getResources',$getResourcesParams); }
I was thinking of a utility snippet with a bunch of getCollection() calls that would get the content of the objects and make the corrections. It would run after the import, but if you have a lot of oddball Ditto tags, it might not be worth it.