1: <?php
 2: /**
 3:  * Object synchronization handler class.
 4:  *
 5:  * You may not change or alter any portion of this comment or credits
 6:  * of supporting developers from this source code or any supporting source code
 7:  * which is considered copyrighted (c) material of the original comment or credit authors.
 8:  * This program is distributed in the hope that it will be useful,
 9:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11:  *
12:  * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13:  * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14:  * @package             kernel
15:  * @subpackage          model
16:  * @since               2.3.0
17:  * @author              Taiwen Jiang <phppp@users.sourceforge.net>
18:  */
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20: 
21: /**
22:  * Object synchronization handler class.
23:  *
24:  * @author Taiwen Jiang <phppp@users.sourceforge.net>
25:  *
26:  * {@link XoopsModelAbstract}
27:  */
28: 
29: /**
30:  * Usage of methods provided by XoopsModelSync:
31:  *
32:  * Step #1: set linked table and adjoint fields through XoopsPersistableObjectHandler:
33:  *                  $handler->table_link = $handler->db->prefix("the_linked_table"); // full name of the linked table that is used for the query
34:  *                  $handler->field_link = "the_linked_field"; // name of field in linked table that will be used to link the linked table with current table
35:  *                  $handler->field_object = "the_object_field"; // name of field in current table that will be used to link the linked table with current table; linked field name will be used if the field name is not set
36:  * Step #2: perform query
37:  */
38: class XoopsModelSync extends XoopsModelAbstract
39: {
40:     /**
41:      * Clean orphan objects against linked objects
42:      *
43:      * @param  string $table_link   table of linked object for JOIN; deprecated, for backward compatibility
44:      * @param  string $field_link   field of linked object for JOIN; deprecated, for backward compatibility
45:      * @param  string $field_object field of current object for JOIN; deprecated, for backward compatibility
46:      * @return bool   true on success
47:      */
48:     public function cleanOrphan($table_link = '', $field_link = '', $field_object = '')
49:     {
50:         if (!empty($table_link)) {
51:             $this->handler->table_link = $table_link;
52:         }
53:         if (!empty($field_link)) {
54:             $this->handler->field_link = $field_link;
55:         }
56:         if (!empty($field_object)) {
57:             $this->handler->field_object = $field_object;
58:         }
59: 
60:         if (empty($this->handler->field_object) || empty($this->handler->table_link) || empty($this->handler->field_link)) {
61:             trigger_error("The link information is not set for '" . get_class($this->handler) . "' yet.", E_USER_WARNING);
62: 
63:             return null;
64:         }
65: 
66:         /**
67:          * for MySQL 4.1+
68:          */
69:         if (version_compare(mysqli_get_server_info($this->handler->db->conn), '4.1.0', 'ge')) {
70:             $sql = "DELETE FROM `{$this->handler->table}`" . " WHERE (`{$this->handler->field_object}` NOT IN ( SELECT DISTINCT `{$this->handler->field_link}` FROM `{$this->handler->table_link}`) )";
71:         } else {
72:             // for 4.0+
73:             $sql = "DELETE `{$this->handler->table}` FROM `{$this->handler->table}`" . " LEFT JOIN `{$this->handler->table_link}` AS aa ON `{$this->handler->table}`.`{$this->handler->field_object}` = aa.`{$this->handler->field_link}`" . " WHERE (aa.`{$this->handler->field_link}` IS NULL)";
74:         }
75:         if (!$result = $this->handler->db->queryF($sql)) {
76:             return false;
77:         }
78: 
79:         return true;
80:     }
81: 
82:     /**
83:      * Synchronizing objects
84:      * @deprecated
85:      *
86:      * @return bool true on success
87:      */
88:     public function synchronization()
89:     {
90:         $retval = $this->cleanOrphan();
91: 
92:         return $retval;
93:     }
94: }
95: