Creating a custom field with the Projects list

Get help from other users here.

Moderators: Developer, Contributor

Post Reply
fdot
Posts: 5
Joined: Feb 24, 2012 8:37 am

Creating a custom field with the Projects list

Post by fdot » Feb 24, 2012 8:48 am

Hello Mantis community :) !

I am currently using Mantis with the following project organisation :

Project 1
-Sub-project 2
--Sub-Sub-project 3
Project 2
...

I would like to create a custom dynamic field which is displaying the list of all the sub-project and sub-sub-project of a project.

I have made several tests based on : http://www.mantisbt.org/docs/master-1.2 ... STOMFIELDS

And the only test which worked is the following one :

Code: Select all

function custom_function_override_enum_projects() {
	
    $t_projects = project_get_all_rows();

    $t_enum = array();
	foreach( $t_projects as $t_project ) {
		$t_enum[] = $t_project['name'];
	}
	
    $t_possible_values = implode( '|', $t_enum );

    return $t_possible_values;
}
But this function is returning the list of all the projects. I have made some tests with others functions than project_get_all_rows but nothing worked.

Is someone got an idea on how I can do ? Or which function I can use ?

Thanks a lot in advance for your help :)

istvanb
Posts: 226
Joined: Aug 22, 2010 4:00 pm

Re: Creating a custom field with the Projects list

Post by istvanb » Feb 24, 2012 9:06 am

You should check the manage_proj_edit_page.php, since the manage project page displays all the subprojects, so you probably can leverage some code from there. Without spending more than 1 minute on this I would start with these lines:

$t_subproject_ids = current_user_get_accessible_subprojects( $f_project_id, /* show_disabled */ true );
if ( Array() != $t_subproject_ids
...

Most likely (but I can not confirm at this moment) the $f_project_id, must be the current project which against the actual issue is getting submitted. Then you simply need to enumerate the $t_subproject_ids array.

fdot
Posts: 5
Joined: Feb 24, 2012 8:37 am

Re: Creating a custom field with the Projects list

Post by fdot » Feb 24, 2012 10:22 am

Hi istvanb,

Thanks a lot for the help.

I have tried to make some tests based on manage_proj_edit_page.php :

Code: Select all

function custom_function_override_enum_projects() {
	
    $t_enum = array();

    $t_project_ids = current_user_get_accessible_subprojects( $f_project_id, /* show_disabled */ true );
	if ( Array() != $t_project_ids ) {
		foreach ( $t_project_ids as $t_project_id ) {
				$t_project = project_get_row( $t_project_id );
				$t_enum[] = $t_project['name'];	
		}
	}
    $t_possible_values = implode( '|', $t_enum );

    return $t_possible_values;
}
But it is displaying nothing. To be honest I am completly lost :/

istvanb
Posts: 226
Joined: Aug 22, 2010 4:00 pm

Re: Creating a custom field with the Projects list

Post by istvanb » Feb 24, 2012 10:41 am

No problem especially since you are lost :)

The query for the subproject IDs is correct (I have checked it).

but you made some mistakes.

1, the if structure is not correct (evaluated as false, so the rest of the code wont be executed)
2, $t_project= project_get_row( $t_project_id ); is wrong, it does not give back the project name

there could be other problems as well, these are the ones I could find quickly. Use the echo command frequently to understand where the your code goes wrong:)

If I were you I would insert my code temporarily to the main_page.php for quick checks until it works fine.

UPDATE:
1, if ( is_array($t_project_ids) )

2, $t_project= project_get_row( $t_project_id ); is wrong, it does not give back the project name
NO ITS NOT WRONG... my mistake
Last edited by istvanb on Feb 24, 2012 11:08 am, edited 1 time in total.

istvanb
Posts: 226
Joined: Aug 22, 2010 4:00 pm

Re: Creating a custom field with the Projects list

Post by istvanb » Feb 24, 2012 10:55 am

See the attached code. If I use echo ($t_possible_values); then it gives me back the subprojects separated by a '|'.
Note that I have no clue about the return function, so thats another part you should verify

Code: Select all

$t_enum = array();

$t_project_ids = current_user_get_accessible_subprojects( helper_get_current_project() );

