PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/class/model/sync.php

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