Blog

Squiz Matrix Custom PHP Development

Sun, Oct. 4, 2009

I spent a good part of my day working on the MySource Matrix Community, which is soon to launch. In doing so, I realized that the MySource Matrix Javascript API was lacking a few features that I needed. I wanted to restrict to a few different root nodes, but my asset only allowed for one to be added. So, I went off looking through other Matrix assets to understand how I could add a root node chooser that allows multiple roots.

  • iphone

In order to add the new multiple asset finder (as Matrix calls it) I had to add my own custom print and process functions to that attribute. For these functions I used the following:

/**
* Paints the root node selection box
*
* @param object &$asset asset being painted
* @param object &$o             backend outputter
* @param string $prefix prefix for the html doc element name
*
* @return boolean
* @access public
*/
function paintRootNode(&$asset, &$o, $prefix)
{
        $write_access = $asset->writeAccess('attributes');
        $curr_list = $asset->attr('root_node');
        
        if ($write_access) {
                multiple_asset_finder($prefix.'_roots', $curr_list, Array('asset' => 'D'));
        } else {
                if (empty($curr_list)) {
                        echo translate('js_api_no_root_nodes_selected');
                } else {
                        ?><ul style="margin:1px 15px;"><?php
                        foreach ($curr_list as $assetid) {
                                echo '<li>'.get_asset_tag_line($assetid).'</li>';
                        }
                        ?></ul><?php
                }
        }

        return $write_access;

}//end paintRootNode()
/**
* Processes the value input from root node selection box
*
* @param object &$asset asset being painted
* @param object &$o             backend outputter
* @param string $prefix prefix for the html doc element name
*
* @return void
* @access public
*/
function processRootNode(&$asset, &$o, $prefix)
{
        $roots = array_get_index($_POST, $prefix.'_roots', Array());
        $root_list = Array();

        foreach ($roots as $root) {
                if (!empty($root['assetid'])) {
                        $root_list[$root['assetid']] = $root['assetid'];
                }
        }

        return $asset->setAttrValue('root_node', $root_list);

}//end processRootNode()

The important part of the first function, that actually prints the root node selector, is the multiple_asset_finder() function. This takes a few different parameters, including the assets prefix and an array of values saved in the asset attribute.

In our processing function, we have to manually grab our newly set values from the $POST array and set them as attributes using the setAttrValue() function.  Once this was done, we finally had an asset chooser that could selected multiple root nodes.

My other two tasks were to add a new attribute called ignore_permissions and remove the API key from displaying unless a user had acquired a lock.  Both of these were relatively easy to add.  The ignore_permissions functionality only required some small changes in the js_api.inc file as well as adding it to the normal places to add a new attribute.  It was added so that a user does not have to have permissions in order to call functions such as createAsset() or setAttributes().  

Making sure that the API key is not displayed until a user has acquired a lock is a security precaution.  If you have users in your system that have read access to assets, they could easily view the details screen of an API asset and copy down the API key.  Now, with this additional feature, users will have to be able to get locks on the attributes before seeing the key, meaning that they have to have write access to the asset.

All in all, it was a nice day of programing and getting work done on the MySource Matrix Community.  As the Community continues to evolve, I am sure there will be more features that I will need to write into the API.  

I will be submitting these new features to Squiz, so I am assuming they would make it into 3.24.2 next month.

Nicholas Hubbard
Owner

Add Comment