Wednesday, October 20, 2010

Modx 2 get document by alias

$columns = array_keys($modx->getFields('modResource'));
$criteria = $modx->newQuery('modResource');
$criteria->setClassAlias('sc');
$criteria->select($columns);
$criteria->andCondition("sc.published=1 and sc.deleted=0 and sc.alias='" . mysql_escape_string($alias) . "'");
$criteria->limit(1);
//$criteria->prepare();
//return $criteria->toSQL();
$objCollection = $modx->getCollection('modResource', $criteria);
$collection = array();
foreach ($objCollection as $obj) {
array_push($collection, $obj->get($columns));
}
if (count($collection) <>
return $collection[0]["content"];

Modx 2 snippet bummer

If you code [[*value]] in snippet php coding, you will realise the value gets populated when you return it.
But, if you are using the value in [[*value]] to check some condition, then you are out of luck.
The real value isnt populated during the script process.
Its only populated after its been processed.

Alternative way is to pass in value to snippet:
[[!mysnippet? &myvalue=`[[*value]]`]


Modx 2 processed tv to display

Previously ive talked about using "input options value" to output list of keys and values for dropdown list or options.
example: "@select listlabel, listvalue from modx_listvalues where listkey='postcategory' order by weight asc, listlabel asc, id asc"

But to output the value, its not the same.

if you installed getResourceField,
adding processTV=`1` and useTV=`1` field=`your_tvfieldname`

processedTV is obtained from the value in the TV variable.
So instead of the value directly "test", you have to put in your binding syntax as value.
Example: "@select listlabel from modx_listvalues where listkey='postcategory' and listvalue='test';

If you dont like the way of putting syntax into the value box, you may try to use snippet to pass in the value to get the value to display. I think this is the better way.

Thursday, October 14, 2010

Modx 2 Login Snippet

The login snippet installable from package management have several flaw in certain usage.
We have a client who has login form @ the very top.

When user login into the site, the user will be redirected to to site_url (start) page if the last page is his/her first visit or landing page.
@solution: Edit Login snippet, and comment off line #155
//$modx->sendRedirect($modx->getOption('site_url'));

2nd problem, since we have disabled redirect, user will be known as "not yet authenticated" in the current page, unless he/she click to another page.
@solution: Edit Login snippet, and append this line #148
$authenticated = true;

Modx 2 query for template variables

This example shows how to query for template variable from a article:

SELECT cv.*, v.`name` FROM `modx_site_tmplvar_contentvalues` cv left join `modx_site_tmplvars` v on cv.tmplvarid=v.id
WHERE `contentid` = #

Wednesday, October 13, 2010

Modx 2 showing error page instead of unauthorize page

In modx 1, its a direct method to setup article / post permission.
1st, setting up resource group. Then setup user group, and add in resource group to user group.
Next, assign user to user group.
Finally, when someone not logged in, the user will be shown a unauthorized page.

But in modx2, there are extra steps to setup this.
Notice that modx2 now have "minimum role", "access policy", "context".
1st. setting up resource group. Then setup user group. Right click on user group, and click "Update user group".
Click on "Resource Group Access" tab, "add resource group"
Select the resource group you created.
Minimum Role: Member (9999)
Access policy: "Load, list and view"
Context: Web

This allow user of type member to load the document, or list it out in rss and also view the document.

Now that you have protected this page, you will notice the unauthorised user will see a error page when trying to view this page. By right, the guest should see the unauthorized page instead...

Okay, to fix this, Go to "Access Control",
Right click on "(Anonymous)", and click "Update user group".
Click on "Resource Group Access" tab, "add resource group".
Select the resource group you created.
Minimum Role: Member (9999)
Access policy: "Load"
Context: Web

This will allow guest to load the document, but will not allow guest to list or view the document. This is how modx2 policy works.

Hope it helps ;)

Tuesday, October 12, 2010

Modx 2 Bummer in XPDO Log

It seems that modx developer have did a mistake by setting debug true/false to xpdo debug with php ERROR LEVEL constant

having : int(30711) for E_ALL & ~E_NOTICE in core/model/modx/modx.class.php line 453:
parent :: setDebug(intval($debug)); // isn't going to make any sense to xpdo debug

to fix it, ive manually
set it to true.

output path:
core/cache/logs/error.log