Sunday, November 28, 2010

MODx IE login problem

It seems that Internet explorer has some problem with login on "some" machine.
The culprit result to cookie issue, where by resetting cookie on the client browser seems to fix the issue.
But asking user to clear their cookie is a near to impossible things todo.

A nice tweak would be resetting the cookie via javascript on the login form itself.
Example:
document.cookie = "PHPSESSID=; __utma=; __utmc=; __utmz=; __utmb=; path=/; domain=.example.com; expires=Thu, 01-Jan-1970 00:00:01 GMT;";

Saturday, November 27, 2010

ExtJS datefield

Here is an example of a date field in extjs:

Ext.onReady(function(){
fieldname = new Ext.form.DateField({
dateFormat: 'd-m-Y', //display format
value: "2010-01-01",
selectOnFocus:true,
minValue: '2010-01-01', //optional start limit
maxValue:'2010-12-31', // optional end limit
applyTo: 'frmMyDate',
});
});

MODx 2 Datetime field

Here is an example of a datetime field in modx 2 [revo] manager:

Ext.onReady(function(){
fieldname = new Ext.ux.form.DateTime({
dateFormat: 'd-m-Y', //display format
hiddenFormat:'Y-m-d H:i:s', //actual data format
value: "2010-01-01 01:00:00",
selectOnFocus:true,
applyTo: 'frmMyDate',
});

});

If you use xtype, its named "xdatetime"

Thursday, November 25, 2010

ExtJS Get form input field handy tips

Getting the field by name:
var inputfield = target.getForm().findField("fieldname");

Hiding a field:
target.getForm().hideField(inputfield);

Showing a field
target.getForm().showField(inputfield);

Wednesday, November 24, 2010

ExtJS dateformat

To render the date format of a date column within a grid, we will need to setup the fields for the grid correctly in the config.

field: [{name: 'fieldname'}, {name:'createdon', type='date', dateFormat=''}]
dateFormat options:
  • m/d/Y or Y-m-d H:i:s
  • timestamp: Unixtime
  • time: (TODO)
To render to your own date format on the grid:
columns: [ {id:'createdon',header: _("createdon"), width: 100, sortable: true, renderer: Ext.util.Format.dateRenderer('d M Y'), dataIndex: 'createdon'}
]

Reference on more dateformat:

ExtJS Datagrid column sortable

I've just realized that if you dont set id for the columns of the datagrid,
the sorting option for the column will be disabled by default, even with sortable set to true.

,columns: [
{id:'id', header: _("id"), width: 80, sortable: true, dataIndex: 'id'},
{id:'pagetitle', header: _("pagetitle"), width: 400, sortable: true, dataIndex: 'pagetitle'}
]

Hope this help you save hours of debugging :)
cheers~

MODx 2 Manager Alert and Prompts examples

Success example:
MODx.msg.alert(_('success'), ("Saved"));

Error example:
MODx.msg.alert(_('error'), ("Some problem occur!"));

Prompt Example:
Note: code highlighted in italic are just sample code, will not work without full code.
---------------------------------------
MODx.msg.confirm({
title: "Delete Article?"
,text: "Confirm delete this article?"
,url: MODx.config.connectors_url + "resource/index.php"
,params: {
action: "delete",
id: selectedIndex
}
,listeners: {
'success': {
fn: function (A) {
if(A.success) {
MODx.msg.alert(_('success'), ("Removed"));
mygrid.refresh();
} else {
MODx.msg.alert(_('error'),A.message);
}
},
scope: this
}
}
});
---------------------------------------

_('...') is a function to output lexicon text (translation text)


Monday, November 22, 2010

ExtJS

Extjs was extended from originally the open source version of YUI.
I find that, due to lack of easy google search on documentation / tutorial on extjs (or maybe because im not good at looking for extjs keywords), i find it quite difficult to pick it up.

I've found an example for jQuery lover:
jQuery examples:
jQuery("#my_field_id") or $("#my_field_id")

ExtJS:
Ext.get("my_field_id"); // return single element with id
Ext.select("#my_field_id div span a");// return array, alike jQuery selector

I've found a good basic tutorial here:

Sunday, November 21, 2010

useful modx 2 variables

PHP Constants:
MODX_BASE_PATH - dir path to index.php (root)

MODX_ASSETS_PATH - dir path to assets
MODX_ASSETS_URL - url to assets

MODX_CONNECTORS_PATH - dir path to connector
MODX_CONNECTORS_URL - url path to connector

