tripal_views_query.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. class tripal_views_query extends views_plugin_query {
  3. /**
  4. * The EntityFieldQuery object used to perform the query
  5. */
  6. var $query;
  7. /**
  8. * The EntityFieldQuery object used to perform the count query.
  9. * It will not include the order by and only fields that are used in
  10. * filters.
  11. */
  12. var $cquery;
  13. /**
  14. * The fields that are to be included in the query.
  15. */
  16. var $fields;
  17. /**
  18. * The filters that are to be included in the query.
  19. */
  20. var $filters;
  21. /**
  22. * The sort item that are to be included in the query.
  23. */
  24. var $order;
  25. /**
  26. * Ensure a table exists in the queue.
  27. *
  28. * This function overrides the views_plugin_query version of the function
  29. * but does nothing other than return the "table" (or bundle) name as
  30. * we won't be using aliases for bundles.
  31. *
  32. * @param $table
  33. * The unaliased name of the table to ensure.
  34. * @param $relationship
  35. * The relationship to ensure the table links to. Each relationship will
  36. * get a unique instance of the table being added. If not specified, will
  37. * be the primary table.
  38. * @param $join
  39. * A views_join object (or derived object) to join the alias in.
  40. *
  41. * @return
  42. * The alias used to refer to this specific table, or NULL if the table
  43. * cannot be ensured.
  44. */
  45. public function ensure_table($table, $relationship = NULL, $join = NULL) {
  46. // Because we are not querying a table, we're querying a TripalFieldQuery
  47. // object we don't need to ensure the table.
  48. return $table;
  49. }
  50. /**
  51. *
  52. */
  53. public function init($base_table = 'tripal_entity', $base_field = 'id', $options) {;
  54. parent::init($base_table, $base_field, $options);
  55. $this->fields = array();
  56. $this->where = array();
  57. $this->order = array();
  58. // Creqte the TripalFieldQuery object.
  59. $this->query = new TripalFieldQuery();
  60. $this->cquery = new TripalFieldQuery();
  61. $this->cquery->count();
  62. // Convert the $base_table into the bundle table. Because every
  63. // tripal site will have different bundle tables we have to do the
  64. // conversion for cross-site compatibility.
  65. list($vocabulary, $accession) = explode('__', $base_table);
  66. $term = tripal_load_term_entity(array('vocabulary' => $vocabulary, 'accession' => $accession));
  67. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  68. // Make sure we only query on the entities for this bundle type.
  69. $this->query->entityCondition('entity_type', 'TripalEntity');
  70. $this->query->entityCondition('bundle', $bundle->name);
  71. $this->cquery->entityCondition('entity_type', 'TripalEntity');
  72. $this->cquery->entityCondition('bundle', $bundle->name);
  73. }
  74. /**
  75. *
  76. */
  77. public function add_field($table_alias, $field_name, $alias = '', $params = array()) {
  78. $this->fields[] = array(
  79. 'table_alias' => $table_alias,
  80. 'field_name' => $field_name,
  81. 'alias' => $alias,
  82. 'params' => $params
  83. );
  84. }
  85. /**
  86. * Add a simple WHERE clause to the query.
  87. *
  88. * @param $group
  89. * The WHERE group to add these to; groups are used to create AND/OR
  90. * sections. Groups cannot be nested. Use 0 as the default group. If the
  91. * group does not yet exist it will be created as an AND group.
  92. * @param $field
  93. * The name of the field to check.
  94. * @param $value
  95. * The value to test the field against. In most cases, this is a scalar.
  96. * For more complex options, it is an array. The meaning of each element
  97. * in the array is dependent on the $operator.
  98. * @param $operator
  99. * The comparison operator, such as =, <, or >=. It also accepts more
  100. * complex options such as IN, LIKE, or BETWEEN. Defaults to IN if $value
  101. * is an array = otherwise. If $field is a string you have to use 'formula'
  102. * here.
  103. */
  104. public function add_where($group, $field_name, $value = NULL, $operator = NULL) {
  105. $this->filters[] = array(
  106. 'group' => $group,
  107. 'field_name' => $field_name,
  108. 'value' => $value,
  109. 'op' => $operator
  110. );
  111. if ($value) {
  112. // Handle the bundle properties separate from real fields.
  113. if ($field_name == 'entity_id' or $field_name == 'status') {
  114. $this->query->propertyCondition($field_name, $value, $operator);
  115. $this->cquery->propertyCondition($field_name, $value, $operator);
  116. return;
  117. }
  118. // If the field_name comes to us with a period in it then it means that
  119. // we need to separate the field name from sub-element names.
  120. $matches = array();
  121. if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
  122. $field_name = $matches[1];
  123. $element_name = $matches[2];
  124. if (tripal_load_include_field_class($field_name)) {
  125. $field = field_info_field($field_name);
  126. $instance = field_info_instance('TripalEntity', $field_name, $this->query->entityConditions['bundle']['value']);
  127. $field_obj = new $field_name($field, $instance);
  128. $element_name = $field_obj->getFieldTermID() . ',' . $element_name;
  129. // Replace periods with commas.
  130. $element_name = preg_replace('/\./', ',', $element_name);
  131. $this->query->fieldCondition($field_name, $element_name, $value, $operator);
  132. $this->cquery->fieldCondition($field_name, $element_name, $value, $operator);
  133. }
  134. else {
  135. throw new Exception("Unkown element id: '$element_name'.");
  136. }
  137. }
  138. else {
  139. $instance = field_info_instance('TripalEntity', $field_name, $this->query->entityConditions['bundle']['value']);
  140. $field_term = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
  141. $this->query->fieldCondition($field_name, $field_term, $value, $operator);
  142. $this->cquery->fieldCondition($field_name, $field_term, $value, $operator);
  143. }
  144. }
  145. }
  146. /**
  147. * Overrides add_orderby().
  148. */
  149. public function add_orderby($table, $field_name = NULL, $order = 'ASC', $alias = '', $params = array()) {
  150. if ($field_name) {
  151. // Make sure we don't put the orderby in more than once.
  152. foreach ($this->order as $index => $order_details) {
  153. if ($order_details['field'] == $field_name) {
  154. return;
  155. }
  156. }
  157. $this->order[] = array(
  158. 'field' => $field_name,
  159. 'direction' => strtoupper($order)
  160. );
  161. // If the field_name comes to us with a period in it then it means that
  162. // we need to separate the field name from sub-element names.
  163. $matches = array();
  164. if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
  165. $field_name = $matches[1];
  166. $element_name = $matches[2];
  167. $field = field_info_field($field_name);
  168. $instance = field_info_instance('TripalEntity', $field_name, $this->query->entityConditions['bundle']['value']);
  169. $element_name = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'] . ',' . $element_name;
  170. $this->query->fieldOrderBy($field_name, $element_name, $order);
  171. }
  172. else {
  173. $instance = field_info_instance('TripalEntity', $field_name, $this->query->entityConditions['bundle']['value']);
  174. $field_term = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
  175. $this->query->fieldOrderBy($field_name, $field_term, $order);
  176. }
  177. }
  178. }
  179. /**
  180. * Overrides build().
  181. */
  182. function build(&$view) {
  183. // Make the query distinct if the option was set.
  184. if (!empty($this->options['distinct'])) {
  185. $this->set_distinct(TRUE, !empty($this->options['pure_distinct']));
  186. }
  187. // Store the view in the object to be able to use it later.
  188. $this->view = $view;
  189. $view->init_pager();
  190. // Let the pager modify the query to add limits.
  191. $this->pager->query();
  192. $view->build_info['query'] = $this->query;
  193. $view->build_info['count_query'] = $this->cquery;
  194. }
  195. /**
  196. *
  197. * @param $view
  198. */
  199. function execute(&$view) {
  200. $query = $view->build_info['query'];
  201. $cquery = $view->build_info['count_query'];
  202. if ($query) {
  203. $start = microtime(TRUE);
  204. try {
  205. if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
  206. // TODO: The code below was taken from the
  207. // views_plugin_pager::execute_count_query($count_query) which would
  208. // be called here, but that function expects the query is a
  209. // database query rather than a TripalEntityField query. We
  210. // really should create a new tripal_views_plugin_pager class
  211. // and call the corresponding function here, but due to time
  212. // constraints this is the shortcut.
  213. $total_items = $cquery->execute();
  214. $this->pager->total_items = $total_items;
  215. if (!empty($this->pager->options['offset'])) {
  216. $this->pager->total_items -= $this->pager->options['offset'];
  217. };
  218. $this->pager->update_page_info();
  219. }
  220. // TODO: we need to implement a new views_plugin_pager class to
  221. // override the pre_execute to set the range, instead we'll just do
  222. // it manully here until we have the class.
  223. $this->pager->pre_execute($query);
  224. $num_items_per_page = $this->pager->get_items_per_page();
  225. $offset = $this->pager->get_current_page() * $num_items_per_page;
  226. $query->range($offset, $num_items_per_page);
  227. // Get the IDs
  228. $results = $query->execute();
  229. $entity_ids = array_keys($results['TripalEntity']);
  230. $this->pager->post_execute($view->result);
  231. if ($this->pager->use_count_query() || !empty($view->get_total_rows)) {
  232. $view->total_rows = $this->pager->get_total_items();
  233. }
  234. // Get the fields to attach to the entity
  235. $fields = array();
  236. $field_ids = array();
  237. foreach ($this->fields as $details) {
  238. $field_name = $details['field_name'];
  239. // If the field_name comes to us with a period in it then it means that
  240. // we need to separate the field name from sub-element names.
  241. $matches = array();
  242. if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
  243. $field_name = $matches[1];
  244. $element_name = $matches[2];
  245. }
  246. $field = field_info_field($field_name);
  247. if ($field) {
  248. $fields[$field_name] = $field;
  249. $field_ids[] = $field['id'];
  250. }
  251. }
  252. // Get the entity IDs from the query.
  253. $entities = tripal_load_entity('TripalEntity', $entity_ids, FALSE, $field_ids);
  254. $i = 0;
  255. foreach ($entities as $entity_id => $entity) {
  256. $view->result[$i] = new stdClass();
  257. foreach ($this->fields as $details) {
  258. $field_name = $details['field_name'];
  259. // The entity_id and link fields are not true fields. They are
  260. // added by the tripal_views_data_tripal_entity() function to provide
  261. // useful fields to reference entities. If we see these
  262. // we need to support them here by giving them values.
  263. if ($field_name == 'entity_id') {
  264. $view->result[$i]->$field_name = $entity;
  265. continue;
  266. }
  267. if ($field_name == 'link') {
  268. $view->result[$i]->$field_name = $entity;
  269. continue;
  270. }
  271. if ($field_name == 'edit_link') {
  272. $view->result[$i]->$field_name = $entity;
  273. continue;
  274. }
  275. if ($field_name == 'delete_link') {
  276. $view->result[$i]->$field_name = $entity;
  277. continue;
  278. }
  279. if ($field_name == 'status') {
  280. $view->result[$i]->$field_name = $entity->status;
  281. continue;
  282. }
  283. // If the field_name comes to us with a period in it then it means that
  284. // we need to separate the field name from sub-element names.
  285. $matches = array();
  286. if (preg_match('/^(.+?)\.(.*)$/', $field_name, $matches)) {
  287. $field_name = $matches[1];
  288. $element_name = $matches[2];
  289. }
  290. if (array_key_exists($field_name, $fields)) {
  291. $items = field_get_items('TripalEntity', $entity, $field_name);
  292. $view->result[$i]->$field_name = $items;
  293. }
  294. }
  295. // Always add the entity to the results so that handlers
  296. // can take advantage of it.
  297. $view->result[$i]->entity = $entity;
  298. $i++;
  299. }
  300. }
  301. catch (Exception $e) {
  302. $view->result = array();
  303. if (!empty($view->live_preview)) {
  304. drupal_set_message($e->getMessage(), 'error');
  305. }
  306. else {
  307. vpr('Exception in @human_name[@view_name]: @message', array('@human_name' => $view->human_name, '@view_name' => $view->name, '@message' => $e->getMessage()));
  308. }
  309. }
  310. }
  311. else {
  312. $start = microtime(TRUE);
  313. }
  314. $view->execute_time = microtime(TRUE) - $start;
  315. }
  316. }