TripalEntityCollection.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. class TripalEntityCollection {
  3. /**
  4. * The name of the bundles (i.e. content type) to which the entities belong.
  5. */
  6. protected $bundles = array();
  7. /**
  8. * The collection ID
  9. */
  10. protected $collection_id = NULL;
  11. /**
  12. * The name of this collection.
  13. */
  14. protected $collection_name = '';
  15. /**
  16. * An array of numeric entities IDs.
  17. */
  18. protected $ids = array();
  19. /**
  20. * An array of field IDs.
  21. */
  22. protected $fields = array();
  23. /**
  24. * The user object of the user that owns the collection.
  25. */
  26. protected $user = array();
  27. /**
  28. * The date that the collection was created.
  29. */
  30. protected $create_date = '';
  31. /**
  32. * The list of downloaders available for this bundle.
  33. */
  34. protected $downloaders = array();
  35. /**
  36. * The description for this collection.
  37. */
  38. protected $description = '';
  39. /**
  40. * Constructs a new instance of the TripalEntityCollection class.
  41. */
  42. public function __construct() {
  43. }
  44. /**
  45. * Deletes the current collection
  46. */
  47. public function delete() {
  48. if (!$this->collection_id) {
  49. throw new Exception('This data collection object has not yet been loaded. Cannot delete.');
  50. }
  51. try {
  52. // Delete from the tripal collection table.
  53. db_delete('tripal_collection')
  54. ->condition('collection_id', $this->collection_id)
  55. ->execute();
  56. // Delete the field groups from the tripal_bundle_collection table.
  57. db_delete('tripal_collection_bundle')
  58. ->condition('collection_id', $this->collection_id)
  59. ->execute();
  60. // Remove any files that may have been created
  61. foreach ($this->downloaders as $class_name => $label) {
  62. tripal_load_include_downloader_class($class_name);
  63. $outfile = $this->getOutfile($class_name);
  64. $downloader = new $class_name($this->bundles, $this->ids, $this->fields,
  65. $outfile, $this->getUserID());
  66. $downloader->delete();
  67. }
  68. // Reset the class to defaults.
  69. $this->collection_id = NULL;
  70. $this->collection_name = '';
  71. $this->create_date = '';
  72. $this->description = '';
  73. }
  74. catch (Exception $e) {
  75. throw new Exception('Cannot delete collection: ' . $e->getMessage());
  76. }
  77. }
  78. /**
  79. * Loads an existing collection using a collection ID.
  80. *
  81. * @param $collection_id
  82. * The ID of the collection to load.
  83. *
  84. * @throws Exception
  85. */
  86. public function load($collection_id) {
  87. // Make sure we have a numeric job_id.
  88. if (!$collection_id or !is_numeric($collection_id)) {
  89. throw new Exception("You must provide the collection_id to load the collection.");
  90. }
  91. $collection = db_select('tripal_collection', 'tc')
  92. ->fields('tc')
  93. ->condition('collection_id', $collection_id)
  94. ->execute()
  95. ->fetchObject();
  96. if (!$collection) {
  97. throw new Exception("Cannot find a collection with the ID provided.");
  98. }
  99. // Fix the date/time fields.
  100. $this->collection_name = $collection->collection_name;
  101. $this->create_date = $collection->create_date;
  102. $this->user = user_load($collection->uid);
  103. $this->description = $collection->description;
  104. $this->collection_id = $collection->collection_id;
  105. /* Add the IDs, Fields, Bundles for this collection from the
  106. * collection_bundle table.
  107. */
  108. $this->bundles = $this->getBundles();
  109. // If more than one bundle plop into associative array.
  110. $bundle_name = "";
  111. if (count($this->bundles) > 1) {
  112. foreach ($this->bundles as $bundle) {
  113. // If bundle name is not bio_data_# then it's an accession name from
  114. // a remote site, so we need to handle it differently.
  115. $bundle_name = $bundle->bundle_name;
  116. if (strpos($bundle->bundle_name, 'bio_data_') !== 0) {
  117. $ids[$bundle_name] = $this->getEntityIDs($bundle_name);
  118. $fields[$bundle_name] = $this->getFields($bundle_name);
  119. }
  120. $bundle_name = $bundle->bundle_name;
  121. $ids[$bundle_name] = $this->getEntityIDs($bundle_name);
  122. $fields[$bundle_name] = $this->getFields($bundle_name);
  123. }
  124. $this->ids = $ids;
  125. $this->fields = $fields;
  126. }
  127. else {
  128. if (!empty($this->bundles)) {
  129. $bundle_name = $this->bundles[0]->bundle_name;
  130. $this->ids = $this->getEntityIDs($bundle_name);
  131. $this->fields = $this->getFields($bundle_name);
  132. }
  133. }
  134. // Iterate through the fields and find out what download formats are
  135. // supported for this basket.
  136. $this->downloaders = $this->getDownloadFormattersList($this->fields);
  137. }
  138. /**
  139. * Creates a new unique collection ID used as a look up against the
  140. * tripal_collection_bundle to find fields, ids, and bundles.
  141. *
  142. * @param $details
  143. * An association array containing the details for a collection. The
  144. * details must include the following key/value pairs:
  145. * - uid: The ID of the user that owns the collection
  146. * - collection_name: The name of the collection
  147. * - description: A user supplied description for the collection.
  148. *
  149. * @throws Exception
  150. */
  151. public function create($details) {
  152. if (!$details['uid']) {
  153. throw new Exception("Must provide a 'uid' key to TripalEntityCollection::create().");
  154. }
  155. if (!$details['collection_name']) {
  156. throw new Exception("Must provide a 'collection_name' key to TripalEntityCollection::create().");
  157. }
  158. // Before inserting the new collection make sure we don't violote the unique
  159. // constraint that a user can only have one collection of the give name.
  160. $has_match = db_select('tripal_collection', 'tc')
  161. ->fields('tc', array('collection_id'))
  162. ->condition('uid', $details['uid'])
  163. ->condition('collection_name', $details['collection_name'])
  164. ->execute()
  165. ->fetchField();
  166. if ($has_match) {
  167. throw new Exception('Cannot create the collection. One with this name already exists');
  168. }
  169. try {
  170. $collection_id = db_insert('tripal_collection')
  171. ->fields(array(
  172. 'collection_name' => $details['collection_name'],
  173. 'create_date' => time(),
  174. 'uid' => $details['uid'],
  175. 'description' => array_key_exists('description', $details) ? $details['description'] : '',
  176. ))
  177. ->execute();
  178. // Now add the second table with bundle info.
  179. $this->addFields($details, $collection_id);
  180. // Now load the job into this object.
  181. $this->load($collection_id);
  182. }
  183. catch (Exception $e) {
  184. throw new Exception('Cannot create collection: ' . $e->getMessage());
  185. }
  186. }
  187. /**
  188. * Creates a new tripal_collection_bundle entry.
  189. *
  190. * @param $details
  191. * An association array containing the details for a collection. The
  192. * details must include the following key/value pairs:
  193. * - bundle_name: The name of the TripalEntity content type.
  194. * - ids: An array of the entity IDs that form the collection.
  195. * - fields: An array of the field IDs that the collection is limited to.
  196. *
  197. * @throws Exception
  198. */
  199. public function addFields($details, $collection_id) {
  200. if (!$details['bundle_name']) {
  201. throw new Exception("Must provide a 'bundle_name' key to TripalEntityCollection::add().");
  202. }
  203. if (!$details['ids']) {
  204. throw new Exception("Must provide a 'ids' key to TripalEntityCollection::add().");
  205. }
  206. if (!$details['fields']) {
  207. throw new Exception("Must provide a 'fields' key to TripalEntityCollection::add().");
  208. }
  209. try {
  210. $collection_bundle_id = db_insert('tripal_collection_bundle')
  211. ->fields(array(
  212. 'bundle_name' => $details['bundle_name'],
  213. 'ids' => serialize($details['ids']),
  214. 'fields' => serialize($details['fields']),
  215. 'collection_id' => $collection_id,
  216. ))
  217. ->execute();
  218. // Now load the job into this object.
  219. //$this->load($collection_bundle_id);
  220. }
  221. catch (Exception $e) {
  222. throw new Exception('Cannot create collection: ' . $e->getMessage());
  223. }
  224. }
  225. /**
  226. * Retrieves the list of bundles associated with the collection.
  227. *
  228. * @return
  229. * An array of bundles.
  230. */
  231. public function getBundles() {
  232. $collection_id = $this->collection_id;
  233. // Return the bundles from the collection_bundle table.
  234. $result = db_select('tripal_collection_bundle')
  235. ->fields('tripal_collection_bundle', array('bundle_name'))
  236. ->condition('collection_id', $collection_id, '=')
  237. ->execute()
  238. ->fetchAll();
  239. return $result;
  240. }
  241. /**
  242. * Retrieves the site id for this specific bundle fo the collection.
  243. *
  244. * @return
  245. * A single site id.
  246. */
  247. public function getSiteId($bundle_name) {
  248. $collection_id = $this->collection_id;
  249. // Return the bundles from the collection_bundle table.
  250. $result = db_select('tripal_collection_bundle')
  251. ->fields('tripal_collection_bundle', array('site_id'))
  252. ->condition('collection_id', $collection_id, '=')
  253. ->condition('bundle_name', $bundle_name, '=')
  254. ->execute()
  255. ->fetchAssoc();
  256. return $result;
  257. }
  258. /**
  259. * Retrieves the list of appropriate download formatters for the basket.
  260. *
  261. * @return
  262. * An associative array where the key is the TripalFieldDownloader class
  263. * name and the value is the human-readable lable for the formatter.
  264. */
  265. public function getDownloadFormattersList($fields) {
  266. $downloaders = array();
  267. // Iterate through the fields and find out what download formats are
  268. // supported for this basket.
  269. foreach ($fields as $bundle_name => $field_group) {
  270. foreach ($field_group as $field_id) {
  271. // Check is $field_groups is an array because if it is that means we
  272. // nested arrays we need to deal with.
  273. if (is_array($field_id)) {
  274. foreach ($field_id as $field) {
  275. // If the $field is numeric it's a field id from the local site but
  276. // if it is not then it's a remote field that needs to be handled differently.
  277. if (!is_numeric($field)) {
  278. // Need the site ID from the tripal_collection_bundle table.
  279. $site_id = $this->getSiteId($bundle_name);
  280. // Use the webservices call to pull the available downloaders list.
  281. // Need to pull the vocab doc of the web services to know the download formats.
  282. // Use the api call to get the vocab for the site which will need to be parsed
  283. // to find the accession and the download types available.
  284. $site_vocab = tripal_web_services_vocab_request($site_id);
  285. foreach ($site_vocab as $item) {
  286. if (is_array($item)) {
  287. foreach ($item as $vocab_term) {
  288. if (!empty($vocab_term['supportedProperty'])) {
  289. $vocab_supported_properties = $vocab_term['supportedProperty'];
  290. if (is_array($vocab_supported_properties)) {
  291. foreach ($vocab_supported_properties as $property) {
  292. if ($property['property'] === $field) {
  293. if (in_array('tripal_formatters', $property)) {
  294. $download_types = $property['tripal_formatters'];
  295. foreach ($download_types as $download_type) {
  296. $this->downloaders[$download_type] = $download_type;
  297. continue 7;
  298. }
  299. }
  300. }
  301. }
  302. }
  303. else {
  304. if (in_array('tripal_formatters', $vocab_supported_properties)) {
  305. $download_types = $vocab_supported_properties['tripal_formatters'];
  306. foreach ($download_types as $download_type) {
  307. $this->downloaders[$download_type] = $download_type;
  308. continue 6;
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. else {
  318. $field_info = field_info_field_by_id($field);
  319. if (!$field_info) {
  320. continue;
  321. }
  322. }
  323. $field_name = $field_info['field_name'];
  324. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  325. // API function
  326. // All fields should support the Tab and CSV downloaders.
  327. $downloaders = array();
  328. $this->downloaders += tripal_get_field_field_formatters($field);
  329. }
  330. }
  331. else {
  332. $field = field_info_field_by_id($field_id);
  333. if (!$field) {
  334. continue;
  335. }
  336. $field_name = $field['field_name'];
  337. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  338. // API function
  339. $downloaders = array();
  340. $this->downloaders += tripal_get_field_field_formatters($field);
  341. }
  342. }
  343. }
  344. return $this->downloaders;
  345. }
  346. /**
  347. * Retrieves the list of appropriate download formatters for the basket.
  348. *
  349. * @return
  350. * An associative array where the key is the TripalFieldDownloader class
  351. * name and the value is the human-readable lable for the formatter.
  352. */
  353. public function getDownloadFormatters() {
  354. return $this->downloaders;
  355. }
  356. /**
  357. * Retrieves the list of entity IDs.
  358. *
  359. * @return
  360. * An array of numeric enity IDs.
  361. */
  362. public function getEntityIDs($bundle_name) {
  363. $collection_id = $this->collection_id;
  364. // Return the bundles from the collection_bundle table.
  365. $result = db_select('tripal_collection_bundle')
  366. ->fields('tripal_collection_bundle', array('ids'))
  367. ->condition('collection_id', $collection_id, '=')
  368. ->condition('bundle_name', $bundle_name, '=')
  369. ->execute()
  370. ->fetchAll();
  371. // Unserialize the array of standard class objects.
  372. $unserialized_result = [];
  373. foreach ($result as $id_list) {
  374. $unserialized_id_list = unserialize($id_list->ids);
  375. foreach ($id_list as $item) {
  376. $unserialized_result[] = $unserialized_id_list;
  377. }
  378. }
  379. return $unserialized_result;
  380. }
  381. /**
  382. * Retrieves the list of fields in the basket.
  383. *
  384. * @return
  385. * An array of numeric field IDs.
  386. */
  387. public function getFields($bundle_name) {
  388. $collection_id = $this->collection_id;
  389. // Return the bundles from the collection_bundle table.
  390. $result = db_select('tripal_collection_bundle')
  391. ->fields('tripal_collection_bundle', array('fields'))
  392. ->condition('collection_id', $collection_id, '=')
  393. ->condition('bundle_name', $bundle_name, '=')
  394. ->execute()
  395. ->fetchAll();
  396. // Unserialize the array of standard class objects.
  397. $unserialized_result = [];
  398. foreach ($result as $field_list) {
  399. $unserialized_field_list = unserialize($field_list->fields);
  400. foreach ($field_list as $item) {
  401. $unserialized_result[] = $unserialized_field_list;
  402. }
  403. }
  404. return $unserialized_result;
  405. }
  406. /**
  407. * Retrieves the date that the basket was created.
  408. *
  409. * @param $formatted
  410. * If TRUE then the date time will be formatted for human readability.
  411. * @return
  412. * A UNIX time stamp string containing the date or a human-readable
  413. * string if $formatted = TRUE.
  414. */
  415. public function getCreateDate($formatted = TRUE) {
  416. if ($formatted) {
  417. return format_date($this->create_date);
  418. }
  419. return $this->create_date;
  420. }
  421. /**
  422. * Retreives the name of the collection.
  423. *
  424. * @return
  425. * A string containing the name of the collection.
  426. */
  427. public function getName() {
  428. return $this->collection_name;
  429. }
  430. /**
  431. * Retrieves the collection ID.
  432. *
  433. * @return
  434. * A numeric ID for this collection.
  435. */
  436. public function getCollectionID(){
  437. return $this->collection_id;
  438. }
  439. /**
  440. * Retreives the collection description
  441. *
  442. * @return
  443. * A string containing the description of the collection.
  444. */
  445. public function getDescription() {
  446. return $this->description;
  447. }
  448. /**
  449. * Retrieves the user object of the user that owns the collection
  450. *
  451. * @return
  452. * A Drupal user object.
  453. */
  454. public function getUser() {
  455. return $this->user;
  456. }
  457. /**
  458. * Retrieves the ID of the user that owns the collection
  459. *
  460. * @return
  461. * The numeric User ID.
  462. */
  463. public function getUserID() {
  464. if ($this->user) {
  465. return $this->user->uid;
  466. }
  467. return NULL;
  468. }
  469. /**
  470. * Retrieves the output filename for the desired formatter.
  471. *
  472. * @param $formatter
  473. * The class name of the formatter to use. The formatter must
  474. * be compatible with the data collection.
  475. *
  476. * @throws Exception
  477. */
  478. public function getOutfile($formatter) {
  479. if(!$this->isFormatterCompatible($formatter)) {
  480. throw new Exception(t('The formatter, "%formatter", is not compatible with this data collection.', array('%formatter' => $formatter)));
  481. }
  482. if (!tripal_load_include_downloader_class($formatter)) {
  483. throw new Exception(t('Cannot find the formatter named "@formatter".', array('@formatter', $formatter)));
  484. }
  485. $extension = $formatter::$default_extension;
  486. $create_date = $this->getCreateDate(FALSE);
  487. $outfile = preg_replace('/[^\w]/', '_', ucwords($this->collection_name)) . '_collection' . '_' . $create_date . '.' . $extension;
  488. return $outfile;
  489. }
  490. /**
  491. * Indicates if the given formatter is compatible with the data collection.
  492. *
  493. * @param $formatter
  494. * The class name of the formatter to check.
  495. * @return boolean
  496. * TRUE if the formatter is compatible, FALSE otherwise.
  497. */
  498. public function isFormatterCompatible($formatter) {
  499. foreach ($this->downloaders as $class_name => $label) {
  500. if ($class_name == $formatter) {
  501. return TRUE;
  502. }
  503. }
  504. return FALSE;
  505. }
  506. /**
  507. * Writes the collection to all file downloadable formats.
  508. *
  509. * @throws Exception
  510. */
  511. public function writeAll() {
  512. foreach ($this->downloaders as $class_name => $label) {
  513. $this->write($class_name);
  514. }
  515. }
  516. /**
  517. * Retrieves the URL for the downloadable file.
  518. *
  519. * @param $formatter
  520. * The name of the class
  521. */
  522. public function getOutfileURL($formatter) {
  523. $outfile = $this->getOutfilePath($formatter);
  524. }
  525. /**
  526. * Retrieves the path for the downloadable file.
  527. *
  528. * The path is in the Drupal URI format.
  529. *
  530. * @param $formatter
  531. * The name of the class
  532. */
  533. public function getOutfilePath($formatter) {
  534. if(!$this->isFormatterCompatible($formatter)) {
  535. throw new Exception(t('The formatter, "@formatter", is not compatible with this data collection.', array('@formatter' => $formatter)));
  536. }
  537. if (!tripal_load_include_downloader_class($formatter)) {
  538. throw new Exception(t('Cannot find the formatter named "@formatter".', array('@formatter', $formatter)));
  539. }
  540. $outfile = $this->getOutfile($formatter);
  541. // Make sure the user directory exists
  542. $user_dir = 'public://tripal/users/' . $this->user->uid;
  543. $outfilePath = $user_dir. '/' . $outfile;
  544. return $outfilePath;
  545. }
  546. /**
  547. * Writes the collection to a file.
  548. *
  549. * @param formatter
  550. * The name of the formatter class to use (e.g. TripalTabDownloader). The
  551. * formatter must be compatible with the data collection.
  552. *
  553. * @throws Exception
  554. */
  555. public function write($formatter) {
  556. if (!$this->isFormatterCompatible($formatter)) {
  557. throw new Exception(t('The formatter, "@formatter", is not compatible with this data collection.', array('@formatter' => $formatter)));
  558. }
  559. if (!tripal_load_include_downloader_class($formatter)) {
  560. throw new Exception(t('Cannot find the formatter named "@formatter".', array('@formatter', $formatter)));
  561. }
  562. $outfile = $this->getOutfile($formatter);
  563. // Filter out fields that aren't supported by the formatter.
  564. $supported_fields = array();
  565. foreach ($this->fields as $field_group) {
  566. foreach ($field_group as $field_id) {
  567. // Check is $field_id is an array because if it is that means we
  568. // nested arrays we need to deal with.
  569. if (is_array($field_id)) {
  570. foreach ($field_id as $field) {
  571. // If the formatter is TripalTabDownloader or TripalCSVDownloader then
  572. // we always want to support the field.
  573. if ($tripal_create_collection_files == 'TripalTabDownloader' or $formatter == 'TripalCSVDownloader') {
  574. if (!in_array($field, $supported_fields)) {
  575. $supported_fields[] = $field;
  576. }
  577. continue;
  578. }
  579. // If the $field is numeric it's a field id from the local site but
  580. // if it is not then it's a remote field that needs to be handled differently.
  581. if (!is_numeric($field)) {
  582. // Need the site ID from the tripal_collection_bundle table.
  583. $collection_id = $this->collection_id;
  584. // Return the bundles from the collection_bundle table.
  585. $collections = db_select('tripal_collection_bundle')
  586. ->fields('tripal_collection_bundle')
  587. ->condition('collection_id', $collection_id, '=')
  588. ->execute()
  589. ->fetchAll();
  590. // Now that we have all possible bundles we need to find the right one.
  591. foreach ($collections as $collection) {
  592. $fields = unserialize($collection->fields);
  593. if (in_array($field, $fields)) {
  594. $site_id = $collection->site_id;
  595. }
  596. }
  597. // Use the webservices call to pull the available downloaders list.
  598. // Need to pull the vocab doc of the web services to know the download formats.
  599. // Use the api call to get the vocab for the site which will need to be parsed
  600. // to find the accession and the download types available.
  601. if (!empty($site_id)) {
  602. $site_vocab = tripal_web_services_vocab_request($site_id);
  603. foreach ($site_vocab as $item) {
  604. if (is_array($item)) {
  605. foreach ($item as $vocab_term) {
  606. if (!empty($vocab_term['supportedProperty'])) {
  607. $vocab_supported_properties = $vocab_term['supportedProperty'];
  608. if (is_array($vocab_supported_properties)) {
  609. foreach ($vocab_supported_properties as $property) {
  610. if ($property['property'] === $field) {
  611. if (in_array('tripal_formatters', $property)) {
  612. $download_types = $property['tripal_formatters'];
  613. foreach ($download_types as $download_type) {
  614. if ($formatter == $download_type) {
  615. $supported_fields[] = $field;
  616. continue 5;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. }
  623. else {
  624. if (in_array('tripal_formatters', $vocab_supported_properties)) {
  625. $download_types = $vocab_supported_properties['tripal_formatters'];
  626. foreach ($download_types as $download_type) {
  627. if ($formatter == $download_type) {
  628. $supported_fields[] = $field;
  629. continue 3;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. }
  640. else {
  641. // Otherwise, find out if the formatter specified is supported by the
  642. // field and if so then add it to our list of supported fields.
  643. $field_info = field_info_field_by_id($field);
  644. $field_name = $field_info['field_name'];
  645. $field_type = $field_info['type'];
  646. if (tripal_load_include_field_class($field_type)) {
  647. $settings = $field_type::$default_instance_settings;
  648. if (array_key_exists('download_formatters', $settings)) {
  649. if (in_array($formatter, $settings['download_formatters'])) {
  650. $supported_fields[] = $field;
  651. }
  652. }
  653. }
  654. else {
  655. // If the formatter is TripalTabDownloader or TripalCSVDownloader then
  656. // we always want to support the field.
  657. if ($formatter == 'TripalTabDownloader' or $formatter == 'TripalCSVDownloader') {
  658. if (!in_array($field_id, $supported_fields)) {
  659. $supported_fields[] = $field_id;
  660. }
  661. continue;
  662. }
  663. // Otherwise, find out if the formatter specified is supporte by the
  664. // field and if so then add it to our list of supported fields.
  665. $field = field_info_field_by_id($field_id);
  666. $field_name = $field['field_name'];
  667. $field_type = $field['type'];
  668. if (tripal_load_include_field_class($field_type)) {
  669. $settings = $field_type::$default_instance_settings;
  670. if (array_key_exists('download_formatters', $settings)) {
  671. if (in_array($formatter, $settings['download_formatters'])) {
  672. $supported_fields[] = $field_id;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. print_r($supported_fields);
  683. $downloader = new $formatter($this->bundles, $this->ids, $supported_fields, $outfile, $this->user->uid);
  684. $downloader->write();
  685. }
  686. }