[ghelda-devel] [28] Add the current "being worked" Groups plugin.

[ Thread Index | Date Index | More lists.tuxfamily.org/ghelda-devel Archives ]


Revision: 28
Author:   odyx
Date:     2009-04-29 14:27:35 +0200 (Wed, 29 Apr 2009)

Log Message:
-----------
Add the current "being worked" Groups plugin.

Aka BIG commit !

Added Paths:
-----------
    trunk/plugins/Groups/
    trunk/plugins/Groups/config.php
    trunk/plugins/Groups/config_core.php
    trunk/plugins/Groups.php


Added: trunk/plugins/Groups/config.php
===================================================================
--- trunk/plugins/Groups/config.php	                        (rev 0)
+++ trunk/plugins/Groups/config.php	2009-04-29 12:27:35 UTC (rev 28)
@@ -0,0 +1,35 @@
+<?php
+/*
+ *  Ghelda - Gestion Hiérarchisée En Ligne D'Adresses
+ *         - Online Hierarchical Handling Of Adresses
+ *
+ *  Copyright (C) 2008 Didier Raboud
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Values must be lowercase (see config_core.php)
+$conf['fields']['type']['values'][1]['name'] = T_('Patrol');
+$conf['fields']['type']['values'][1]['roles'][1] = T_('Patrol Chief');
+$conf['fields']['type']['values'][1]['roles'][2] = T_('Patrol Sub-Chief');
+
+$conf['fields']['type']['values'][2]['name'] = T_('Troup');
+$conf['fields']['type']['values'][2]['roles'][1] = T_('Troup Chief');
+$conf['fields']['type']['values'][2]['roles'][2] = T_('Troup Chief Adjunct');
+
+$conf['orderby'][] = 'type';
+$conf['orderby'][] = 'name';
+
+
+?>

Added: trunk/plugins/Groups/config_core.php
===================================================================
--- trunk/plugins/Groups/config_core.php	                        (rev 0)
+++ trunk/plugins/Groups/config_core.php	2009-04-29 12:27:35 UTC (rev 28)
@@ -0,0 +1,56 @@
+<?php
+/*
+ *  Ghelda - Gestion Hiérarchisée En Ligne D'Adresses
+ *         - Online Hierarchical Handling Of Adresses
+ *
+ *  Copyright (C) 2008 Didier Raboud
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// All fields values _must_ be lowercase due to MSDB2 weird handling of field names.
+
+// Required
+$conf['fields']['group_id']['type'] = 'INT( 10 )';
+$conf['fields']['group_id']['name'] = T_('Identifier');
+$conf['fields']['group_id']['restricted'] = 'key';
+$conf['fields']['group_id']['order'] = 1000;	// Has to be the highest
+
+$conf['fields']['type']['type'] = 'SELECT';
+$conf['fields']['type']['name'] = T_('Type');
+$conf['fields']['type']['order'] = 900;
+
+$conf['fields']['name']['type'] = 'INT( 10 )';
+$conf['fields']['name']['name'] = T_('Name');
+$conf['fields']['name']['order'] = 800;
+
+// Required
+$conf['fields_link']['people_group_id']['type'] = 'INT( 10 )';
+$conf['fields_link']['people_group_id']['name'] = T_('Identifier');
+$conf['fields_link']['people_group_id']['restricted'] = 'key';
+
+$conf['fields_link']['start_date']['type'] = 'DATE';
+$conf['fields_link']['start_date']['name'] = T_('Start date');
+
+$conf['fields_link']['end_date']['type'] = 'DATE';
+$conf['fields_link']['end_date']['name'] = T_('End date');
+
+$conf['fields_link']['role']['type'] = 'SELECT';
+$conf['fields_link']['role']['name'] = T_('Role');
+
+// Optional
+
+// Needed.
+
+?>

Added: trunk/plugins/Groups.php
===================================================================
--- trunk/plugins/Groups.php	                        (rev 0)
+++ trunk/plugins/Groups.php	2009-04-29 12:27:35 UTC (rev 28)
@@ -0,0 +1,370 @@
+<?php
+/*
+ *  Ghelda - Gestion Hiérarchisée En Ligne D'Adresses
+ *         - Online Hierarchical Handling Of Adresses
+ *
+ *  Copyright (C) 2008 Didier Raboud
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+define('G_GROUPS_URL_MODE','mode');
+define('G_GROUPS_DEFAULT_MODE','list');
+define('G_GROUPS_ID_NEW','-1');
+define('G_GROUPS_ID','group_id');
+define('G_PEOPLE_GROUPS_ID','people_group_id');
+
+class Groups extends Plugin {
+	// List of the modes
+	private $modes = array('edit','list','new','del');
+	// Actual mode
+	private $mode;
+	// List of the fields
+	private $fields = array();
+	// Formatted list of fields
+	private $formattedFields;
+	// Name of the table
+	private $tableName;
+	// Name of the link table
+	private $linkPeopleTableName;
+	
+	//! Constructor
+	function __construct(&$parentRef,$name)
+	{
+		$to_return = parent::__construct(&$parentRef,$name);
+
+		$this->fields = $this->configH[$this->name]['fields'];
+		// Seek the data
+		$this->formattedFields = array_keys($this->fields);
+		$this->formattedFields = implode($this->formattedFields,'`,`');
+
+		$this->tableName = $this->configH['DB']['prefix'].'groups';
+		$this->linkPeopleTableName = $this->configH['DB']['prefix'].'people_groups';
+
+		return $to_return;
+	}
+
+
+	function modesMenu()
+	{
+		$this->startTrad();
+		$targets['Tname'] = T_('Groups');
+		$targets['new']['name']  = T_('New');
+		$targets['list']['name'] = T_('List');
+		$this->stopTrad();
+		return $targets;
+	}
+
+	function main()
+	{	$this->startTrad();
+
+		// Take the mode from the URL or take the default mode
+		$this->mode = array_key_exists(G_GROUPS_URL_MODE,$this->url->querystring) ? $this->url->querystring[G_GROUPS_URL_MODE] : G_GROUPS_DEFAULT_MODE;
+		
+		// If the mode is not know, trigger an error
+		if(array_search($this->mode,$this->modes) === false) {
+			trigger_error(T_('Intrusion tentative detected: ').T_("Don't try to use undefined mode names!"), G_E_LOG);
+			return false;
+		}
+
+		$i = 0;
+		// Sort the fields in descending Order 
+		foreach( $this->fields as $fieldName => $field ) {
+			$orderArray[$fieldName] = !empty($field['order']) ? $field['order'] : $i--;
+		}
+		array_multisort($orderArray,SORT_DESC,$this->fields);
+
+		// Only execute if the user is authentified
+		if($this->authH->getAuth()) {
+			// From here, we are in know mode
+			// Get the Id from a query or edit myself
+			$groups_id = array_key_exists(G_GROUPS_ID,$_GET) ? $_GET[G_GROUPS_ID] : G_GROUPS_ID_NEW;
+
+			switch($this->mode) {
+				case 'edit':	$this->m_edit($groups_id,$this->fields);	break;
+				case 'list':	$this->m_list();	break;
+				case 'new':	$this->m_edit(G_GROUPS_ID_NEW,$this->fields);	break;
+				case 'del':	$this->m_del($groups_id);	break;
+			}
+		}
+		
+		$this->stopTrad();
+	}
+
+	function functionality($funcStr,$funcArgs = '')
+	{
+		switch($funcStr) {
+			case "main":
+				$this->main();
+				break;
+			case "People.edit.bottom":
+				return $this->People_edit_bottom($funcArgs);
+				break;
+		}
+	}
+
+	private function m_list()
+	{
+		$query = sprintf("SELECT `%s` FROM `%s` ORDER BY `%s` ASC LIMIT 0 , 30",	// FIXME: No limit and no Selection of Columns
+			$this->formattedFields,
+			$this->tableName,
+			G_GROUPS_ID);
+
+		$fieldsContents = $this->db->getAssoc($query);
+		if(PEAR::isError($fieldsContents))
+			return false;
+
+		$this->url->addQueryString(G_GROUPS_URL_MODE,'edit');
+		// Select only the useful fields
+		$fieldsSpec = $this->fields;
+		
+		$modes['edit']	= array(
+				'name' => T_('Edit'),
+				'image' => 'pen');
+		$modes['del'] =array('name' =>T_('Delete'),'image' => 'cross');
+		$this->output->displayList($fieldsSpec,$fieldsContents,array('id'=>'People_list'),$modes);
+	}	
+
+	private function m_edit($id,&$fieldsSpec)
+	{
+		// Modification asked
+		if($_POST[$this->configH['URL']['action']] == 'change') {
+
+			// Get the fields from the keyed array
+			$fields = array_keys($fieldsSpec);
+
+			// Go along the fields
+			foreach($fields as $fieldName_) {
+				$fieldName = $this->name.'_'.$fieldName_;
+				// Jump the non-modified ones (and if we are creating a new user)
+				if(	$id != G_GROUPS_ID_NEW &&
+					isset($_POST[$fieldName]) &&
+					$_POST[$fieldName] == $_POST[$fieldName.'_old']) {
+					continue;
+				}
+				// Get the type of the field and store its value
+				$type = $fieldsSpec[$fieldName_]['type'];
+				$fieldValue = $_POST[$fieldName];
+
+				// Jump id (No insert nor check)
+				if($fieldName_ == G_GROUPS_ID ) {
+					continue;
+				}
+
+				$fieldContent = g_getFormattedField($_POST,$type,$fieldName,$fieldName_,$fieldsSpec);
+				if( $fieldContent === false ) { // The given date is false
+					$fieldsSpec[$fieldName_]['error'] = true;
+					$newError = true;
+					continue;
+				}
+				$fieldsContents[$fieldName_] = $fieldContent;
+				$formattedFields .= "`$fieldName_` = '".$fieldContent."', ";
+			}
+			// Strip the two last characters (border issue)
+			$formattedFields = substr($formattedFields,0,-2);
+			// If we are updating (thus not making a new entry)
+			if($id <> G_GROUPS_ID_NEW) {
+				$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = '%s' LIMIT 1",
+					$this->tableName,
+					$formattedFields,
+					G_GROUPS_ID,
+					$id);
+				$queryR =& $this->db->query($query);
+				if(PEAR::isError($queryR)) return false;
+
+			} elseif ($newError !== true) { // Creating new entry iff we have not had any error before.
+				$query = sprintf("INSERT INTO `%s` SET %s",
+					$this->tableName,
+					$formattedFields,
+					G_GROUPS_ID,
+					$id);
+				$queryR =& $this->db->query($query);
+				if(PEAR::isError($queryR))
+					return false;
+				// Get the just produced ID (per client => thread-safe)
+				$query = "SELECT LAST_INSERT_ID()";
+				$queryR =& $this->db->query($query);
+				$id =& current($queryR->fetchRow());
+				// Success => mode = 'edit'
+				$this->mode = 'edit';
+			}
+			$this->url->removeQueryString($this->configH['URL']['action']);
+		}
+
+		// Display the edited user
+
+		// If we are not creating a new groups
+		if( $id <> G_GROUPS_ID_NEW ) {
+			// Get the formatted fields list
+			$formattedFields = array_keys($fieldsSpec);
+			$formattedFields = implode($formattedFields,'`,`');
+			$query = sprintf("SELECT `%s` FROM `%s` WHERE `%s` = '%s' LIMIT 1",
+				$formattedFields,
+				$this->tableName,
+				G_GROUPS_ID,
+				$id);
+			$queryR =& $this->db->query($query);
+			if(PEAR::isError($queryR))
+				return false;
+			$fieldsContents =& $queryR->fetchRow();
+		} else { // We are creating a new user
+			// Set the ID to the special one
+			$fieldsContents[G_GROUPS_ID] = $id;
+			$displayOptions['title'] = T_('New groups');
+		}
+		// Make the URL OK
+		$this->url->addQueryString(G_GROUPS_URL_MODE,$this->mode);
+		$this->url->addQueryString(G_GROUPS_ID,$id);
+
+		$fieldsContents[G_GROUPS_ID] = $id;
+
+		$displayOptions['id']		= 'Groups_view';
+		$displayOptions['command']	= T_('Execute');
+		$displayOptions['target']	= $this->url->getURL();
+		$displayOptions['action']	= 'change';
+
+		$fullFieldsSpec[$this->name] = $fieldsSpec;
+
+		$fullFieldsContents[$this->name] = $fieldsContents;
+
+		$this->output->displayFields($fullFieldsSpec,$fullFieldsContents,$displayOptions);
+	}
+
+	private function m_del($id)
+	{
+		// Delete the given id (seek for confirmation)
+		
+		// In case of confirmation
+		if($_POST[$this->configH['URL']['action']] == 'confirm') {
+			$query = sprintf("DELETE FROM `%s` WHERE `%s` = %d LIMIT 1",
+				$this->tableName,
+				G_GROUPS_ID,
+				$id);
+			$queryR =& $this->db->query($query);
+			if(PEAR::isError($queryR))
+				return false;
+			$this->m_list();
+		} else {
+			$query = sprintf("SELECT `%s` FROM `%s` WHERE `%s` = '%s' LIMIT 1",
+				$this->formattedFields,
+				$this->tableName,
+				G_GROUPS_ID,
+				$id);
+			$queryR =& $this->db->query($query);
+			if(PEAR::isError($queryR))
+				return false;
+			$fieldsContents =& $queryR->fetchRow();
+
+			// Display the fields and launch the command to confirm
+			$this->output->displayFields($this->fields,$fieldsContents,array('id'=>'Groups_view','command'=>T_('Confirm deletion'),'target'=>$this->url->getURL(),'action'=>'confirm','title'=>T_('Do you really want to delete the following entry?')),$this->name);
+		}
+	}
+
+	private function People_edit_bottom($funcArgs)
+	{	$this->startTrad();
+
+		$id_people = $funcArgs[G_DB_PEOPLE_ID];
+
+		// Modification asked
+		if($_POST[$this->configH['URL']['action']] == 'change') {
+			gol('POST =',$_POST);
+		}
+
+		$tableFields = $this->configH[$this->name]['fields'];
+		// Groups table
+		$tableFieldsNames = array_keys($tableFields);
+		array_walk($tableFieldsNames,'concat_to_each',array('`'.$this->tableName.'`.`','`'));
+
+		// Get the Ordering
+		$tableOrderBy = $this->configH[$this->name]['orderby'];
+		array_walk($tableOrderBy,'concat_to_each',array('`'.$this->tableName.'`.`','`'));
+
+		// Get listof groups
+		$query = sprintf("SELECT %s FROM `%s` WHERE 1 ORDER BY %s ASC; ",
+				implode(',',$tableFieldsNames),
+				$this->tableName,
+				implode(',',$tableOrderBy));
+		$queryR =& $this->db->query($query);
+		if(PEAR::isError($queryR))
+			return false;
+
+		// Create empty fake one
+		$groups[G_GROUPS_ID_NEW]['name'] = '';
+		$groups[G_GROUPS_ID_NEW]['values'] = array(G_GROUPS_ID_NEW => '');
+
+		while (($groups_row = $queryR->fetchRow())) {
+			$type = $groups_row['type'];
+			$id = $groups_row[G_GROUPS_ID];
+			$groups[$type]['name'] = $this->configH[$this->name]['fields']['type']['values'][$type]['name'];
+			$groups[$type]['values'][$id] = $groups_row['name'];
+		}
+
+		$linkTableFields = $this->configH[$this->name]['fields_link'];
+		// Groups-to-people table
+		$linkTableFieldsNames = array_keys($linkTableFields);
+		array_walk($linkTableFieldsNames,'concat_to_each',array('`'.$this->linkPeopleTableName.'`.`','`'));
+		$variablesChain = array_merge($tableFieldsNames,$linkTableFieldsNames);
+		$variablesChain = implode(',',$variablesChain);
+
+		// Display list of connections + one empty
+		// TODO: No LIMIT !
+		$query = sprintf("SELECT %s FROM `%s` WHERE `%s` = `%s` AND `%s` = '%s'",
+				$variablesChain,
+				implode('`,`',array($this->linkPeopleTableName,$this->tableName)),
+				implode('`.`',array($this->linkPeopleTableName,G_GROUPS_ID)),
+				implode('`.`',array($this->tableName,G_GROUPS_ID)),
+				G_DB_PEOPLE_ID,
+				$id_people);
+		$queryR =& $this->db->query($query);
+		if(PEAR::isError($queryR))
+			return false;
+
+		while (($pg_row = $queryR->fetchRow())) {
+			$key = $pg_row[G_PEOPLE_GROUPS_ID];
+			$to_return['content'][G_GROUPS_ID.'['.$key.']'] = $pg_row[G_GROUPS_ID];
+			$to_return['spec'][G_GROUPS_ID.'['.$key.']'] = array(
+				'type' => 'SELECT',
+				'name' => T_('Group'),
+				'optgroups' => true,
+				'values' => $groups,
+				'multiple' => false
+			);
+			foreach($linkTableFields as $fieldName => $fieldSpecs) {
+				if( array_key_exists('restricted',$fieldSpecs) ) continue;
+
+				$to_return['spec'][$fieldName.'['.$key.']'] = $fieldSpecs;
+				$to_return['content'][$fieldName.'['.$key.']'] = $pg_row[$fieldName];
+			}
+		}
+		// Add a fake entry
+		$key = G_GROUPS_ID_NEW;
+		$to_return['spec'][G_GROUPS_ID.'['.$key.']'] = array(
+				'type' => 'SELECT',
+				'name' => T_('Group'),
+				'optgroups' => true,
+				'values' => $groups,
+				'multiple' => false
+		);
+		// Inputs display possible with name=id='field_name[identifiant]'
+		foreach($linkTableFields as $fieldName => $fieldSpecs) {
+			if( array_key_exists('restricted',$fieldSpecs) ) continue;
+			$to_return['spec'][$fieldName.'['.$key.']'] = $fieldSpecs;
+			$to_return['content'][$fieldName.'['.$key.']'] = $pg_row[$fieldName];
+		}
+
+		$this->stopTrad();
+		return $to_return;
+	}
+}
+?>


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/