if ( is_array($t_project_ids) ) 
{
foreach ( $t_project_ids as $t_project_id ) {
$t_project = project_get_row( $t_project_id );
$t_enum[] = $t_project['name'];   
}
   
$t_possible_values = implode( '|', $t_enum );
}

istvanb
Posts: 226
Joined: Aug 22, 2010 4:00 pm

Re: Creating a custom field with the Projects list

Post by istvanb » Feb 24, 2012 11:12 am

I have messed up this topic a bit with my back-and-forth comments, sorry about that. The bottomline is that I understand what you have tried to achieve and now I see why you have used the if structure as you did (copied from the project management php). I have no clue why it does not work, but it does not on my computer.

The code I have copied with the different evaluation work like a charm, but again I have no clue about php.

Next time you deals an issue like this just use the echo command frequently, that how I debug php stuff if I need to :)

let us know if you succeed!

fdot
Posts: 5
Joined: Feb 24, 2012 8:37 am

Re: Creating a custom field with the Projects list

Post by fdot » Feb 24, 2012 12:54 pm

Hey thanks a lot for all the tips i have made severals tests and now I know why it is not working :)

My projects are organized like this :

Project 1
-Sub-project 2
--Sub-Sub-project 3
Project 2

So my issues are always in some sub-sub-projects.

If I use

Code: Select all

current_user_get_accessible_subprojects( helper_get_current_project() )
It will try to display all the projects listed under my sub-sub-project. So I need to point to the parent project id and I didn't find how to do.

So i have made the following changes :

Code: Select all

function custom_function_override_enum_projects() {

    $t_enum = array();
	$t_project_ids = project_hierarchy_get_all_subprojects( 30 );
	if ( is_array($t_project_ids) )  {
		foreach ( $t_project_ids as $t_project_id ) {
				$t_project = project_get_row( $t_project_id );
				$t_enum[] = $t_project['name']; 
		}
	}
	$t_possible_values = implode( '|', $t_enum );
    return $t_possible_values;
}
30 is the of the parent project (in my example if I am in sub-sub-project3, 30 is the Id of Project 1)

So I am still looking for a solution for getting a parent project ID when you are into a sub-project or a sub-sub-project.

Have you got any idea ?

fdot
Posts: 5
Joined: Feb 24, 2012 8:37 am

Re: Creating a custom field with the Projects list

Post by fdot » Feb 27, 2012 5:29 am

Hi, I finally found a solution, for the moment it is a little bit quick and dirty but it is doing the job :

Code: Select all

function custom_function_override_enum_projects() {
	$p_project_id = helper_get_current_project();
	$Parentprojectid = project_hierarchy_get_parent($p_project_id);
	$TopParentprojectid = project_hierarchy_get_parent($Parentprojectid);
	if ($TopParentprojectid == '0') {
		$valprojectid = $Parentprojectid;	
	} else {
		$TopParentprojectidlvl2 = project_hierarchy_get_parent($TopParentprojectid);
		if ($TopParentprojectidlvl2 == '0') {
			$valprojectid = $TopParentprojectid;
		} else {
			$valprojectid = $TopParentprojectidlvl2;
		}
	}
    $t_enum = array();
	$t_project_ids = project_hierarchy_get_all_subprojects( $valprojectid );
	if ( is_array($t_project_ids) )  {
		foreach ( $t_project_ids as $t_project_id ) {
				$t_project = project_get_row( $t_project_id );
				$t_enum[] = $t_project['name']; 
		}
	}
	$t_possible_values = implode( '|', $t_enum );
    return $t_possible_values;
}
Thanks again for your help :)

All the best !

istvanb
Posts: 226
Joined: Aug 22, 2010 4:00 pm

Re: Creating a custom field with the Projects list

Post by istvanb » Feb 27, 2012 5:37 am

Pretty cool! Can you post a screenshot about how it works in your computer? It looks like a cool mod, so maybe other users will find it useful too.

fdot
Posts: 5
Joined: Feb 24, 2012 8:37 am

Re: Creating a custom field with the Projects list

Post by fdot » Feb 27, 2012 8:49 am

It would be a pleasure but unfortunatly currently I can't make any screenshots :/

But to all the others users who are looking for this kind of solution feel free to use the code and to improve it !

I have plan to make 2-3 changes on it, I will post them here ;)

Post Reply