Page 1 of 1

How to add options for backend users when creating/editing content

Posted: Fri Jul 18, 2008 5:46 am
by oi_antz
Hi all, I'm real sold on CMSms, lovely to work with and such beautiful code. I got a hang on how to write modules, but there's still more that I probably should learn before I get all "set in my ways" lol.

Is it possible for my module to add extra fields to the create content and edit content forms? I want to have a drop menu that allows staff to select which user groups are allowed to view the page. Of course, I would prefer if this could be done with a hook or some similar technique rather than go hacking at the core files.

Any comments and links to relevant documentation is greatly appreciated, cheers!

Re: How to add options for backend users when creating/editing content

Posted: Fri Jul 18, 2008 6:31 am
by cyberman
Not sure how much do you have read :) ...

http://www.cmsmadesimple.org/apidoc/

Re: How to add options for backend users when creating/editing content

Posted: Fri Jul 18, 2008 9:50 am
by oi_antz
Cheers for the link.

Re: How to add options for backend users when creating/editing content

Posted: Fri Jul 18, 2008 1:19 pm
by oi_antz
Can't see anything in there that does it, and the form appears to be hard-coded into admin/editcontent.php...

I tracked down the origin of the looped form elements, defined in Content.inc.php line 209 :

Code: Select all

<?php	      
            $additionalcall = '';
	    foreach($gCms->modules as $key=>$value)
	    {
		if (get_preference(get_userid(), 'wysiwyg')!="" && 
		    $gCms->modules[$key]['installed'] == true &&
		    $gCms->modules[$key]['active'] == true &&
		    $gCms->modules[$key]['object']->IsWYSIWYG() &&
		    $gCms->modules[$key]['object']->GetName()==get_preference(get_userid(), 'wysiwyg'))
		    )
		{
		    $additionalcall = $gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
		}
	    }
... I'm thinking of adding something similar that will allow modules to insert form fields by just returning an array from a method like :

Code: Select all

<?php
class MyModule
{
    function GetCustomContentFields(){ return array(lang('label'), '<input name="whatever" />', 'MethodToCallOnSubmit'); }
}
... see any problems or room for improvement?

Re: How to add options for backend users when creating/editing content

Posted: Fri Jul 18, 2008 1:23 pm
by oi_antz
Returns an array of arrays, so can do multiple fields. Then the 3rd parameter 'MethodToCallOnSubmit' is not required for each field, it could be just a set method in the plugin class:

Code: Select all

<?php
class MyModule 
{
    function PostContentSubmit(){
        if(count($_POST)>0){
            // do something with the submitted data
        }
    }
}
Will have a crack at it today.

Re: How to add options for backend users when creating/editing content

Posted: Sat Jul 19, 2008 2:09 am
by oi_antz
Part one of the solution:

Open lib/classes/contenttypes/Content.inc.php

Find the point where $additionalcall gets defined (around line 209).

Replace the existing code:

Code: Select all

<?php
  $additionalcall = '';
	    foreach($gCms->modules as $key=>$value)
	    {
    		if (get_preference(get_userid(), 'wysiwyg')!="" && 
    		    $gCms->modules[$key]['installed'] == true &&
    		    $gCms->modules[$key]['active'] == true &&
    		    $gCms->modules[$key]['object']->IsWYSIWYG() &&
    		    $gCms->modules[$key]['object']->GetName()==get_preference(get_userid(), 'wysiwyg'))
    		{
    		    $additionalcall = $gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
    		}
	    }
With the following code, noting the additional statements that "get custom form fields" :

Code: Select all

<?php
  $additionalcall = '';
	    foreach($gCms->modules as $key=>$value)
	    {
    		if (get_preference(get_userid(), 'wysiwyg')!="" && 
    		    $gCms->modules[$key]['installed'] == true &&
    		    $gCms->modules[$key]['active'] == true &&
    		    $gCms->modules[$key]['object']->IsWYSIWYG() &&
    		    $gCms->modules[$key]['object']->GetName()==get_preference(get_userid(), 'wysiwyg'))
    		{
    		    $additionalcall = $gCms->modules[$key]['object']->WYSIWYGPageFormSubmit();
    		}
    		// get custom form fields
    		if(method_exists($gCms->modules[$key]['object'], 'GetContentFields')){
            $extraFields = $gCms->modules[$key]['object']->getContentFields();
            if(!is_array($extraFields)) continue;
            foreach($extraFields as $extraField){
                if(!is_array($extraField) || count($extraField)!=2) continue;
                $ret[] = $extraField; 
            };
        };
    		
	    }
... so far, this will allow any module to add form fields to the "Add new page" and "Edit existing page" pages in the admin section, under content > pages

Your module will need to provide a method called "GetContentFields", something like this:

Code: Select all

<?php
class FEUsers extends CMSModule
{
function GetContentFields(){ 
    return array(
        array('Access Group', '<select name="access_groups"><option value="public">Public</option></select>')
    );
}
}
Now onto step two - processing the data...

Re: How to add options for backend users when creating/editing content

Posted: Sat Jul 19, 2008 2:21 am
by oi_antz
Well, as it turns out, there is already a call that can be utilized for this:

$pluginObj->ContentEditPre($contentObj);

or

$pluginObj->ContentEditPost($contentObj);

That's nice, reduces the number of hacks I have to keep track of.

Hope this is useful to someone wanting to add custom fields to the content forms from within their module.

Re: How to add options for backend users when creating/editing content

Posted: Sat Jul 19, 2008 2:27 am
by oi_antz
It is worthwhile pointing out that when creating new pages, the page id $contentObj->Id() is not set until ContentEditPost(). So if your module depends on knowing the page id, you will need to use ContentEditPost() for your processing logic.

Re: How to add options for backend users when creating/editing content

Posted: Sat Jul 19, 2008 2:39 am
by oi_antz
Here is the resulting file. Make sure you backup the existing file before overwriting it!

download the file attached to the next reply, it has been updated

Re: How to add options for backend users when creating/editing content

Posted: Mon Jul 21, 2008 9:50 pm
by oi_antz
Updated the file, the call to $ModuleName->GetContentFields() now passes the content object so you can get the page id from it.

eg:

Code: Select all

<?php
function GetContentFields($contentObj){ 
    $DB = Antz::getDb('cmsms');
    //$DB->debug(true);
    //$DB->showErrors(true);
    $pageid = (int) $contentObj->Id();
    if($pageid > -1){
        // page exists, get the groups
        $currentGroups = $DB->fetchAll("SELECT * FROM ".cms_db_prefix()."fe_pages_to_groups WHERE page_id = '{$pageid}'");
        $cg = array();
        foreach($currentGroups as $k=>$v){
            $cg[$k] = $v['group_key'];
        };
    }else{
        $cg = array('public');
    };
    $accessGroups = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}fe_user_groups");
    $opts = array();
    foreach($accessGroups as $k=>$v){
        $selected = (in_array($v['keyname'], $cg)) ? 'selected="selected"' : '';
        $opts[] = '<option value="'.$v['keyname'].'" '.$selected.'>'.$v['title'].'</option>';
    };
    return array(
        array('Access Groups:', '<select name="access_groups[]" multiple="multiple">'.implode("\n", $opts).'</select>')
    );
}