M_DataMapper

The NextGEN Gallery M DataMapper class.

Defined (1)

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

/products/photocrati_nextgen/modules/datamapper/module.datamapper.php  
  1. class M_DataMapper extends C_Base_Module 
  2. function define() 
  3. parent::define( 
  4. 'photocrati-datamapper',  
  5. 'DataMapper',  
  6. 'Provides a database abstraction layer following the DataMapper pattern',  
  7. '0.10',  
  8. 'https://www.imagely.com',  
  9. 'Photocrati Media',  
  10. 'https://www.imagely.com' 
  11. ); 
  12.  
  13. C_Photocrati_Installer::add_handler($this->module_id, 'C_Datamapper_Installer'); 
  14.  
  15. function _register_adapters() 
  16. $this->get_registry()->add_adapter('I_Component_Factory', 'A_DataMapper_Factory'); 
  17.  
  18.  
  19. function _register_hooks() 
  20. add_filter('posts_request', array(&$this, 'set_custom_wp_query'), 50, 2); 
  21. add_filter('posts_fields', array(&$this, 'set_custom_wp_query_fields'), 50, 2); 
  22. add_filter('posts_where', array(&$this, 'set_custom_wp_query_where'), 50, 2); 
  23. add_filter('posts_groupby', array(&$this, 'set_custom_wp_query_groupby'), 50, 2); 
  24.  
  25.  
  26. /** 
  27. * Sets a custom SQL query for the WP_Query class, when the Custom Post 
  28. * DataMapper implementation is used 
  29. * @param string $sql 
  30. * @param WP_Query $wp_query 
  31. * @return string 
  32. */ 
  33. function set_custom_wp_query($sql, &$wp_query) 
  34. if ($wp_query->get('datamapper')) { 
  35.  
  36. // Set the custom query 
  37. if (($custom_sql = $wp_query->get('custom_sql'))) { 
  38. $sql = $custom_sql; 
  39.  
  40. // Perhaps we're to initiate a delete query instead? 
  41. elseif ($wp_query->get('is_delete')) { 
  42. $sql = preg_replace("/^SELECT.*FROM/i", "DELETE FROM", $sql); 
  43.  
  44. if ($wp_query->get('debug')) var_dump($sql); 
  45.  
  46. return $sql; 
  47.  
  48. /** 
  49. * Sets custom fields to select from the database 
  50. * @param string $fields 
  51. * @param WP_Query $wp_query 
  52. * @return string 
  53. */ 
  54. function set_custom_wp_query_fields($fields, &$wp_query) 
  55. if ($wp_query->get('datamapper')) { 
  56. if (($custom_fields = $wp_query->get('fields')) && $custom_fields != 'ids') { 
  57. $fields = $custom_fields; 
  58.  
  59. return $fields; 
  60.  
  61.  
  62. /** 
  63. * Sets custom where clauses for a query 
  64. * @param string $where 
  65. * @param WP_Query $wp_query 
  66. * @return string 
  67. */ 
  68. function set_custom_wp_query_where($where, &$wp_query) 
  69. if ($wp_query->get('datamapper')) { 
  70. $this->add_post_title_where_clauses($where, $wp_query); 
  71. $this->add_post_name_where_clauses($where, $wp_query); 
  72.  
  73. return $where; 
  74.  
  75.  
  76. /** 
  77. * Adds additional group by clauses to the SQL query 
  78. * @param string $groupby 
  79. * @param WP_Query $wp_query 
  80. * @return string 
  81. */ 
  82. function set_custom_wp_query_groupby($groupby, &$wp_query) 
  83. $retval = $groupby; 
  84. $group_by_columns = $wp_query->get('group_by_columns'); 
  85. if ($group_by_columns) { 
  86. $retval = str_replace('GROUP BY', '', $retval); 
  87. $columns = explode(', ', $retval); 
  88. foreach (array_reverse($columns) as $column) { 
  89. array_unshift($group_by_columns, trim($column)); 
  90. $retval = "GROUP BY ".implode(', ', $group_by_columns); 
  91. // Not all mysql servers allow access to create temporary tables which are used when doing GROUP BY 
  92. // statements; this can potentially ruin basic queries. If no group_by_columns is set AND the query originates 
  93. // within the datamapper we strip the "GROUP BY" clause entirely in this filter. 
  94. else if ($wp_query->get('datamapper')) { 
  95. $retval = ''; 
  96. return $retval; 
  97.  
  98.  
  99. /** 
  100. * Formats the value of used in a WHERE IN 
  101. * SQL clause for use in the WP_Query where clause 
  102. * @param string|array $values 
  103. * @return string 
  104. */ 
  105. function format_where_in_value($values) 
  106. if (is_string($values) && strpos($values, ', ') !== FALSE) 
  107. $values = explode(", ", $values); 
  108. elseif (!is_array($values)) 
  109. $values = array($values); 
  110.  
  111. // Quote the titles 
  112. foreach ($values as $index => $value) { 
  113. $values[$index] = "'{$value}'"; 
  114.  
  115. return implode(', ', $values); 
  116.  
  117.  
  118. /** 
  119. * Adds post_title to the where clause 
  120. * @param string $where 
  121. * @param WP_Query $wp_query 
  122. * @return string 
  123. */ 
  124. function add_post_title_where_clauses(&$where, &$wp_query) 
  125. global $wpdb; 
  126.  
  127. // Handle post_title query var 
  128. if (($titles = $wp_query->get('post_title'))) { 
  129. $titles = $this->format_where_in_value($titles); 
  130. $where .= " AND {$wpdb->posts}.post_title IN ({$titles})"; 
  131.  
  132. // Handle post_title_like query var 
  133. elseif (($value = $wp_query->get('post_title__like'))) { 
  134. $where .= " AND {$wpdb->posts}.post_title LIKE '{$value}'"; 
  135.  
  136.  
  137. /** 
  138. * Adds post_name to the where clause 
  139. * @param type $where 
  140. * @param type $wp_query 
  141. */ 
  142. function add_post_name_where_clauses(&$where, &$wp_query) 
  143. global $wpdb; 
  144.  
  145. if (($name = $wp_query->get('page_name__like'))) { 
  146. $where .= " AND {$wpdb->posts}.post_name LIKE '{$name}'"; 
  147. elseif (($names = $wp_query->get('page_name__in'))) { 
  148. $names = $this->format_where_in_value($names); 
  149. $where .= " AND {$wpdb->posts}.post_name IN ({$names})"; 
  150.  
  151. /** 
  152. * Unserializes data using our proprietary format 
  153. * @param string $value 
  154. * @return mixed 
  155. */ 
  156. static function unserialize($value) 
  157. $retval = NULL; 
  158. if (is_string($value)) 
  159. $retval = stripcslashes($value); 
  160.  
  161. if (strlen($value) > 1) 
  162. // We can't always rely on base64_decode() or json_decode() to return FALSE as their documentation 
  163. // claims so check if $retval begins with a: as that indicates we have a serialized PHP object. 
  164. if (strpos($retval, 'a:') === 0) 
  165. $er = error_reporting(0); 
  166. $retval = unserialize($value); 
  167. error_reporting($er); 
  168. else { 
  169. // We use json_decode() here because PHP's unserialize() is not Unicode safe. 
  170. $retval = json_decode(base64_decode($retval), TRUE); 
  171.  
  172. return $retval; 
  173.  
  174. /** 
  175. * Serializes the data 
  176. * @param mixed $value 
  177. * @return string 
  178. */ 
  179. static function serialize($value) 
  180. //Using json_encode here because PHP's serialize is not Unicode safe 
  181. return base64_encode(json_encode($value)); 
  182.  
  183. function get_type_list() 
  184. return array( 
  185. 'A_Datamapper_Factory' => 'adapter.datamapper_factory.php',  
  186. 'C_Datamapper_Installer' => 'class.datamapper_installer.php',  
  187. 'C_Datamapper' => 'class.datamapper.php',  
  188. 'C_Custompost_Datamapper_Driver' => 'class.custompost_datamapper_driver.php',  
  189. 'C_Customtable_Datamapper_Driver' => 'class.customtable_datamapper_driver.php',  
  190. 'C_Datamapper_Driver_Base' => 'class.datamapper_driver_base.php',  
  191. 'C_Datamapper_Model' => 'class.datamapper_model.php',  
  192. 'M_Datamapper' => 'module.datamapper.php' 
  193. );