~intrahealth+informatics/ihris-common/4.0.23-release

« back to all changes in this revision

Viewing changes to iHRIS_PageFormLists.php

  • Committer: Luke Duncan
  • Date: 2007-09-23 00:56:50 UTC
  • Revision ID: lduncan@intrahealth.org-20070923005650-0ka6pvrl3gfxq9yl
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * © Copyright 2007 IntraHealth International, Inc.
 
4
 * 
 
5
 * This File is part of iHRIS
 
6
 * 
 
7
 * iHRIS is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 * 
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
/**
 
21
 * Manage editing of all the list databases used for drop down menus.
 
22
 * 
 
23
 * This page has the code for the {@link ListsPage} object and then it
 
24
 * creates an instance of this page object and calls the {@link ListsPage::display() display}
 
25
 * method.
 
26
 * @package iHRIS
 
27
 * @subpackage Common
 
28
 * @access public
 
29
 * @author Luke Duncan <lduncan@intrahealth.org>
 
30
 * @copyright Copyright &copy; 2007 IntraHealth International, Inc. 
 
31
 * @since v2.0.0
 
32
 * @version v2.0.0
 
33
 */
 
34
 
 
35
/**
 
36
 * Page object to handle the management of all the list databases used for drop down menus.
 
37
 * 
 
38
 * This page handles all the editing of the list database tables using the relevant
 
39
 * {@link I2CE_Form} interface objects.  The base page lists all the types of data
 
40
 * that can be edited.  From there you can add a new entry or edit an existing one.
 
41
 * 
 
42
 * This object overrides the default {@link save()} and {@link action()} methods since there are many
 
43
 * database objects to be edited.
 
44
 * @package iHRIS
 
45
 * @subpackage Common
 
46
 * @access public
 
47
 */
 
