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

Revision 7655, 23.0 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

  • 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                        if($table_main!='') { //Quando faço checkout e não incluo a tabela principal, a função precisa funcionar...
209                                $query = $query_main_select . $query_main_from . $query_main_where;
210       
211//                      echo 'Query in SO_Main CheckOut: "'.$query.'"<br>';
212                               
213                                if ($this->db->query($query,__LINE__, __FILE__)) {
214                                        if ($this->db->next_record()) {
215                                                reset($this->db_tables[$table_main]['fields']);
216                       
217                                                while (list(,$field_info) = each($this->db_tables[$table_main]['fields']))
218                                                {
219                                                        $this->db_tables[$table_main]['fields'][$field_info['name']]['value'] = $this->db->f($field_info['name']);
220                                                        $this->manage_fields($this->db_tables[$table_main]['fields'][$field_info['name']], 'sync');
221                                                }
222                                        }
223                                }
224                        }
225                        //echo "\n\n\n".$query_multi_select;
226                        foreach($query_multi_select as $table => $query)
227                        {
228                               
229                                $query = $query_multi_select[$table] . $query_multi_from[$table] . $query_multi_where[$table];
230
231                                //echo 'Query in SO_Main CheckOut: "'.$query.'"<br>';
232                                if ($this->db->query($query, __LINE__, __FILE__)) {
233                                        $pos = 0;
234                                        while($this->db->next_record())
235                                        {
236                                                reset($this->db_tables[$table]['fields']);                                     
237                                                while (list(,$field_info) = each($this->db_tables[$table]['fields']))
238                                                {
239                                                        $this->db_tables[$table]['fields'][$field_info['name']]['values'][$pos] = $this->db->f($field_info['name']);
240                                                        $this->manage_fields($this->db_tables[$table]['fields'][$field_info['name']], 'sync', 'new');
241                                                }
242                                                ++$pos;
243                                        }
244                                        ++$pos;
245                                }
246                        }
247                       
248                        return true;
249                }
250       
251                /*!
252               
253                        @function commit
254                        @abstract Saves object to DB
255                        @author Raphael Derosso Pereira
256                       
257                        @return integer The object ID (primary_key)
258               
259                */
260                function commit ( )
261                {
262                        if ($this->state === 'new')
263                        {
264                                return $this->commit_new();
265                        }
266                        else if ($this->state === 'delete')
267                        {
268                                return $this->delete_record();
269                        }
270                       
271                        $query_main_head = 'UPDATE ';
272                        $query_main_tail = ' WHERE ';
273                        $query_main_fields = false;
274                       
275                        $query_multi_head = array();
276                        $query_multi_fields = array();
277
278                        $table_main = '';
279                        $main_table_changed = false;
280                        reset($this->db_tables);
281                        while(list($table, $table_info) = each($this->db_tables))
282                        {
283                                if ($table_info['type'] === 'main')
284                                {
285                                        $table_main = $table;
286                                        $main_pkey = $table_info['keys']['primary'][0]['name'];
287                                       
288                                        $query_main_head .= $table . ' SET ';
289                                       
290                                        foreach($table_info['fields'] as $field_info)
291                                        {
292                                                if ($field_info['state'] === 'changed' or $field_info['state'] === 'new')
293                                                {
294                                                        $main_table_changed = true;
295
296                                                        if ($field_info['value'] == null)
297                                                        {
298                                                                $sep = '';
299                                                        }
300                                                        else
301                                                        {
302                                                                $sep = "'";
303                                                        }
304
305                                                        if ($field_info['name'] === 'photo')
306                                                        {
307                                                                $query_main_fields .= $field_info['name'].'='.$this->db->quote($field_info['value'], 'blob').',';
308                                                        }
309                                                        else
310                                                        {
311                                                                $f_value = $field_info['value'] === null ? 'NULL' : "'".$field_info['value']."'";
312                                                                $query_main_fields .= $field_info['name'].'='.$f_value.',';
313                                                        }
314                                                        $this->manage_fields($this->main_fields[$field_info['name']], 'sync');
315                                                }
316                                                else if ($field_info['state'] === 'deleted')
317                                                {
318                                                        $main_table_changed = true;
319                                                        $query_main_fields .= $field_info['name'].'=NULL,';
320                                                        $this->manage_fields($this->main_fields[$field_info['name']], 'sync');
321                                                }
322                                        }
323
324                                        if (!$main_table_changed)
325                                        {
326                                                continue;
327                                        }
328                                       
329                                        $query_main_fields{strlen($query_main_fields)-1} = ' ';
330                                       
331                                        $query_main_tail .= $main_pkey.'=\''.$this->id.'\'';
332
333                                        $query = $query_main_head . $query_main_fields . $query_main_tail;
334                                       
335                                        //echo '<p>Main Update Query: "'.$query.'"</p>';
336               
337                                        if (!$this->db->query($query, __LINE__, __FILE__))
338                                        {
339                                                return false;
340                                        }
341                                }
342                                else if ($table_info['type'] === 'multi')
343                                {
344                                        if (!$this->commit_multi($table, $table_info))
345                                        {
346                                                exit('Couldn\'t commit on table "'.$table.'"');
347                                        }
348                                }
349                        }
350                       
351                        $this->state = 'sync';
352
353                        return $this->id;
354                }
355               
356                /*!
357               
358                        @function delete_record
359                        @abstract Removes the current entry from the DB
360                        @author Raphael Derosso Pereira
361               
362                */
363                function delete_record ()
364                {
365                        reset($this->db_tables);
366                        while(list($table, $table_info) = each($this->db_tables))
367                        {
368                                if ($table_info['type'] === 'main')
369                                {
370                                        $where = false;
371                                        $where[$table_info['keys']['primary'][0]['name']] = $table_info['keys']['primary'][0]['value'];
372                                       
373                                        $sql = "DELETE FROM $table WHERE ".$table_info['keys']['primary'][0]['name']."='".$table_info['keys']['primary'][0]['value']."'";
374                                       
375                                        if (!$this->db->query($sql, __LINE__, __FILE__))
376                                        {
377                                                return false;
378                                        }
379
380                                        //if (!$this->db->delete($table,$where,__LINE__,__FILE__))
381                                        //{
382                                        //      return false;
383                                        //}
384                                       
385                                        continue;
386                                }
387                                else if ($table_info['type'] === 'multi')
388                                {
389                                        $where = false;
390                                        $n_values = count($table_info['keys']['primary'][0]['values']);
391                                        for ($i = 0; $i < $n_values; ++$i)
392                                        {
393                                                unset($where);
394                                                foreach ($table_info['keys']['primary'] as $key)
395                                                {
396                                                        //$where[$key['name']] = $key['values'][$i];
397                                                        $where[] = $key['name']."='".$key['values'][$i]."'";
398                                                }
399                                               
400                                                //print_r($where);
401                                                $sql = "DELETE FROM $table WHERE ".implode(' AND ', $where);
402                                               
403                                                //echo $sql;
404                                               
405                                                if (!$this->db->query($sql,__LINE__,__FILE__))
406                                                {
407                                                        return false;
408                                                }
409                                               
410                                        }
411                                        continue;
412                                }
413                        }
414                       
415                        return true;
416                }
417               
418                /*!
419               
420                        @function commit_new
421                        @abstract Sets the new ID and commits the data to the DB
422                        @author Raphael Derosso Pereira
423                       
424                        @return integer The new ID
425               
426                */
427                function commit_new()
428                {
429                        $query_main_head = 'INSERT INTO ';
430                        $query_main_fields = '( ';
431                        $query_main_values = ' VALUES ( ';
432                       
433                        $query_multi_head = array();
434                        $query_multi_fields = array();
435                        $query_multi_values = array();
436
437                        reset($this->db_tables);
438                        while(list($table, $table_info) = each($this->db_tables))
439                        {
440                                if ($table_info['type'] === 'main')
441                                {
442                                        $main_table_changed = false;
443
444                                        $table_main = $table;
445                                        $main_pkey = $table_info['keys']['primary'][0]['name'];
446                                       
447                                        $query_main_head .= $table . ' ';
448                                       
449                                        $query_id = 'SELECT MAX('.$main_pkey.') AS new_id FROM '.$table_main;
450                                       
451                                        //echo 'Query MAX: '.$query_id.'<br>';
452                                        if (!$this->db->query($query_id, __LINE__, __FILE__) or !$this->db->next_record())
453                                        {
454                                                exit('Error in '.__FILE__.' line: '.__LINE__);
455                                        }
456                                       
457                                        $this->id = $this->db->f('new_id')+1;
458                                        $this->manage_fields($this->main_fields[$main_pkey], 'sync');
459                                        //echo 'ID: '.$this->id.' Type: '.gettype($this->id);
460                               
461                                        foreach($table_info['fields'] as $field_info)
462                                        {
463                                                if ($field_info['state'] === 'changed')
464                                                {
465                                                        $main_table_changed = true;
466                                                        $query_main_fields .= $field_info['name'].',';
467                                                       
468                                                        if ($field_info['name'] === 'photo')
469                                                        {
470                                                                $query_main_values .= $this->db->quote($field_info['value'], 'blob').',';
471                                                        }
472                                                        else
473                                                        {
474                                                                $query_main_values .= ($field_info['value'] === null || $field_info['value'] == '') ? 'NULL,' : "'".$field_info['value']."',";
475                                                        }
476                                                        $this->manage_fields($this->main_fields[$field_info['name']], 'sync');
477                                                }
478                                        }
479                                       
480                                        if (!$main_table_changed)
481                                        {
482                                                continue;
483                                        }
484                                       
485                                        $query_main_fields .= $main_pkey.')';
486                                        $query_main_values .= '\''.$this->id.'\')';
487
488                                        $query = $query_main_head . $query_main_fields . $query_main_values;
489                                       
490                                        //echo '<p>Main Insert Query: "'.$query.'"</p>';
491
492                                        if (!$this->db->query($query, __LINE__, __FILE__))
493                                        {
494                                                return false;
495                                        }
496                                }
497                                else if ($table_info['type'] === 'multi')
498                                {
499                                        $this->commit_multi($table, $table_info);
500                                }
501                        }
502                       
503                        $this->state = 'sync';
504                        return $this->id;
505                }
506
507                /*!
508               
509                        @function commit_multi
510                        @abstract Handles the commit of Multi Values tables
511                        @author Raphael Derosso Pereira
512                       
513                        @param string $table The table name
514                        @param array $table_info The table infrastructure
515               
516                */
517                function commit_multi($table, $table_info)
518                {
519                        reset($table_info['fields']);
520                        list(,$t_field) = each($table_info['fields']);
521                        $n_values = count($t_field['values']);
522                       
523                        //echo 'Table: '.$table.'<br>n_values => '.$n_values.'<br>';
524                       
525                        reset($table_info['fields']);
526                        for ($i = 0; $i < $n_values; ++$i)
527                        {
528                                $multi_table_changed = false;
529                                $query_multi_update_head = 'UPDATE '.$table.' SET ';
530                                $query_multi_insert_head = 'INSERT INTO '.$table;
531                                $query_multi_update_fields = false;
532                                $query_multi_insert_fields = '(';
533                                $query_multi_insert_values = ' VALUES (';
534                                $query_multi_update_tail = ' WHERE ';
535                                $main_pkeys = $table_info['keys']['primary'];
536                               
537                                foreach($table_info['fields'] as $field_info)
538                                {
539                                        $f_value = $field_info['values'][$i] == null ? 'NULL' : $field_info['values'][$i];
540                                       
541                                        switch ($field_info['states'][$i])
542                                        {
543                                                case 'changed':
544                                                        $multi_table_changed = 'changed';
545                                                        $query_multi_update_fields .= $field_info['name'].'='.$f_value.',';
546                                                        $this->manage_fields($this->db_tables[$table]['fields'][$field_info['name']], 'sync', $i);
547                                                        break;
548                                       
549                                                case 'new':
550                                                        $multi_table_changed = 'new';
551                                                        $query_multi_insert_fields .= $field_info['name'].',';
552                                                        $query_multi_insert_values .= $f_value.',';
553                                                        $this->manage_fields($this->db_tables[$table]['fields'][$field_info['name']], 'sync', $i);
554                                                        break;
555
556                                                case 'deleted':
557                                                        $multi_table_changed = 'deleted';
558                                                        //unset($this->db_tables[$table]['fields'][$field_info['name']]['values'][$i]);
559                                                        //unset($this->db_tables[$table]['fields'][$field_info['name']]['states'][$i]);
560                                                        break;
561                                        }
562                                }
563
564                                if (!$multi_table_changed)
565                                {
566                                        continue;
567                                }
568                               
569                                $query_multi_update_fields{strlen($query_multi_update_fields)-1} = ' ';
570
571                                foreach($main_pkeys as $pkey)
572                                {
573                                        $query_multi_update_tail_t[] = $pkey['name'].'=\''.$pkey['values'][$i].'\'';
574                                }
575                                $query_multi_update_tail .= @implode(' AND ', $query_multi_update_tail_t);
576                                unset($query_multi_update_tail_t);
577                               
578                                $query_multi_insert_fields{strlen($query_multi_insert_fields)-1} = ')';
579                                $query_multi_insert_values{strlen($query_multi_insert_values)-1} = ')';
580
581                                switch ($multi_table_changed)
582                                {
583                                        case 'changed':
584                                                $query = $query_multi_update_head . $query_multi_update_fields . $query_multi_update_tail;
585                                                break;
586                                               
587                                        case 'new':
588                                                $query = $query_multi_insert_head . $query_multi_insert_fields . $query_multi_insert_values;
589                                                break;
590
591                                        case 'deleted':
592                                                $query = 'DELETE FROM ' . $table . $query_multi_update_tail;
593                                }
594
595//                              echo '<p>Multi Query, type '.$multi_table_changed.': "'.$query.'"</p>';
596
597                                if (!$this->db->query($query, __LINE__, __FILE__))
598                                {
599                                        return false;
600                                }
601                        }
602/*
603                        foreach($this->db_tables[$table]['fields'] as $field_info)
604                        {
605                                $this->db_tables[$table]['fields'][$field_info['name']]['values'] = array_values($field_info['values']);
606                                $this->db_tables[$table]['fields'][$field_info['name']]['states'] = array_values($field_info['states']);
607                        }
608*/
609                        return true;
610                }
611       
612                /*!
613               
614                        @function get_id
615                        @abstract Return the object ID
616                        @author Raphael Derosso Pereira
617                       
618                */
619                function get_id (  )
620                {
621                        return $this->id;
622                }
623       
624                /*!
625               
626                        @function get_db_tables
627                        @abstract Return the object DB Tables
628                        @author Raphael Derosso Pereira
629                       
630                */
631                function get_db_tables (  )
632                {
633                        return $this->db_tables;
634                }
635
636                /**
637                 * Misc Methods
638                 */
639                 
640                /*!
641               
642                        @function get_class_by_field
643                        @abstract Returns the name of the class that has the specified
644                                field
645                        @author Raphael Derosso Pereira
646                        @param string $field The field name
647                                       
648                */
649                function get_class_by_field ($field)
650                {
651                        $class_name = '_UNDEF_';
652                       
653                        switch ($field)
654                        {
655                                case 'id_status':
656                                case 'status_name':
657                                        $class_name = 'so_status';
658                                        break;                                         
659
660                                case 'id_prefix':
661                                case 'prefix':
662                                        $class_name = 'so_prefix';
663                                        break;                                         
664
665                                case 'id_suffix':
666                                case 'suffix':
667                                        $class_name = 'so_suffix';
668                                        break;                                         
669
670                                case 'id_contact':
671                                case 'id_owner':
672                                case 'photo':
673                                case 'alias':
674                                case 'given_names':
675                                case 'family_names':
676                                case 'names_ordered':
677                                case 'birthdate':
678                                case 'sex':
679                                case 'pgp_key':
680                                case 'notes':
681                                case 'id_related':
682                                case 'is_global':
683                                case 'title':
684                                case 'department':
685                                        $class_name = 'so_contact';
686                                        break;                                         
687                               
688                                case 'id_company':
689                                case 'id_company_owner':
690                                case 'company_name':
691                                case 'company_notes':
692                                        $class_name = 'so_company';
693                                        break;                         
694                                       
695                                case 'legal_info_name':
696                                case 'legal_info_value':
697                                        $class_name = 'so_company_legal';
698                                        break;                                         
699
700                                case 'id_typeof_company_legal':
701                                case 'company_legal_type_name':
702                                        $class_name = 'so_company_legal_type';
703                                        break;                                         
704                                       
705                                case 'id_typeof_contact_relation':
706                                case 'contact_relation_name':
707                                case 'contact_relation_is_subordinated':
708                                        $class_name = 'so_contact_relation_type';
709                                        break;                                         
710                                       
711                                case 'id_typeof_company_relation':
712                                case 'company_relation_name':
713                                case 'company_relation_is_subordinated':
714                                        $class_name = 'so_company_relation_type';
715                                        break;                                         
716                                       
717                                case 'id_address':
718                                case 'address1':
719                                case 'address2':
720                                case 'complement':
721                                case 'address_other':
722                                case 'postal_code':
723                                case 'po_box':
724                                case 'address_is_default':
725                                        $class_name = 'so_address';
726                                        break;
727
728                                case 'id_city':
729                                case 'city_name':
730                                case 'city_timezone':
731                                case 'city_geographic_location':
732                                        $class_name = 'so_city';
733                                        break;
734
735                                case 'id_state':
736                                case 'state_name':
737                                case 'state_symbol':
738                                        $class_name = 'so_state';
739                                        break;
740                               
741                                case 'country_name':
742                                        $class_name = 'so_country';
743                                        break;
744                                       
745                                case 'id_typeof_contact_address':
746                                case 'contact_address_type_name':
747                                        $class_name = 'so_contact_address_type';
748                                        break;
749                                       
750                                case 'id_typeof_company_address':
751                                case 'company_address_type_name':
752                                        $class_name = 'so_company_address_type';                                               
753                                       
754                                case 'id_connection':
755                                case 'connection_name':
756                                case 'connection_value':
757                                case 'connection_is_default':
758                                        $class_name = 'so_connection';
759                                        break;                                         
760                                       
761                                case 'id_typeof_contact_connection':
762                                case 'contact_connection_type_name':
763                                        $class_name = 'so_contact_connection_type';
764                                        break;
765                                       
766                                case 'id_typeof_company_connection':
767                                case 'company_connection_type_name':
768                                        $class_name = 'so_company_connection_type';
769                                        break;
770
771                                default:
772                                        break;                                         
773
774                        }
775                       
776                        if ($class_name == '_UNDEF_')
777                        {
778                                return false;
779                        }
780                       
781                        return 'contactcenter.'.$class_name;
782                }
783               
784                /*!
785                       
786                        @function get_table_by_field
787                        @abstract Returns the table wich holds the specified field
788                        @author Raphael Derosso Pereira
789                       
790                        @param string $field The field to be found
791                        @param array $tables The array returned by get_db_tables
792                */
793                function get_table_by_field ($field, & $tables)
794                {
795                        $return = false;
796                       
797                        reset($tables);
798                        while(list($table, $properties) = each($tables))
799                        {
800                                if(array_key_exists($properties['fields']))
801                                {
802                                        if (strtolower($properties['type']) === 'main')
803                                        {
804                                                return $table;
805                                        }
806                                        array_push($return,$table);
807                                }
808                        }
809                       
810                        return $return;
811                }
812
813                /*!
814               
815                        @function manage_fields
816                        @abstract Change the state of the fields
817                        @author Raphael Derosso Pereira
818                       
819                        @param mixed $field The reference to the field
820                        @param string $new_state The new field state
821                        @param mixed $position [optional] The position where the field
822                                is state is to be stored. This is used when the table type
823                                is 'multi'.
824               
825                */
826                function manage_fields(& $field, $new_state, $position = false)
827                {
828                        if ($this->state !== 'delete')
829                        {
830                                if ($this->state != 'new')
831                                {
832                                        $this->state = 'changed';
833                                }
834                               
835                                if ($position === false)
836                                {
837                                        $field['state'] = $new_state;
838                                       
839                                        return;
840                                }
841                               
842                                if ($position !== 'new')
843                                {
844                                        $field['states'][$position] = $new_state;
845                                }
846                                else
847                                {
848                                        array_push($field['states'], $new_state);
849                                }
850                        }
851                }
852               
853                /*!
854                       
855                        @function reset_values
856                        @abstract Reset all the values to false and all
857                                states to empty
858                        @author Raphael Derosso Pereira
859                */
860                function reset_values()
861                {
862                        foreach($this->db_tables as $table_name => $table_info)
863                        {
864                                foreach($table_info['fields'] as $field => $field_info)
865                                {
866                                        if ($field_info['value'])
867                                        {
868                                                $this->db_tables[$table_name]['fields'][$field]['value'] = false;
869                                                $this->manage_fields($this->db_tables[$table_name]['fields'][$field], 'empty');
870                                        }
871                                        else if ($field_info['values'])
872                                        {
873                                                unset($this->db_tables[$table_name]['fields'][$field]['states']);
874                                                unset($this->db_tables[$table_name]['fields'][$field]['values']);
875                                        }
876                                }
877                        }
878                        $this->state = 'new';
879                }
880
881
882                /*!
883               
884                        @function remove
885                        @abstract Removes the entry from the DB
886                        @author Raphael Derosso Pereira
887                */
888                function remove()
889                {
890                        $this->state = 'delete';
891                       
892                        $result = $this->commit();
893                        $this->reset_values();
894                        return $result;
895                }
896       
897        }
898?>
Note: See TracBrowser for help on using the repository browser.