|--oohello |--inc | |--class.hello.inc.php |--setup | |--setup.inc.php | |--phpgw_??.lang |--templates | |--default | | |--hello_form.tpl | | |--hello_result.tpl |--index.phpOne of the main ideas in phpgroupware OO applications, is that the application may be called by phpgroupware index.php. When index.php is invoked with an argument named menuaction that has a value structured like: applicationName.className.methodName.
<?php
$GLOBALS['phpgw_info']['flags'] = array(
'currentapp' => 'oohello',
'noheader' => True,
'nonavbar' => True
);
include('../header.inc.php');
$obj = CreateObject('oohello.hello');
$obj->say_hello();
$GLOBALS['phpgw']->common->phpgw_footer();
?>
Note that index.php creates the object and calls the say-hello() method.
<?php
class hello
{
var $t; //template
var $input='World';
var $public_functions = array('say_hello' => True); //callable functions
// constructor
function hello()
{
$this->t =& $GLOBALS['phpgw']->template;
return True;
}
function say_hello()
{
// phpgroupware header and navigation bar
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
if(count($_POST)){ // if called by POST
$this->input=get_var('input',array('POST'));
}
//Compute hello()
$result = lang('hello').' '.$this->input.' !';
//we will use two templates
$this->t->set_file(array(
'result' => 'hello_result.tpl',
'form' => 'hello_form.tpl'
)
);
//filling the first template
$this->t->set_var('hello_input',htmlspecialchars($this->input));
$this->t->set_var('hello_result',htmlspecialchars($result));
//filling the second template
$this->t->set_var('hello_action',$GLOBALS['phpgw']->link('/index.php','menuaction=oohello.hello.say_hello'));
$this->t->set_var('hello_value',htmlspecialchars($this->input));
$this->t->pparse('out','result');
$this->t->pparse('out','form');
return True;
}
}
?>
<?php
$setup_info['oohello']['name'] = 'oohello';
$setup_info['oohello']['title'] = 'Oohello';
$setup_info['oohello']['version'] = '0.9.16';
$setup_info['oohello']['app_order'] = 7;
$setup_info['oohello']['tables'] = array();
$setup_info['oohello']['enable'] = 1;
$setup_info['oohello']['globals_checked'] = True;
/* Dependancies for this app to work */
$setup_info['oohello']['depends']= array(
array('appname' => 'phpgwapi', 'versions' => Array('0.9.16')),
array('appname' => 'admin', 'versions' => Array('0.9.16')),
array('appname' => 'preferences','versions' => Array(0.9.16')
);
?>
Hook files are located in inc directory and they must be
referenced in setup.inc.php.
|-- oohello
|--inc
| |--hook_about.inc.php
The hook_about.inc.php should contain something like this:
<?php
function about_app($tpl,$handle)
{
$s = '<b>' . lang('hello') . '</b><p>'
.lang('written by:') . ' Christian Bac<br>';
return $s;
}
?>
You also have to add the information into setup.inc.php saying that
there is a hook for that application.$setup_info['oohello']['hooks'][] = 'about';
|--oostep4 | |--inc | | |--class.hello.inc.php | | |--hook_about.inc.php | | |--hook_preferences.inc.php | |--setup | | |--setup.inc.php | | |--phpgw_??.lang | |--templates | | |--default | | | |--hello_form.tpl | | | |--hello_result.tpl | | | |--preferences.tpl | |--index.phpYou have to initialize the content of oostep3 hierarchy with files from oohello.
You also have to modify te content of setup.inc.php to modify the name of the module (replace oohello by oostep3).
<font face="{font_face}" size="{font_size}">
<p>Type Here your text to hello()</p>
<form method="POST" action="{hello_action}">
<input type="text" name="input" value= "{hello_value}" >
<input type="submit" value="OK">
</form>
</font>
Hello_result.tpl must be modified accordingly.
class hello
{
var $t; //template
var $prefs; //programm preferences
...
function setdef()
{
$this->t->set_var('font_face',$this->prefs['font_type']);
$this->t->set_var('font_size',$this->prefs['font_size']);
$this->t->set_var('lang_submit',lang('submit'));
}
...
class hello
{
...
function read_preferences()
{
$GLOBALS['phpgw']->preferences->read_repository();
if($GLOBALS['phpgw_info']['user']['preferences']['oohello'])
{
$this->prefs['font_type'] =$GLOBALS['phpgw_info']['user']['preferences']['oohello']['font_type'];
$this->prefs['font_size'] =$GLOBALS['phpgw_info']['user']['preferences']['oohello']['font_size'];
}
else
{
prefs=NULL;
}
}
function save_preferences()
{
$GLOBALS['phpgw']->preferences->read_repository();
if ($this->prefs)
{
$GLOBALS['phpgw']->preferences->change('oohello','font_type',$this->prefs['font_type']);
$GLOBALS['phpgw']->preferences->change('oohello','font_size',$this->prefs['font_size']);
$GLOBALS['phpgw']->preferences->save_repository(True);
}
}
...
...
function preferences()
{
$submit = get_var('submit',array('POST'));
$prefs = get_var('prefs',array('POST'));
if($submit)
{
$this->prefs=$prefs;
$this->save_preferences();
Header('Location: ' . $GLOBALS['phpgw']->link('/preferences/index.php'));
$GLOBALS['phpgw']->common->phpgw_exit();
}
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
$this->t->set_file(array('preferences' =>'preferences.tpl'));
$this->setdef();
$this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/index.php','menuaction=oohello.hello.preferences'));
$this->t->set_var('lang_action',lang('preferences'));
$this->t->set_var('lang_select_font',lang('choose a font'));
$this->t->set_var('lang_select_size',lang('choose the font size'));
$font_type_array=array(
'arial'=>'Arial,Helvetica,sans-serif',
'times' =>'Times New Roman,Times,serif',
'verdana' => 'Verdana,Arial,Helvetica,sans-serif',
'georgia' => 'Georgia,Times New Roman,Times,serif',
'courier' => 'Courier New,Courier,mono',
'tahoma' => 'Tahoma,Verdana,Arial,Helvetica,sans-serif'
);
$type_options ='';
foreach($font_type_array as $alias => $font_name){
$type_options .= '<option value="'.$font_name.'"';
if($this->prefs['font_type']==$font_name){
$type_options .= 'selected';
}
$type_options .= '>' . lang($alias) .'</option>' . "\n";
}
$this->t->set_var('type_options',$type_options);
$font_size_array=array(
1 => 'very small', 2=> 'small', 3 => 'medium',
4 => 'large', 5 => 'very large');
$size_options ='';
foreach($font_size_array as $key => $size){
$size_options .= '<option value="'.$key.'"';
if($this->prefs['font_size']==$key){
$size_options .= ' selected';
}
$size_options .= '>'.lang($size).'</option>\n';
}
$this->t->set_var('size_options',$size_options);
$this->t->pfp('out','preferences');
}
}
?>
<font face="{font_face}" size="{font_size}">
<center>
<form method="POST" name="prefs_form" action="{actionurl}">
<table width="70%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td colspan="2" bgcolor="#c0c0c0" align="center"><b>{lang_action}</b></td>
</tr>
<tr>
<td align="right">{lang_select_font}:</td>
<td><select name="prefs[font_type]">{type_options}</select></font></td>
</tr>
<tr>
<td align="right">{lang_select_size}:</td>
<td><select name="prefs[font_size]">{size_options}</select></td>
</tr>
<tr valign="bottom">
<td height="50"><input type="submit" name="submit" value="{lang_submit}">
<td> </td>
</tr>
</table>
</form></td>
</center>
</font>
var $public_functions = array(
'say_hello' => True,
'preferences' => True
);
<?php
// Only Modify the $file and $title variables.....
$title = $appname;
$file = Array('Preferences' => $GLOBALS['phpgw']->link('/index.php','menuaction=oohello.hello.preferences'));
//Do not modify below this line
display_section($appname,$title,$file);
?>
|--oostep5 | |--inc | | |--class.hello.inc.php | | |--hook_about.inc.php | | |--hook_preferences.inc.php | | |--hook_admin.inc.php | |--setup | | |--setup.inc.php | | |--tables_current.inc.php | | |--phpgw_??.lang | |--templates | | |--default | | | |--hello_form.tpl | | | |--hello_result.tpl | | | |--preferences.tpl | |--index.php
<?php
$phpgw_baseline = array(
'phpgw_hellooptions' => array(
'fd' => array(
'hello_id' => array('type' => 'auto', 'nullable' => False),
'hello_type' => array('type' => 'varchar', 'precision'=>10),
'hello_key' => array('type' => 'varchar', 'precision' =>20),
'hello_value' => array('type' => 'varchar','precision' =>100)
),
'pk' => array('hello_id'),
'fk' => array(),
'ix' => array(),
'uc' => array('hello_type','hello_key')
)
);
?>
This table is read when you install the application.$GLOBALS['phpgw']->db in the class constructor.
$face_query='select * from phpgw_hellooptions where hello_type ='."'".'face'."';";
$type_options ='';
$this->db->query($face_query);
while($this->db->next_record()){
$type_options .= '<option value="'.$this->db->f('hello_value').'"';
if($this->prefs['font_type']==$this->db->f('hello_value')){
$type_options .= 'selected';
}
$type_options .= '>' . lang($this->db->f('hello_key')). '</option>' . "\n";
}
$this->t->set_var('type_options',$type_options);
$size_query='select * from phpgw_hellooptions where hello_type ='."'".'size'."';";
$size_options ='';
$this->db->query($size_query);
while($this->db->next_record()){
$size_options .= '<option value="'.$this->db->f('hello_value').'"';
if($this->prefs['font_size']==$this->db->f('hello_value')){
$size_options .= 'selected';
}
$size_options .= '>' . lang($this->db->f('hello_key')). '</option>' . "\n";
}
function add_choice($type,$key,$value)
{
if($type && $key && $value)
{
$face_query='select * from phpgw_hellooptions where hello_type ='."'".$type
."' And hello_key ='".$key. "';";
$this->db->query($face_query);
if($this->db->next_record())
{ //looks like the record exist replace values
}
else
{//insert new record
$insert_query='insert into phpgw_hellooptions (hello_type, hello_key, hello_value) values ('
."'".$type."','".$key. "','".$value."');";
$this->db->query($insert_query);
}
}
}
function admin(){
$submit = get_var('submit',array('POST'));
if ($submit)
{
$this->add_choice(get_var('type', array('POST')),
get_var('key',array('POST')),
get_var('value',array('POST')));
Header('Location: ' . $GLOBALS['phpgw']->link('/admin/index.php'));
$GLOBALS['phpgw']->common->phpgw_exit();
}
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
$this->t->set_file(array('hello_add' => 'add.tpl',
// 'hello_del' => 'dell.tpl'
)
);
$this->setdef();
$type_choice='<option value="face" selected>face</option>'
.'<option value="size">size</option>';
$this->t->set_var('type_options',$type_choice);
$this->t->set_var('lang_add_font_choice',lang('add a font choice'));
$this->t->set_var('lang_key',lang('key'));
$this->t->set_var('lang_value',lang('value'));
$this->t->set_var('action_url',$GLOBALS['phpgw']->link('/index.php','menuaction=oostep5.hello.admin'));
$this->t->pparse('out','hello_add');
}
<?php
$file['Site Configuration'] =
$GLOBALS['phpgw']->link('/index.php',
array('menuaction' => 'oostep5.hello.admin'));
display_section('oostep5',$file);
?>
|--oostep6 | |--inc | | |--class.hello_ui.inc.php | | |--class.hello_bo.inc.php | | |--class.hello_so.inc.php | | |--hook_about.inc.php | | |--hook_preferences.inc.php | | |--hook_admin.inc.php | |--setup | | |--setup.inc.php | | |--tables_current.inc.php | | |--phpgw_??.lang | |--templates | | |--default | | | |--hello_form.tpl | | | |--hello_result.tpl | | | |--preferences.tpl | |--index.phpTo do this we select the functions from hello class and decide if they can be put directly in one of the three new objects.
<?php
class hello_so
{
var $db; //phpgw db
var $option_table;
function hello_so()
{
$this->db=$GLOBALS['phpgw']->db;
$this->option_table='phpgw_helloopts5';
return True;
}
function get_faces_list(){
$face_list=array();
$face_query='select * from'.$this->option_table.' where hello_type =\'face\';';
$this->db->query($face_query);
while($this->db->next_record()){
$value=$this->db->f('hello_value');
$key=$this->db->f('hello_key');
$face_list[$key]=$value;
}
return $face_list;
}
function get_sizes_list(){
$size_list=array();
$size_query='select * from
'.$this->option_table.' where hello_type =\'size\';';
$this->db->query($size_query);
while($this->db->next_record()){
$value=$this->db->f('hello_value');
$key=$this->db->f('hello_key');
$size_list[$key]=$value;
}
return $size_list;
}
function add_choice($type,$key,$value){
if($type && $key && $value){
$where=' where hello_type =\''.$type.'\' And hello_key =\''.$key. '\';';
$select_query='select * from '.$this->option_table.$where;
$this->db->query($select_query);
if($this->db->next_record()){
//looks like the record exist replace values
$mod_query='update '. $this->option_table.' set hello_value=\''
.$value.'\''. $where;
}else{//insert new record
$mod_query='insert into '. $this->option_table
.' (hello_type, hello_key, hello_value)
values (\''.$type.'\',\''.$key. '\',\''.$value.'\');';
}
$this->db->query($mod_query);
}
}
}
?>
<?php
class hello_bo
{
var $so; //link to so object
var $prefs; //programm preferences
function hello_bo()
{
$this->so=CreateObject('oostep6.hello_so');
$this->read_preferences();
return True;
}
function get_prefs()
{
return $this->prefs;
}
function set_prefs($prefs)
{
$this->prefs=$prefs;
$this->save_preferences();
}
function read_preferences()
{
$GLOBALS['phpgw']->preferences->read_repository();
if($GLOBALS['phpgw_info']['user']['preferences']['oostep6'])
{
$this->prefs['font_type'] = $GLOBALS['phpgw_info']['user']['preferences']['oostep6']['font_type'];
$this->prefs['font_size'] = $GLOBALS['phpgw_info']['user']['preferences']['oostep6']['font_size'];
}
else
{
$this->prefs=NULL;
}
}
function save_preferences()
{
$GLOBALS['phpgw']->preferences->read_repository();
if ($this->prefs)
{
$GLOBALS['phpgw']->preferences->change('oostep6','font_type',$this->prefs['font_type']);
$GLOBALS['phpgw']->preferences->change('oostep6','font_size',$this->prefs['font_size']);
$GLOBALS['phpgw']->preferences->save_repository(True);
}
}
function add_choice($type,$key,$value){
$this->so->add_choice($type,$key,$value);
}
function get_faces_list(){
return $this->so->get_faces_list();
}
function get_sizes_list(){
return $this->so->get_sizes_list();
}
}
?>
<?php
class hello_ui
{
var $bo; //back office object
var $t; //template
var $input='World';
var $public_functions = array
(
'say_hello' => True,
'preferences' => True,
'admin' => True
);
function hello_ui()
{
$this->bo= CreateObject('oostep6.hello_bo');
$this->t = $GLOBALS['phpgw']->tempate;
return True;
}
function say_hello()
{
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
if(count($_POST)){
$this->input=get_var('input',array('POST'));
}
//Compute hello()
$result = lang('hello').' '.$this->input.' !';
//we will use two templates
$this->t->set_file(array(
'result' => 'hello_result.tpl',
'form' => 'hello_form.tpl'
)
);
$this->setdef();
//filling the first template
$this->t->set_var('hello_input',htmlspecialchars($this->input));
$this->t->set_var('hello_result',htmlspecialchars($result));
//filling the second template
$this->t->set_var('hello_action',$GLOBALS['phpgw']->link('/index.php','menuaction=oostep6.hello_ui.say_hello'));
$this->t->set_var('hello_value',htmlspecialchars($this->input));
//parse and write out the templates
$this->t->pparse('out','result');
$this->t->pparse('out','form');
return True;
}
function setdef(){
$prefs=$this->bo->get_prefs();
$this->t->set_var('font_face',$prefs['font_type']);
$this->t->set_var('font_size',$prefs['font_size']);
$this->t->set_var('lang_submit',lang('submit'));
}
function preferences()
{
if (count($_POST))
{
$prefs = get_var('prefs',array('POST'));
$this->bo->set_prefs($prefs);
Header('Location: ' .$GLOBALS['phpgw']->link('/preferences/index.php'));
$GLOBALS['phpgw']->common->phpgw_exit();
}
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
$this->t->set_file(array('preferences' =>'preferences.tpl'));
$this->setdef();
$this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/index.php','menuaction=oostep6.hello_ui.preferences'));
$this->t->set_var('lang_action',lang('preferences'));
$this->t->set_var('lang_select_font',lang('choose a font'));
$this->t->set_var('lang_select_size',lang('choose the font size'));
$prefs=$this->bo->get_prefs();
$face_list=$this->bo->get_faces_list();
$type_options ='';
foreach($face_list as $key => $value){
$type_options .= '<option value="'.$value.'"';
if($prefs['font_type']==$value){
$type_options .= 'selected';
}
$type_options .= '>' . lang($key).'</option>' . "\n";
}
$this->t->set_var('type_options',$type_options);
$size_list=$this->bo->get_sizes_list();
$size_options ='';
foreach($size_list as $key => $value){
$size_options .= '<option value="'.$value.'"';
if($prefs['font_size']==$value){
$size_options .= 'selected';
}
$size_options .= '>' . lang($key).'</option>' . "\n";
}
$this->t->set_var('size_options',$size_options);
$this->t->pfp('out','preferences');
}
function admin(){
$submit = get_var('submit',array('POST'));
if ($submit)
{
$this->bo->add_choice(get_var('type',array('POST')),
get_var('key',array('POST')),
get_var('value',array('POST')));
Header('Location: ' .
$GLOBALS['phpgw']->link('/admin/index.php'));
$GLOBALS['phpgw']->common->phpgw_exit();
}
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
$this->t->set_file(array('hello_add' =>'add.tpl',
// 'hello_del' => 'dell.tpl'
));
$this->setdef();
$type_choice='<option value="face" selected>face</option>'
.'<option value="size">size</option>';
$this->t->set_var('type_options',$type_choice);
$this->t->set_var('lang_add_font_choice',
lang('add a font choice'));
$this->t->set_var('lang_key',lang('key'));
$this->t->set_var('lang_value',lang('value'));
$this->t->set_var('action_url',$GLOBALS['phpgw']->link('/index.php','menuaction=oostep6.hello_ui.admin'));
$this->t->pparse('out','hello_add');
}
}
?>