source: trunk/contactcenter/inc/class.so_main.inc.php @ 2

Revision 2, 22.8 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /***************************************************************************\
3  * eGroupWare - Contacts Center                                              *
4  * http://www.egroupware.org                                                 *
5  *                                                                           *
6  * Storage Object Classes                                                    *
7  * Written by:                                                               *
8  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
9  *  sponsored by Thyamad - http://www.thyamad.com                            *
10  * ------------------------------------------------------------------------- *
11  *  This program is free software; you can redistribute it and/or modify it  *
12  *  under the terms of the GNU General Public License as published by the    *
13  *  Free Software Foundation; either version 2 of the License, or (at your   *
14  *  option) any later version.                                               *
15  \***************************************************************************/
16
17        /*!
18                This is the main SO class. Every other SO class is derived from this.
19                The purpose of this class is to centralize the search, checkout and
20                commit methods, that are designed to be common to all derived classes.
21        */
22
23        class so_main
24        {
25
26                // The ID of the class, usually its primary key on the DB.
27                var $id;
28               
29                // The state of this object
30                var $state;
31               
32                // The DB object
33                var $db;
34               
35                /**
36                 * The DB tables' used by the class
37                 *
38                 * This attribute is VERY important. It is responsible
39                 * to maintain all the DB Tables' Information.
40                 *
41                 * It's format is:
42                 *
43                 * $db_tables = array(
44                 *              '<table1_name>' => array(
45                 *                      'type'      => '<table_type>',
46                 *                      'keys'      => array(
47                 *                              'primary'       => array(
48                 *                                      <pkey1> => array(
49                 *                                              'name'  => '<key_field_name>',
50                 *                                              'value' =>      <key_value>),
51                 *                                      <pkey2> => array(...)),
52                 *                              'foreign'       => array(
53                 *                                      <fkey1> => array(
54                 *                                              'name'                  => '<key_field_name>',
55                 *                                              'value'                 => <key_value>,
56                 *                                              'association'   => array(
57                 *                                                      'table'         => '<original_table>',
58                 *                                                      'field'         => '<original_field>')),
59                 *                                      <fkey2> => array(...)),
60                 *                      'fields'        => array(
61                 *                              <field1>        => array(
62                 *                                      'name'  => '<field_name>',
63                 *                                      'type'  => '<field_type>',
64                 *                                      'state' => '<field_state>',
65                 *                                      'value' => <field_value>),
66                 *                              <field2>        => array(...)))),
67                 *              '<table2_name>' => array(...));
68                 *
69                 * The variables:
70                 *      <table_type> can be: (if you find any other type, please, report it).
71                 *              - 'main'        => The main table used by the class. In essence,
72                 *                      this table
73                 *              - 'single'      => The single tables are the ones that each field
74                 *                      keeps just one value;
75                 *              - 'multi'       => The multi tables are those in which fields keeps
76                 *                      multiple values. So, for this type of tables, the 'state'
77                 *                      and 'value' properties becomes arrays and their names are
78                 *                      'states' and 'values';
79                 *
80                 *      <field_type> can be: (report if find any other)
81                 *              - false         => The field is an ordinary field;
82                 *              - 'primary'     => The field is a primary key;
83                 *              - 'foreign' => The field is a foreign key;
84                 *              - array()       => The field is a multi-type field. The elements of
85                 *                      the array specifies the types.
86                 */
87                       
88                var $db_tables = array();
89
90                var $remove_all = false;
91       
92                function init()
93                {
94                        $this->db = $GLOBALS['phpgw']->db;
95                }
96
97                /*!
98               
99                        @function get_field
100                        @abstract Returns the value of the Field specified
101                        @author Raphael Derosso Pereira
102                       
103                        @param string $field The name of the Field
104               
105                */
106                function get_field($field)
107                {
108                        if(array_key_exists($field, $this->main_fields))
109                        {
110                                return $this->main_fields[$field]['value'];
111                        }
112                       
113                        // TODO: Log?
114                }
115       
116                function get_state()
117                {
118                        return $this->state;
119                }       
120
121                /*!
122               
123                        @function set_field
124                        @abstract Sets the value of the Field specified
125                        @author Raphael Derosso Pereira
126                       
127                        @param string $field The name of the Field
128                        @param mixed $value The value to be setup
129               
130                */
131                function set_field($field, $value)
132                {
133                        if(array_key_exists($field, $this->main_fields))
134                        {
135                                $this->main_fields[$field]['value'] = $value;
136                                $this->manage_fields($this->main_fields[$field], 'changed');
137                        }
138                       
139                        // TODO: Log?
140                        return;
141                }
142
143                /*!
144               
145                        @function checkout
146                        @abstract Load object from DB
147                        @author Raphael Derosso Pereira
148                       
149                        @param integer $id The object ID (primary_key)
150               
151                        @notes IMPORTANT!!! This version just loads from DB in the case
152                                where there is just ONE primary key in the main table. If you
153                                want to extend it, go ahead and share the code!!! :)
154                */
155               
156                function checkout ( $id )
157                {
158                        $query_main_select = 'SELECT ';
159                        $query_main_from = ' FROM ';
160                        $query_main_where = ' WHERE ';
161                       
162                        $query_multi_select = array();
163                        $query_multi_from = array();
164                        $query_multi_where = array();
165                       
166                        $table_main = '';
167                        reset($this->db_tables);
168                        while(list($table, $table_info) = each($this->db_tables))
169                        {
170                                if ($table_info['type'] === 'main')
171                                {
172                                        $table_main = $table;
173                                        $main_pkey = $table_info['keys']['primary'][0]['name'];
174                                       
175                                        foreach($table_info['fields'] as $field_info)
176                                        {
177                                                $query_main_select .= $table.'.'.$field_info['name'].',';
178                                        }
179
180                                        $query_main_select{strlen($query_main_select)-1} = ' ';         
181                                        $query_main_from .= $table;
182                                        $query_main_where .= $table_info['keys']['primary'][0]['name'].'=\''.$id.'\'';
183                                       
184                                        break;
185                                }
186                        }
187                       
188                        reset($this->db_tables);
189                        while(list($table, $table_info) = each($this->db_tables))
190                        {
191                                if ($table_info['type'] === 'multi')
192                                {
193                                        $query_multi_select[$table] = 'SELECT ';
194                                        $query_multi_from[$table] = ' FROM ';
195                                        $query_multi_where[$table] = ' WHERE ';
196                               
197                                        foreach($table_info['fields'] as $field_info)
198                                        {
199                                                $query_multi_select[$table] .= $table.'.'.$field_info['name'].',';
200                                        }
201
202                                        $query_multi_select[$table]{strlen($query_multi_select[$table])-1} = ' ';               
203                                        $query_multi_from[$table] .= $table;
204                                        $query_multi_where[$table] .= $table_info['keys']['primary'][0]['name'].'=\''.$id.'\'';
205                                }
206                        }
207
208                        $query = $query_main_select . $query_main_from . $query_main_where;
209
210//                      echo 'Query in SO_Main CheckOut: "'.$query.'"<br>';
211                       
212                        if (!$this->db->query($query,__LINE__, __FILE__))
213                        {
214                                return false;
215                        }
216                       
217                        if (!$this->db->next_record())
218                        {
219                                return false;
220                        }
221                       
222                        reset($this->db_tables[$table_main]['fields']);
223
224                        while (list(,$field_info) = each($this->db_tables[$table_main]['fields']))
225                        {
226                                $this->db_tables[$table_main]['fields'][$field_info['name']]['value'] = $this->db->f($field_info['name']);
227                                $this->manage_fields($this->db_tables[$table_main]['fields'][$field_info['name']], 'sync');
228                        }
229
230                        foreach($query_multi_select as $table => $query)
231                        {
232                                $query = $query_multi_select[$table] . $query_multi_from[$table] . $query_multi_where[$table];
233
234                                if (!$this->db->query($query, __LINE__, __FILE__))
235                                {
236                                        return false;
237                                }
238                               
239                                $pos = 0;
240                                while($this->db->next_record())
241                                {
242                                        reset($this->db_tables[$table]['fields']);                                     
243                                        while (list(,$field_info) = each($this->db_tables[$table]['fields']))
244                                        {
245                                                $this->db_tables[$table]['fields'][$field_info['name']]['values'][$pos] = $this->db->f($field_info['name']);
246                                                $this->manage_fields($this->db_tables[$table]['fields'][$field_info['name']], 'sync', 'new');
247                                        }
248                                        $pos++;
249                                }
250                        }
251                       
252                        return true;
253                }
254       
255                /*!
256               
257                        @function commit
258                        @abstract Saves object to DB
259                        @author Raphael Derosso Pereira
260                       
261                        @return integer The object ID (primary_key)
262               
263                */
264                function commit ( )
265                {
266                        if ($this->state === 'new')
267                        {
268                                return $this->commit_new();
269                        }
270                        else if ($this->state === 'delete')
271                        {
272                                return $this->delete_record();
273                        }
274                       
275                        $query_main_head = 'UPDATE ';
276                        $query_main_tail = ' WHERE ';
277                        $query_main_fields = false;
278                       
279                        $query_multi_head = array();
280                        $query_multi_fields = array();
281
282                        $table_main = '';
283                        $main_table_changed = false;
284                        reset($this->db_tables);
285                        while(list($table, $table_info) = each($this->db_tables))
286                        {
287                                if ($table_info['type'] === 'main')
288                                {
289                                        $table_main = $table;
290                                        $main_pkey = $table_info['keys']['primary'][0]['name'];
291                                       
292                                        $query_main_head .= $table . ' SET ';
293                                       
294                                        foreach($table_info['fields'] as $field_info)
295                                        {
296                                                if ($field_info['state'] === 'changed' or $field_info['state'] === 'new')
297                                                {
298                                                        $main_table_changed = true;
299
300                                                        if ($field_info['value'] == null)
301                                                        {
302                                                                $sep = '';
303                                                        }
304                                                        else
305                                                        {
306                                                                $sep = "'";
307                                                        }
308
309                                                        if ($field_info['name'] === 'photo')
310                                                        {
311                                                                $query_main_fields .= $field_info['name'].'='.$this->db->quote($field_info['value'], 'blob').',';
312                                                        }
313                                                        else
314                                                        {
315                                                                $f_value = $field_info['value'] === null ? 'NULL' : "'".$field_info['value']."'";
316                                                                $query_main_fields .= $field_info['name'].'='.$f_value.',';
317                                                        }
318                                                        $this->manage_fields($this->main_fields[$field_info['name']], 'sync');
319                                                }
320                                                else if ($field_info['state'] === 'deleted')
321                                                {
322                                                        $main_table_changed = true;
323                                                        $query_main_fields .= $field_info['name'].'=NULL,';
324                                                        $this->manage_fields($this->main_fields[$field_info['name']], 'sync');
325                                                }
326                                        }
327
328                                        if (!$main_table_changed)
329                                        {
330                                                continue;
331                                        }
332                                       
333                                        $query_main_fields{strlen($query_main_fields)-1} = ' ';
334                                       
335                                        $query_main_tail .= $main_pkey.'=\''.$this->id.'\'';
336
337                                        $query = $query_main_head . $query_main_fields . $query_main_tail;
338                                       
339                                        //echo '<p>Main Update Query: "'.$query.'"</p>';
340               
341                                        if (!$this->db->query($query, __LINE__, __FILE__))
342                                        {
343                                                return false;
344                                        }
345                                }
346                                else if ($table_info['type'] === 'multi')
347                                {
348                                        if (!$this->commit_multi($table, $table_info))
349                                        {
350                                                exit('Couldn\'t commit on table "'.$table.'"');
351                                        }
352                                }
353                        }
354                       
355                        $this->state = 'sync';
356
357                        return $this->id;
358                }
359               
360                /*!
361               
362                        @function delete_record
363                        @abstract Removes the current entry from the DB
364                        @author Raphael Derosso Pereira
365               
366                */
367                function delete_record ()
368                {
369                        reset($this->db_tables);
370                        while(list($table, $table_info) = each($this->db_tables))
371                        {
372                                if ($table_info['type'] === 'main')
373                                {
374                                        $where = false;
375                                        $where[$table_info['keys']['primary'][0]['name']] = $table_info['keys']['primary'][0]['value'];
376                                       
377                                        $sql = "DELETE FROM $table WHERE ".$table_info['keys']['primary'][0]['name']."='".$table_info['keys']['primary'][0]['value']."'";
378                                       
379                                        if (!$this->db->query($sql, __LINE__, __FILE__))
380                                        {
381                                                return false;
382                                        }
383
384                                        //if (!$this->db->delete($table,$where,__LINE__,__FILE__))
385                                        //{
386                                        //      return false;
387                                        //}
388                                       
389                                        continue;
390                                }
391                                else if ($table_info['type'] === 'multi')
392                                {
393                                        $where = false;
394                                        $n_values = count($table_info['keys']['primary'][0]['values']);
395                                        for ($i = 0; $i < $n_values; $i++)
396                                        {
397                                                unset($where);
398                                                foreach ($table_info['keys']['primary'] as $key)
399                                                {
400                                                        //$where[$key['name']] = $key['values'][$i];
401                                                        $where[] = $key['name']."='".$key['values'][$i]."'";
402                                                }
403                                               
404                                                //print_r($where);
405                                                $sql = "DELETE FROM $table WHERE ".implode(' AND ', $where);
406                                               
407                                                //echo $sql;
408                                               
409                                                if (!$this->db->query($sql,__LINE__,__FILE__))
410                                                {
411                                                        return false;
412                                                }
413                                               
414                                        }
415                                        continue;
416                                }
417                        }
418                       
419                        return true;
420                }
421               
422                /*!
423               
424                        @function commit_new
425                        @abstract Sets the new ID and commits the data to the DB
426                        @author Raphael Derosso Pereira
427                       
428                        @return integer The new ID
429               
430                */
431                function commit_new()
432                {
433                        $query_main_head = 'INSERT INTO ';
434                        $query_main_fields = '( ';
435                        $query_main_values = ' VALUES ( ';
436                       
437                        $query_multi_head = array();
438                        $query_multi_fields = array();
439                        $query_multi_values = array();
440
441                        reset($this->db_tables);
442                        while(list($table, $table_info) = each($this->db_tables))
443                        {
444                                if ($table_info['type'] === 'main')
445                                {
446                                        $main_table_changed = false;
447
448                                        $table_main = $table;
449                                        $main_pkey = $table_info['keys']['primary'][0]['name'];
450                                       
451                                        $query_main_head .= $table . ' ';
452                                       
453                                        $query_id = 'SELECT MAX('.$main_pkey.') AS new_id FROM '.$table_main;
454                                       
455                                        //echo 'Query MAX: '.$query_id.'<br>';
456                                        if (!$this->db->query($query_id, __LINE__, __FILE__) or !$this->db->next_record())
457                                        {
458                                                exit('Error in '.__FILE__.' line: '.__LINE__);
459                                        }
460                                       
461                                        $this->id = $this->db->f('new_id')+1;
462                                        $this->manage_fields($this->main_fields[$main_pkey], 'sync');
463                                        //echo 'ID: '.$this->id.' Type: '.gettype($this->id);
464                               
465                                        foreach($table_info['fields'] as $field_info)
466                                        {
467                                                if ($field_info['state'] === 'changed')
468                                                {
469                                                        $main_table_changed = true;
470                                                        $query_main_fields .= $field_info['name'].',';
471                                                       
472                                                        if ($field_info['name'] === 'photo')
473                                                        {
474                                                                $query_main_values .= $this->db->quote($field_info['value'], 'blob').',';
475                                                        }
476                                                        else
477                                                        {
478                                                                $query_main_values .= $field_info['value'] === null ? 'NULL,' : "'".$field_info['value']."',";
479                                                        }
480                                                        $this->manage_fields($this->main_fields[$field_info['name']], 'sync');
481                                                }
482                                        }
483                                       
484                                        if (!$main_table_changed)
485                                        {
486                                                continue;
487                                        }
488                                       
489                                        $query_main_fields .= $main_pkey.')';
490                                        $query_main_values .= '\''.$this->id.'\')';
491
492                                        $query = $query_main_head . $query_main_fields . $query_main_values;
493                                       
494                                        //echo '<p>Main Insert Query: "'.$query.'"</p>';
495
496                                        if (!$this->db->query($query, __LINE__, __FILE__))
497                                        {
498                                                return false;
499                                        }
500                                }
501                                else if ($table_info['type'] === 'multi')
502                                {
503                                        $this->commit_multi($table, $table_info);
504                                }
505                        }
506                       
507                        $this->state = 'sync';
508                        return $this->id;
509                }
510
511                /*!
512               
513                        @function commit_multi
514                        @abstract Handles the commit of Multi Values tables
515                        @author Raphael Derosso Pereira
516                       
517                        @param string $table The table name
518                        @param array $table_info The table infrastructure
519               
520                */
521                function commit_multi($table, $table_info)
522                {
523                        reset($table_info['fields']);
524                        list(,$t_field) = each($table_info['fields']);
525                        $n_values = count($t_field['values']);
526                       
527                        //echo 'Table: '.$table.'<br>n_values => '.$n_values.'<br>';
528                       
529                        reset($table_info['fields']);
530                        for ($i = 0; $i < $n_values; $i++)
531                        {
532                                $multi_table_changed = false;
533                                $query_multi_update_head = 'UPDATE '.$table.' SET ';
534                                $query_multi_insert_head = 'INSERT INTO '.$table;
535                                $query_multi_update_fields = false;
536                                $query_multi_insert_fields = '(';
537                                $query_multi_insert_values = ' VALUES (';
538                                $query_multi_update_tail = ' WHERE ';
539                                $main_pkeys = $table_info['keys']['primary'];
540                               
541                                foreach($table_info['fields'] as $field_info)
542                                {
543                                        $f_value = $field_info['values'][$i] == null ? 'NULL' : $field_info['values'][$i];
544                                       
545                                        switch ($field_info['states'][$i])
546                                        {
547                                                case 'changed':
548                                                        $multi_table_changed = 'changed';
549                                                        $query_multi_update_fields .= $field_info['name'].'='.$f_value.',';
550                                                        $this->manage_fields($this->db_tables[$table]['fields'][$field_info['name']], 'sync', $i);
551                                                        break;
552                                       
553                                                case 'new':
554                                                        $multi_table_changed = 'new';
555                                                        $query_multi_insert_fields .= $field_info['name'].',';
556                                                        $query_multi_insert_values .= $f_value.',';
557                                                        $this->manage_fields($this->db_tables[$table]['fields'][$field_info['name']], 'sync', $i);
558                                                        break;
559
560                                                case 'deleted':
561                                                        $multi_table_changed = 'deleted';
562                                                        //unset($this->db_tables[$table]['fields'][$field_info['name']]['values'][$i]);
563                                                        //unset($this->db_tables[$table]['fields'][$field_info['name']]['states'][$i]);
564                                                        break;
565                                        }
566                                }
567
568                                if (!$multi_table_changed)
569                                {
570                                        continue;
571                                }
572                               
573                                $query_multi_update_fields{strlen($query_multi_update_fields)-1} = ' ';
574
575                                foreach($main_pkeys as $pkey)
576                                {
577                                        $query_multi_update_tail_t[] = $pkey['name'].'=\''.$pkey['values'][$i].'\'';
578                                }
579                                $query_multi_update_tail .= @implode(' AND ', $query_multi_update_tail_t);
580                                unset($query_multi_update_tail_t);
581                               
582                                $query_multi_insert_fields{strlen($query_multi_insert_fields)-1} = ')';
583                                $query_multi_insert_values{strlen($query_multi_insert_values)-1} = ')';
584
585                                switch ($multi_table_changed)
586                                {
587                                        case 'changed':
588                                                $query = $query_multi_update_head . $query_multi_update_fields . $query_multi_update_tail;
589                                                break;
590                                               
591                                        case 'new':
592                                                $query = $query_multi_insert_head . $query_multi_insert_fields . $query_multi_insert_values;
593                                                break;
594
595                                        case 'deleted':
596                                                $query = 'DELETE FROM ' . $table . $query_multi_update_tail;
597                                }
598
599//                              echo '<p>Multi Query, type '.$multi_table_changed.': "'.$query.'"</p>';
600
601                                if (!$this->db->query($query, __LINE__, __FILE__))
602                                {
603                                        return false;
604                                }
605                        }
606/*
607                        foreach($this->db_tables[$table]['fields'] as $field_info)
608                        {
609                                $this->db_tables[$table]['fields'][$field_info['name']]['values'] = array_values($field_info['values']);
610                                $this->db_tables[$table]['fields'][$field_info['name']]['states'] = array_values($field_info['states']);
611                        }
612*/
613                        return true;
614                }
615       
616                /*!
617               
618                        @function get_id
619                        @abstract Return the object ID
620                        @author Raphael Derosso Pereira
621                       
622                */
623                function get_id (  )
624                {
625                        return $this->id;
626                }
627       
628                /*!
629               
630                        @function get_db_tables
631                        @abstract Return the object DB Tables
632                        @author Raphael Derosso Pereira
633                       
634                */
635                function get_db_tables (  )
636                {
637                        return $this->db_tables;
638                }
639
640                /**
641                 * Misc Methods
642                 */
643                 
644                /*!
645               
646                        @function get_class_by_field
647                        @abstract Returns the name of the class that has the specified
648                                field
649                        @author Raphael Derosso Pereira
650                        @param string $field The field name
651                                       
652                */
653                function get_class_by_field ($field)
654                {
655                        $class_name = '_UNDEF_';
656                       
657                        switch ($field)
658                        {
659                                case 'id_status':
660                                case 'status_name':
661                                        $class_name = 'so_status';
662                                        break;                                         
663
664                                case 'id_prefix':
665                                case 'prefix':
666                                        $class_name = 'so_prefix';
667                                        break;                                         
668
669                                case 'id_suffix':
670                                case 'suffix':
671                                        $class_name = 'so_suffix';
672                                        break;                                         
673
674                                case 'id_contact':
675                                case 'id_owner':
676                                case 'photo':
677                                case 'alias':
678                                case 'given_names':
679                                case 'family_names':
680                                case 'names_ordered':
681                                case 'birthdate':
682                                case 'sex':
683                                case 'pgp_key':
684                                case 'notes':
685                                case 'id_related':
686                                case 'is_global':
687                                case 'title':
688                                case 'department':
689                                        $class_name = 'so_contact';
690                                        break;                                         
691                               
692                                case 'id_company':
693                                case 'id_company_owner':
694                                case 'company_name':
695                                case 'company_notes':
696                                        $class_name = 'so_company';
697                                        break;                         
698                                       
699                                case 'legal_info_name':
700                                case 'legal_info_value':
701                                        $class_name = 'so_company_legal';
702                                        break;                                         
703
704                                case 'id_typeof_company_legal':
705                                case 'company_legal_type_name':
706                                        $class_name = 'so_company_legal_type';
707                                        break;                                         
708                                       
709                                case 'id_typeof_contact_relation':
710                                case 'contact_relation_name':
711                                case 'contact_relation_is_subordinated':
712                                        $class_name = 'so_contact_relation_type';
713                                        break;                                         
714                                       
715                                case 'id_typeof_company_relation':
716                                case 'company_relation_name':
717                                case 'company_relation_is_subordinated':
718                                        $class_name = 'so_company_relation_type';
719                                        break;                                         
720                                       
721                                case 'id_address':
722                                case 'address1':
723                                case 'address2':
724                                case 'complement':
725                                case 'address_other':
726                                case 'postal_code':
727                                case 'po_box':
728                                case 'address_is_default':
729                                        $class_name = 'so_address';
730                                        break;
731
732                                case 'id_city':
733                                case 'city_name':
734                                case 'city_timezone':
735                                case 'city_geographic_location':
736                                        $class_name = 'so_city';
737                                        break;
738
739                                case 'id_state':
740                                case 'state_name':
741                                case 'state_symbol':
742                                        $class_name = 'so_state';
743                                        break;
744                               
745                                case 'country_name':
746                                        $class_name = 'so_country';
747                                        break;
748                                       
749                                case 'id_typeof_contact_address':
750                                case 'contact_address_type_name':
751                                        $class_name = 'so_contact_address_type';
752                                        break;
753                                       
754                                case 'id_typeof_company_address':
755                                case 'company_address_type_name':
756                                        $class_name = 'so_company_address_type';                                               
757                                       
758                                case 'id_connection':
759                                case 'connection_name':
760                                case 'connection_value':
761                                case 'connection_is_default':
762                                        $class_name = 'so_connection';
763                                        break;                                         
764                                       
765                                case 'id_typeof_contact_connection':
766                                case 'contact_connection_type_name':
767                                        $class_name = 'so_contact_connection_type';
768                                        break;
769                                       
770                                case 'id_typeof_company_connection':
771                                case 'company_connection_type_name':
772                                        $class_name = 'so_company_connection_type';
773                                        break;
774
775                                default:
776                                        break;                                         
777
778                        }
779                       
780                        if ($class_name == '_UNDEF_')
781                        {
782                                return false;
783                        }
784                       
785                        return 'contactcenter.'.$class_name;
786                }
787               
788                /*!
789                       
790                        @function get_table_by_field
791                        @abstract Returns the table wich holds the specified field
792                        @author Raphael Derosso Pereira
793                       
794                        @param string $field The field to be found
795                        @param array $tables The array returned by get_db_tables
796                */
797                function get_table_by_field ($field, & $tables)
798                {
799                        $return = false;
800                       
801                        reset($tables);
802                        while(list($table, $properties) = each($tables))
803                        {
804                                if(array_key_exists($properties['fields']))
805                                {
806                                        if (strtolower($properties['type']) === 'main')
807                                        {
808                                                return $table;
809                                        }
810                                        array_push($return,$table);
811                                }
812                        }
813                       
814                        return $return;
815                }
816
817                /*!
818               
819                        @function manage_fields
820                        @abstract Change the state of the fields
821                        @author Raphael Derosso Pereira
822                       
823                        @param mixed $field The reference to the field
824                        @param string $new_state The new field state
825                        @param mixed $position [optional] The position where the field
826                                is state is to be stored. This is used when the table type
827                                is 'multi'.
828               
829                */
830                function manage_fields(& $field, $new_state, $position = false)
831                {
832                        if ($this->state !== 'delete')
833                        {
834                                if ($this->state != 'new')
835                                {
836                                        $this->state = 'changed';
837                                }
838                               
839                                if ($position === false)
840                                {
841                                        $field['state'] = $new_state;
842                                       
843                                        return;
844                                }
845                               
846                                if ($position !== 'new')
847                                {
848                                        $field['states'][$position] = $new_state;
849                                }
850                                else
851                                {
852                                        array_push($field['states'], $new_state);
853                                }
854                        }
855                }
856               
857                /*!
858                       
859                        @function reset_values
860                        @abstract Reset all the values to false and all
861                                states to empty
862                        @author Raphael Derosso Pereira
863                */
864                function reset_values()
865                {
866                        foreach($this->db_tables as $table_name => $table_info)
867                        {
868                                foreach($table_info['fields'] as $field => $field_info)
869                                {
870                                        if ($field_info['value'])
871                                        {
872                                                $this->db_tables[$table_name]['fields'][$field]['value'] = false;
873                                                $this->manage_fields($this->db_tables[$table_name]['fields'][$field], 'empty');
874                                        }
875                                        else if ($field_info['values'])
876                                        {
877                                                unset($this->db_tables[$table_name]['fields'][$field]['states']);
878                                                unset($this->db_tables[$table_name]['fields'][$field]['values']);
879                                        }
880                                }
881                        }
882                        $this->state = 'new';
883                }
884
885
886                /*!
887               
888                        @function remove
889                        @abstract Removes the entry from the DB
890                        @author Raphael Derosso Pereira
891                */
892                function remove()
893                {
894                        $this->state = 'delete';
895                       
896                        $result = $this->commit();
897                        $this->reset_values();
898                        return $result;
899                }
900       
901        }
902?>
Note: See TracBrowser for help on using the repository browser.