Mixin_NextGen_Table_Extras

Class Mixin_NextGen_Table_Extras.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/nextgen_data/package.module.nextgen_data.php  
  1. class Mixin_NextGen_Table_Extras extends Mixin 
  2. const CUSTOM_POST_NAME = __CLASS__; 
  3. public function initialize() 
  4. // Each record in a NextGEN Gallery table has an associated custom post in the wp_posts table 
  5. $this->object->_custom_post_mapper = new C_CustomPost_DataMapper_Driver($this->object->get_object_name()); 
  6. $this->object->_custom_post_mapper->set_model_factory_method('extra_fields'); 
  7. /** 
  8. * Defines a column for the mapper 
  9. * @param $name 
  10. * @param $data_type 
  11. * @param null $default_value 
  12. * @param bool $extra 
  13. */ 
  14. public function define_column($name, $data_type, $default_value = NULL, $extra = FALSE) 
  15. $this->call_parent('define_column', $name, $data_type, $default_value); 
  16. if ($extra) { 
  17. $this->object->_columns[$name]['extra'] = TRUE; 
  18. } else { 
  19. $this->object->_columns[$name]['extra'] = FALSE; 
  20. /** 
  21. * Gets a list of all the extra columns defined for this table 
  22. * @return array 
  23. */ 
  24. public function get_extra_columns() 
  25. $retval = array(); 
  26. foreach ($this->object->_columns as $key => $properties) { 
  27. if ($properties['extra']) { 
  28. $retval[] = $key; 
  29. return $retval; 
  30. /** 
  31. * Adds a column to the database 
  32. * @param $column_name 
  33. * @param $datatype 
  34. * @param null $default_value 
  35. */ 
  36. public function _add_column($column_name, $datatype, $default_value = NULL) 
  37. $skip = FALSE; 
  38. if (isset($this->object->_columns[$column_name]) and $this->object->_columns[$column_name]['extra']) { 
  39. $skip = TRUE; 
  40. if (!$skip) { 
  41. $this->call_parent('_add_column', $column_name, $datatype, $default_value); 
  42. return !$skip; 
  43. public function create_custom_post_entity($entity) 
  44. $custom_post_entity = new stdClass(); 
  45. // If the custom post entity already exists then it needs 
  46. // an ID 
  47. if (isset($entity->extras_post_id)) { 
  48. $custom_post_entity->ID = $entity->extras_post_id; 
  49. // If a property isn't a column for the table, then 
  50. // it belongs to the custom post record 
  51. foreach (get_object_vars($entity) as $key => $value) { 
  52. if (!$this->object->has_column($key)) { 
  53. unset($entity->{$key}); 
  54. if ($this->object->has_defined_column($key) && $key != $this->object->get_primary_key_column()) { 
  55. $custom_post_entity->{$key} = $value; 
  56. // Used to help find these type of records 
  57. $custom_post_entity->post_name = self::CUSTOM_POST_NAME; 
  58. return $custom_post_entity; 
  59. /** 
  60. * Creates a new record in the custom table, as well as a custom post record 
  61. * @param $entity 
  62. */ 
  63. public function _create($entity) 
  64. $retval = FALSE; 
  65. $custom_post_entity = $this->create_custom_post_entity($entity); 
  66. // Try persisting the custom post type record first 
  67. if ($custom_post_id = $this->object->_custom_post_mapper->save($custom_post_entity)) { 
  68. // Try saving the custom table record. If that fails, then destroy the previously 
  69. // created custom post type record 
  70. if (!($retval = $this->call_parent('_create', $entity))) { 
  71. $this->object->_custom_post_mapper->destroy($custom_post_id); 
  72. } else { 
  73. $entity->extras_post_id = $custom_post_id; 
  74. return $retval; 
  75. // Updates a custom table record and it's associated custom post type record in the database 
  76. public function _update($entity) 
  77. $retval = FALSE; 
  78. $custom_post_entity = $this->create_custom_post_entity($entity); 
  79. $custom_post_id = $this->object->_custom_post_mapper->save($custom_post_entity); 
  80. $entity->extras_post_id = $custom_post_id; 
  81. $retval = $this->call_parent('_update', $entity); 
  82. foreach ($this->get_extra_columns() as $key) { 
  83. if (isset($custom_post_entity->{$key})) { 
  84. $entity->{$key} = $custom_post_entity->{$key}; 
  85. return $retval; 
  86. public function destroy($entity) 
  87. if (isset($entity->extras_post_id)) { 
  88. wp_delete_post($entity->extras_post_id, TRUE); 
  89. return $this->call_parent('destroy', $entity); 
  90. public function _regex_replace($in) 
  91. global $wpdb; 
  92. $from = 'FROM `' . $this->object->get_table_name() . '`'; 
  93. $out = str_replace('FROM', ', GROUP_CONCAT(CONCAT_WS(\'@@\', meta_key, meta_value)) AS \'extras\' FROM', $in); 
  94. $out = str_replace($from, "{$from} LEFT OUTER JOIN `{$wpdb->postmeta}` ON `{$wpdb->postmeta}`.`post_id` = `extras_post_id` ", $out); 
  95. return $out; 
  96. /** 
  97. * Gets the generated query 
  98. */ 
  99. public function get_generated_query() 
  100. // Add extras column 
  101. if ($this->object->is_select_statement() && stripos($this->object->_select_clause, 'count(') === FALSE) { 
  102. $table_name = $this->object->get_table_name(); 
  103. $primary_key = "{$table_name}.{$this->object->get_primary_key_column()}"; 
  104. if (stripos($this->object->_select_clause, 'DISTINCT') === FALSE) { 
  105. $this->object->_select_clause = str_replace('SELECT', 'SELECT DISTINCT', $this->object->_select_clause); 
  106. $this->object->group_by($primary_key); 
  107. $sql = $this->call_parent('get_generated_query'); 
  108. // Sections may be omitted by wrapping them in mysql/C style comments 
  109. if (stripos($sql, '/*NGG_NO_EXTRAS_TABLE*/') !== FALSE) { 
  110. $parts = explode('/*NGG_NO_EXTRAS_TABLE*/', $sql); 
  111. foreach ($parts as $ndx => $row) { 
  112. if ($ndx % 2 != 0) { 
  113. continue; 
  114. $parts[$ndx] = $this->_regex_replace($row); 
  115. $sql = implode('', $parts); 
  116. } else { 
  117. $sql = $this->_regex_replace($sql); 
  118. } else { 
  119. $sql = $this->call_parent('get_generated_query'); 
  120. return $sql; 
  121. public function _convert_to_entity($entity) 
  122. // Add extra columns to entity 
  123. if (isset($entity->extras)) { 
  124. $extras = $entity->extras; 
  125. unset($entity->extras); 
  126. foreach (explode(', ', $extras) as $extra) { 
  127. if ($extra) { 
  128. list($key, $value) = explode('@@', $extra); 
  129. if ($this->object->has_defined_column($key) && !isset($entity->key)) { 
  130. $entity->{$key} = $value; 
  131. // Cast custom_post_id as integer 
  132. if (isset($entity->extras_post_id)) { 
  133. $entity->extras_post_id = intval($entity->extras_post_id); 
  134. } else { 
  135. $entity->extras_post_id = 0; 
  136. $retval = $this->call_parent('_convert_to_entity', $entity); 
  137. return $entity;