48
class iHRIS_PageFormLists extends I2CE_PageForm {
 
49
        
 
50
        /**
 
51
         * @var string The type of list object being edited.
 
52
         */
 
53
        protected $type;
 
54
        /**
 
55
         * @var integer The record id number of the object being edited.
 
56
         */
 
57
        protected $id;
 
58
        
 
59
        /**
 
60
         * Create a new instance of this page.
 
61
         * 
 
62
         * This will call the parent constructor and then setup the base
 
63
         * template pages for the {@link I2CE_Template template}.  It also sets up the values
 
64
         * for the member variables.
 
65
         * @param string $title The title for this page.
 
66
         * @param string $defaultHTMLFile The default HTML file for this page.
 
67
         * @param mixed $access The role required to access this page.
 
68
         * @param array $files The list of template files to load for this page.
 
69
         */
 
70
        public function __construct( $title, $defaultHTMLFile, $access = I2CE_ROLE_ADMIN, $files=array() ) {
 
71
                parent::__construct( $title, $defaultHTMLFile, $access, $files );
 
72
                $this->type = "";
 
73
        $this->id = 0;
 
74
        if ( $this->isPost() ) {
 
75
                $this->type = $this->post('type');
 
76
                if ( $this->post_exists( 'id' ) ) {
 
77
                    $this->id = $this->post('id');
 
78
                }
 
79
            } else {
 
80
                if ( $this->get_exists( 'type' ) ) {
 
81
                    $this->type = $this->get('type');
 
82
                }
 
83
                if ( $this->get_exists( 'id' ) ) {
 
84
                    $this->id = $this->get('id');
 
85
                }
 
86
            }
 
87
        }
 
88
        
 
89
        /**
 
90
         * Load the HTML template files for editing and confirming the list information.
 
91
         * 
 
92
         * Since this page has special versions, this method is only called when a particular list object
 
93
         * is being added or edited.  All other pages are loaded within the {@link action} method.
 
94
         */
 
95
        protected function loadHTMLTemplates() {
 
96
        $this->template->addHTMLFile( "lists_form_base.html" );
 
97
        $this->template->addHTMLFile( $this->getPrimary()->getHTMLTemplate(), "tbody" );
 
98
        if ( $this->showContact() ) $this->template->appendHTMLFileById( "lists_form_contact.html", "tbody", "list_fields" );
 
99
        }
 
100
        
 
101
        /**
 
102
         * Create and load data for the objects used for this form.
 
103
         * 
 
104
         * Create the list object and if this is a form submission load
 
105
         * the data from the form data.  It determines the type based on the
 
106
         * {@link $type} member variable.
 
107
         */
 
108
        protected function loadObjects() {
 
109
                $factory = I2CE_FormFactory::instance();
 
110
        switch( $this->type ) {
 
111
                case "" : break;
 
112
                default :
 
113
                        $obj = $factory->createForm( $this->type, $this->id );
 
114
                    if ( $this->getExtra( $this->type ) && !$this->id ) {
 
115
                                $obj->load( $_GET );
 
116
                    }
 
117
                        $this->setObject( $obj );
 
118
                        break;
 
119
        }
 
120
        if ( $this->showContact() ) {
 
121
                        $contact = $factory->createForm( "contact" );
 
122
                        $contact->contact_type = $factory->getConst( "contact", "TYPE_FACILITY" );
 
123
                        $this->setObject( $contact, I2CE_PageForm::EDIT_CHILD );
 
124
                }
 
125
                parent::loadObjects();
 
126
        }
 
127
        
 
128
        /**
 
129
         * Display the save or confirm button templates as needed.
 
130
         * @param boolean $save Flag to show the save button.
 
131
         */
 
132
        protected function loadButtons( $save = false ) {
 
133
                if ( $save ) {
 
134
                $this->template->addHTMLFile( "button_save.html" );
 
135
                } else {
 
136
                $this->template->addHTMLFile( "button_confirm_admin.html" );     
 
137
                }
 
138
        }
 
139
        
 
140
        /**
 
141
         * Save the objects to the database.
 
142
         * 
 
143
         * Save the list object being edited and return to the appropriate admin page.
 
144
         */
 
145
        protected function save() {
 
146
                parent::save();
 
147
                $this->setRedirect( "lists.php?type=" . $this->type );
 
148
        }
 
149
        
 
150
        
 
151
        /**
 
152
         * Perform the actions of the page.
 
153
         * 
 
154
         * This handles some special actions because there are three versions of this page:
 
155
         * - The list page of all objects that can be edited.
 
156
         * - The add/update page for each list object.
 
157
         * - The edit form for each list object.
 
158
         * 
 
159
         * Only in the third case is the parent object action method called since that is the default setup
 
160
         * for editing objects used in most other {@link PageForm} objects.
 
161
         */
 
162
        protected function action() {
 
163
                if ( $this->type == "" ) {
 
164
                $this->template->addHTMLFile( "lists.html" );
 
165
                } elseif ( !$this->isPost() && !$this->get_exists('id') && !$this->get_exists('add') ) {
 
166
 
 
167
                        $this->loadObjects();
 
168
            if ( $select_obj =& $this->getExtra( $this->type, true ) ) {
 
169
                $this->template->addHTMLFile( "lists_" . $this->type . ".html" );
 
170
                                $select_obj->setFormLists( $this->template, true );
 
171
            } else {
 
172
                $this->template->addHTMLFile( "lists_type_" . $this->getPrimary()->getListType() . ".html" );
 
173
                if ( $this->getPrimary()->getListType() == "list" ) {
 
174
                        $this->template->appendHTMLFileById( "lists_type_header.html", "th", "lists_header" );
 
175
                        $list = I2CE_List::getList( $this->type );
 
176
                        foreach( $list as $record => $value ) {
 
177
                                $imported_row =& $this->template->appendHTMLFileById( "lists_type_row.html", "tr", "lists_body" );
 
178
                                if ( ++$cnt % 2 == 1 ) $this->template->setNodeAttribute( "class", "even", $imported_row );
 
179
                                $this->template->setDisplayData( "add_lists_id", "id=" . $record . "&type=" . $this->type );
 
180
                                $this->template->setDisplayData( "lists_row_name", $value );
 
181
                        }
 
182
                } elseif ( $this->getPrimary()->getListType() == "dual" ) {
 
183
                        $head = $this->template->appendHTMLFileById( "lists_type_header.html", "th", "lists_header" );
 
184
                        $this->template->setNodeAttribute( "colspan", "2", $head );
 
185
                        $list = $this->getPrimary()->getOptionsByLink();
 
186
                        $link_form = "";
 
187
                        $add_link_rows = array();
 
188
                        foreach( $list as $link => $options ) {
 
189
                                if ( $link_form == "" ) $link_form = I2CE_List::lookupFormName( $link );
 
190
                                $link_value = I2CE_List::lookup( $link, $link_form );
 
191
                                $add_link_rows[ $link_value ] = array();
 
192
                                foreach( $options as $record => $value ) {
 
193
                                        $add_link_rows[ $link_value ][$record] = $value;
 
194
                                }
 
195
                        }
 
196
                        
 
197
                        $link_rows = array_keys( $add_link_rows );
 
198
                        sort( $link_rows );
 
199
                        foreach( $link_rows as $link_value ) {
 
200
                                foreach( $add_link_rows[$link_value] as $record => $value ) {
 
201
                                        $imported_row =& $this->template->appendHTMLFileById( "lists_type_dual_row.html", "tr", "lists_body" );
 
202
                                        if ( ++$cnt % 2 == 1 ) $this->template->setNodeAttribute( "class", "even", $imported_row );
 
203
                                        $this->template->setDisplayData( "add_lists_id", "id=" . $record . "&type=" . $this->type );
 
204
                                        $this->template->setDisplayData( "lists_row_name", $value );
 
205
                                        $this->template->setDisplayData( "lists_row_link", $link_value );                                       
 
206
                                }
 
207
                        }
 
208
                        
 
209
                } else {
 
210
                        I2CE_List::listOptions( $this->template, "add_ids", $this->type );
 
211
                }
 
212
            }
 
213
            $this->setDisplayData();
 
214
            $this->setForm();
 
215
                } else {
 
216
                        parent::action();
 
217
                }
 
218
                $this->template->setAttribute( "class", "active", "menuLists", "a[@href='lists.php']" );
 
219
        }
 
220
 
 
221
        /**
 
222
         * Check if this type has a special page to display for the edit selection lists.
 
223
         * 
 
224
         * This will be needed if the object is linked to another form so the drop downs can be displayed.
 
225
         * Optionally this method will return the object to use to populate the drop downs.
 
226
         * @param string $type
 
227
         * @param boolean $return_object
 
228
         * @return mixed Boolean or the I2CE_Form object to populate the drop downs.
 
229
         */
 
230
        public function getExtra( $type, $return_object=false ) {
 
231
                if ( $type == "region" || $type == "district" || $type == "county" ) {
 
232
                        if ( $return_object ) return new iHRIS_LocationSelect( $type );
 
233
                        else return true;               
 
234
                } else {
 
235
                        return false;
 
236
                }
 
237
        }
 
238
        
 
239
        /**
 
240
         * Check to see if the current type should display contact information when editing this list object.
 
241
         * @return boolean
 
242
         */
 
243
        public function showContact() {
 
244
                return false;
 
245
        }
 
246
}
 
247
 
 
248
?>