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