MODX_PROCESSORS_PATH - dir path to processor: example: core/model/modx/processors/

MODX_CORE_PATH - dir path to core

MODX_SITE_URL - domainurl
MODX_HTTP_HOST - hostname / domain

MODX_URL_SCHEME - http:// / https://

MODX_MANAGER_PATH - dir path to manager
MODX_MANAGER_URL - url to manager

$modx->context->get("key"); // get context name
$modx->context->getConfig("upload_maxsize"); // example of getting upload max size


JAVASCRIPT Constants:
MODx.config.http_host, example www.example.com
MODx.config.http_host_remote, example http://www.example.com
MODx.config.site_url , example: http://www.example.com/
MODx.config.assets_path, example "/assets/"
MODx.config.connectors_url, example "/connectors/"
MODx.config.manager_url, example "/manager/"
MODx.config.template_url, example "/manager/templates/default/"


Wednesday, November 17, 2010

New Modx 2.0.5 Profile Set

Damn cool :D
[Link here: http://bit.ly/aXTvxB]

Modx 2 getUser

Instead of calling $modx->getUser to get currently logged in user,
use getAuthenticatedUser.

Example:
$oUser = $modx->getAuthenticatedUser($modx->context->get("key"));
echo $oUser->username;
echo $oUser->getOne("Profile")->fullname;

If you use "getUser", it might return the first user in the database if the person is not logged in, which might not be what you are looking for.

hope it helps :)
Cheers~

Tuesday, November 16, 2010

Ping server from many different country

http://www.just-ping.com

DNS server temporary down

Update: DNS service is up and running...

Update 6.10pm:
Some connection seems okay, but still unstable on many other network...

Hi,
right now, we are having some packet lost on the network to our dns server.
Please be patient and wait for the network team to work on the issue.

best regards,
James

Sunday, November 14, 2010

Modx 2 How does getService works?

modx->getService($name, $class= '', $path= '', $params= array ())

name = name to be assigned to modx->$name and modx->services[$name]
class = classname (resolving to "classname".class.php)
path = path to look for class
params = array of value pass to class.

Example:

getService("login", "Login", corepath."/components/login", ...);
Resolve to:
core/components/login/Login.class.php
initiate class __construct(modxobject, $params);



Wednesday, November 10, 2010

Modx 2 [revo] with PHPUnit

Test driven development is quite important in developing useful code.
As we test each single functionality, we might find some bug which may exists in the framework we are using.

I'm going to show how phpunit could be use with modx for testing.
First, install phpunit through pear module, which the reference can be found here:

http://www.phpunit.de/manual/current/en/installation.html

Once installed, you can now starting coding by putting up these codes above your test class:
----------------------
define('MODX_API_MODE', true);
//define("MODX_HTTP_HOST", ""); //this is optional, set it to the url of your context
global $modx;
require(dirname(dirname(__FILE__)) . "/index.php"); //path to modx root/index.php
$modx->getRequest();
----------------------

Now, we are ready to create the test unit.
Example:
--------------------------
class TestDB extends PHPUnit_Framework_TestCase {

public function testSuite() {
$this->doTestDatabase();
}

public function doTestDatabase() {
global $modx;
$user = $modx->getObject("modUser", 1);
$this->assertEquals(1, $user->get("id"));
}
}
--------------------------
Running the test:
phpunit TestDB.php

Hope it helps ya :)
Cheers~

XPDO save with addOne

updates: dec 16: My mistake, ive used addOne on a one to many relation, therefore, should use addMany instead of addOne..

-------------

im not sure if this is a bug or the way it should be working.

It seems that if we addOne() to a new object, and save the parent object, the child object and parent object get saved.

But if we edit an existing parent object, and use addOne, the child object did not get saved when i call parentobject->save()...
Please note that the parent object is not modified in my case.


updates: dec 13: relying on addOne to save record is not recommended. Best to set the foreign key value manually.

Hope this help ya :)
Cheers~

Sunday, November 7, 2010

Modx 2 creating child on a protected resource

It seems that, there is some behavior in modx 2 which works kind of odd.
With a resource assigned to resource group, user will need to have 2 different policies under user group to be able to create child under the protected resource.

+ Resource group: "your resource group name", min role: "Member", Access policies: "Administrator", Context: "mgr"
+ Resource group: "your resource group name", min role: "Member", Access policies: "Resource<", Context: "mgr"

Hope it helps for ya ;)
Cheers~

Wednesday, November 3, 2010

Modx 2 Limiting manager screen user to certain context

How do i protect my resources and allow only certain context editable by manager users A?

Filter out context #1 , and only allow context #2 to be edited by user A
First:
Grant permission to the other context (not to be shown to user A) to administrator user group
- Edit Adminisrator user group
- Context Access
- Add context:
+ Context: #1
+ Minimum role: 0
+ Access policy: Administrator

2nd:
Create a role for user A, with ID less than 9999, but above authority level 0, example:
- Role: "Editor"
- authority level: 1000

3rd:
Create the user group for user A:
Example:
- "Editors"

4th: Add user A to user group "Editors"
Security: Access Control: User groups, right click, edit user group "Editor"
Users: Add user to group
Example:
- User: User A
- Role: "Editor"

5th: Goto: Security: Access Control: Access Policies
Right click resource policy, duplicate.
Edit "Duplicate of Resource" policy, permission tab.
Add "resource_tree", and rename policy name to "ResourceTree" and save.
*Note: this will allow showing of resource tree when user is editing context #2


6th: Goto: Security: Access Control: Access Policies, Add policy:
Example:
- Policy: "Editor admin", save. (to be used in manager context for user A)
Go to permission tab.
Add the following permissions:
+ change_profile
+ class_map
+ countries
+ edit_document
+ frames
+ help
+ home
+ load
+ logout
+ resource_tree
+ save_document
+ view
+ view_document
+ new_document
Save.

7th: Goto: Security: Access Control
Right click on "Editors", update user group, Goto context access tab.
Add 3 type of context:
1. Context: mgr, min role: "Editor" (1000), Access policy: "Editor admin"
2. Context: "Context #2", min role: "Editor" (1000), Access policy: "ResourceTree"
3. Context: "Context #2", min role: "member" (9999), Access policy: "Load, list and view"


Now you shall have User A be able to access and edit document in context #2, but context #1 will be hidden from User A.
And anyone logged in as Member of user A will only be able to load,list and view resources created by User A.

And please remember to flush permissions, and relogin the user to test.

How about limiting resources in Context?

This can be done by using resource group.
Example:
Those resources that you do not wish to allow user to edit resource group A
While those resource you wish to allow user to edit as resource group B

1st, setup the resource group A and resource group B.
Then assign resource to resource group accordingly.

2nd, update usergroup "adminisrator",
And goto "Resource group access", and add 2 access:
- Resource group A; min-role: super user; policy: administrator; Context: mgr
- Resource group B; min-role: super user; policy: administrator; Context: mgr
*Note: the above set both group A and B be accessible by administrator users

3rd, update usergroup "Editor",
goto "Context access" tab:
Add 2 access:
- context: mgr; min-role: "editor"; policy: "Editor admin" (as setup above)
- context: "web/mycontext/context #2", min-role: "editor", policy: "load, list, view" (this will allow resource_tree to list resources within the context"

Then, goto "resource group access" tab:
Add access:
- resource group: "resource group B", min-role: "editor"; policy: "administrator", context: mgr (this will allow view/editing/new resource)

Done :)
Hope it helps for you...
Cheers~

Modx 2 Save bar missing

If you ever encounter save bar went missing when you try to edit the resources,
you will realise it only happen to weblink / symlink type of resources.

I realise this is due to the problem of tiny mce plugin, which in my case, might be obsolete due to some upgrading issue.

What you may do is keep uninstall and force-removal on tinymce plugin until you no longer see any version of it.
Then, in package manager, re-add the package again.
Hope it works for ya :)

Cheers~

Tuesday, November 2, 2010

Modx 2 protected content

Modx default permission setting will redirect user to an unauthorised page when the user did not have permission to view the page.

What if you want to show a teaser of the document?
Here is how:
Create the resource, and do not set the page permission.
Instead, set it to a template which output the resource content by a snippet.

Example template:
[[!out.content? &content=`[[*content]]` &guest=`[[*introtext:empty=`[[*content:ellipsis=`600`:striptags]]`]]` &role=`myrole`]]
-------------
This example above will pass in summary of 600 charactors text for guest while output the full content on Logged in with role specified

-------------
Example snippet:
-------------
$aGroups = $modx->getUserDocGroups(true);
if ($aGroups != null) {
foreach($aGroups as $sGroup) {
if (strtolower(trim($sGroup)) == strtolower(trim($role)) ) {
return $content;
}
}
}

return $guest;

Hope it helps ;)
Cheers~