tripal_bulk_loader.admin.templates.inc 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  1. <?php
  2. /**
  3. * @file
  4. * All functions in this file pertain to administrative management of bulk loader templates
  5. */
  6. /**
  7. * @section
  8. * Modify Template
  9. * The main form for creating/editing templates as a whole
  10. */
  11. /**
  12. * The main form reached at admin/tripal/tripal_bulk_loader/create and /edit
  13. *
  14. * @ingroup tripal_bulk_loader
  15. */
  16. function tripal_bulk_loader_modify_template_base_form($form, &$form_state = NULL, $mode) {
  17. // set the breadcrumb
  18. $breadcrumb = array();
  19. $breadcrumb[] = l('Home', '<front>');
  20. $breadcrumb[] = l('Administration', 'admin');
  21. $breadcrumb[] = l('Tripal', 'admin/tripal');
  22. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  23. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  24. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  25. drupal_set_breadcrumb($breadcrumb);
  26. // get template id from path and rebuild form
  27. if (isset($form_state['build_info']['args'][1])) {
  28. $mode = 'edit';
  29. if (preg_match('/^\d+$/', $form_state['build_info']['args'][1])) {
  30. $form_state['storage']['template_id'] = $form_state['build_info']['args'][1];
  31. $template_id = $form_state['storage']['template_id'];
  32. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  33. $result = db_query($sql, array(':template' => $template_id))->fetchObject();
  34. $form_state['storage']['template'] = unserialize($result->template_array);
  35. if (!$form_state['storage']['template']) {
  36. $form_state['storage']['template'] = array();
  37. }
  38. $form_state['storage']['template_name'] = $result->name;
  39. $form_state['storage']['record2priority'] = array();
  40. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  41. if (!is_array($record_array)) {
  42. continue; }
  43. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  44. }
  45. }
  46. else {
  47. $form_state['storage']['template_id'] = FALSE;
  48. $form['records']['no_records']['#value'] = TRUE;
  49. $form['fields']['total_fields']['#value'] = 0;
  50. }
  51. }
  52. else {
  53. $mode = 'create';
  54. $form_state['storage']['template_id'] = FALSE;
  55. $form['records']['no_records']['#value'] = TRUE;
  56. $form['fields']['total_fields']['#value'] = 0;
  57. }
  58. $form['mode'] = array(
  59. '#type' => 'hidden',
  60. '#value' => $mode,
  61. );
  62. if ($form_state['storage']['template_id']) {
  63. $form['template_name'] = array(
  64. '#type' => 'item',
  65. '#title' => 'Template',
  66. '#markup' => $form_state['storage']['template_name'],
  67. '#weight' => 1,
  68. );
  69. }
  70. else {
  71. if (preg_match('/create/', $mode)) {
  72. $form['new_template_name'] = array(
  73. '#type' => 'textfield',
  74. '#title' => 'Template Name',
  75. '#weight' => 1,
  76. );
  77. }
  78. elseif (preg_match('/edit/', $mode)) {
  79. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  80. $resource = db_query($sql);
  81. $templates = array();
  82. $templates[''] = 'Select a Template';
  83. while ($r = $resource->fetchObject()) {
  84. $templates[$r->template_id] = $r->name;
  85. }
  86. $form['template_id'] = array(
  87. '#title' => t('Template'),
  88. '#description' => t('Please select the template you would like to edit.'),
  89. '#type' => 'select',
  90. '#options' => $templates,
  91. '#default_value' => $form_state['storage']['template_id'],
  92. '#weight' => 0,
  93. '#required' => TRUE,
  94. '#weight' => 1,
  95. );
  96. }
  97. }
  98. $mode_title = (preg_match('/create/', $mode)) ? 'Create Template' : 'Edit Template';
  99. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  100. $form['submit'] = array(
  101. '#type' => 'submit',
  102. '#value' => $value,
  103. '#weight' => 10,
  104. );
  105. if (preg_match('/edit/', $mode) && !$form_state['storage']['template_id']) {
  106. return $form;
  107. }
  108. $form['records'] = array(
  109. '#type' => (isset($form_state['storage']['template_id']))? 'fieldset' : 'hidden',
  110. '#title' => t('Current Records'),
  111. '#collapsible' => TRUE,
  112. '#weight' => 2,
  113. '#description' => t('A "Record" is an entry in a database table. Each of the records '
  114. . 'added to the bulk loader will be added per constant set and per line of you '
  115. . 'input file when a Tripal Bulk Loading Job is created using this template. '
  116. . 'Furthermore, if you add two records for the same table (i.e. feature table) then '
  117. . 'two distinct entries will be created per line (i.e. if recordA specifies a parent '
  118. . 'feature and recordB specifies the child using different columns in the input file '
  119. . 'then you will have one entry in the feature table specifying the parent and '
  120. . 'another one for the child.')
  121. );
  122. $form['records']['description'] = array(
  123. '#type' => 'item',
  124. '#markup' => 'Records will be inserted into the chado database in the order listed below. To '
  125. .'change this order: <ul><li>Drag the rows into the correct order <br/>(If you don\'t have javascript enabled then enter '
  126. .'the numbers 1 and up in the Order textboxes to indicate the correct order).</li></ul>',
  127. );
  128. $form['records']['records-data'] = array(
  129. '#prefix' => '<div id="tripal-bulk-loader-template-records">',
  130. '#suffix' => '</div>',
  131. '#tree' => TRUE,
  132. '#theme' => 'tripal_bulk_loader_modify_template_base_form_records'
  133. );
  134. $form['records']['no_records'] = array(
  135. '#type' => 'hidden',
  136. '#value' => TRUE,
  137. );
  138. $form['records']['submit-new_record'] = array(
  139. '#type' => 'submit',
  140. '#value' => 'New Record/Field',
  141. );
  142. $form['records']['submit-reorder'] = array(
  143. '#type' => 'submit',
  144. '#value' => 'Save Order',
  145. );
  146. $form['fields'] = array(
  147. '#type' => (isset($form_state['storage']['template_id']))? 'fieldset' : 'hidden',
  148. '#title' => t('Current Fields'),
  149. '#collapsible' => TRUE,
  150. '#weight' => 3,
  151. '#description' => t('A "Field" is a single column in a database entry. As such a field '
  152. . 'is always associated with a database entry (record).')
  153. );
  154. $form['fields']['fields-data'] = array(
  155. '#tree' => TRUE,
  156. );
  157. if (isset($form_state['storage']['template'])) {
  158. // List Current Fields -------------------------------------------------------------
  159. $num_fields=0;
  160. $record_href_template = 'admin/tripal/loaders/bulk/template/%template/%action/%priority';
  161. $field_href_template = 'admin/tripal/loaders/bulk/template/%template/%action/%priority/%field';
  162. foreach ($form_state['storage']['template'] as $priority => $table_array) {
  163. if (!is_array($table_array)) {
  164. continue; }
  165. $form['records']['no_records']['#value'] = FALSE;
  166. $mode_value = '';
  167. $table_array['optional'] = (!isset($table_array['optional'])) ? FALSE : $table_array['optional'];
  168. if ($table_array['optional']) {
  169. $mode_value .= 'optional ';
  170. }
  171. // for backwards compatibility we want to convert insert_unique to be 'insert'
  172. // and optional to 'insert'
  173. $table_array['mode'] = (!isset($table_array['mode'])) ? 'insert' : $table_array['mode'];
  174. if (strcmp($table_array['mode'], 'insert_unique')==0) {
  175. $mode_value .= 'insert or select if duplicate';
  176. }
  177. elseif (strcmp($table_array['mode'], 'optional')==0) {
  178. $mode_value .= 'optional insert';
  179. }
  180. elseif (strcmp($table_array['mode'], 'insert_once')==0) {
  181. $mode_value .= 'insert once';
  182. }
  183. elseif (strcmp($table_array['mode'], 'select_once')==0) {
  184. $mode_value .= 'select once';
  185. }
  186. elseif ($table_array['mode']) {
  187. $mode_value .= $table_array['mode'];
  188. }
  189. else {
  190. $mode_value .= 'insert';
  191. }
  192. // add in the select if duplicate
  193. if (isset($table_array['select_if_duplicate'])) {
  194. if ($table_array['select_if_duplicate']) {
  195. $mode_value .= ' or select if duplicate';
  196. }
  197. }
  198. if (isset($table_array['select_optional'])) {
  199. if ($table_array['select_optional']) {
  200. $mode_value .= ' (no fail)';
  201. }
  202. }
  203. if (isset($table_array['update_if_duplicate'])) {
  204. if ($table_array['update_if_duplicate']) {
  205. $mode_value .= ' or update if duplicate';
  206. }
  207. }
  208. // add in the disabled
  209. if (isset($table_array['disable'])) {
  210. if ($table_array['disable']) {
  211. $mode_value .= '. <font color="Red">DISABLED</font>';
  212. }
  213. }
  214. $form['records']['records-data'][$priority] = array(
  215. 'title' => array(
  216. '#type' => 'item',
  217. '#markup' => filter_xss($priority . ". " . $table_array['record_id']),
  218. '#prefix' => "<a name=\"record_$priority\"></a>",
  219. ),
  220. 'view-fields-link' => array(
  221. '#type' => 'markup',
  222. '#markup' => "<a href=\"#fields_$priority\"> View Fields </a>",
  223. ),
  224. 'chado_table' => array(
  225. '#type' => 'item',
  226. '#markup' => filter_xss($table_array['table']),
  227. ),
  228. 'mode' => array(
  229. '#type' => 'item',
  230. '#markup' => $mode_value,
  231. ),
  232. 'new_priority' => array(
  233. '#type' => 'select',
  234. '#options' => range(1, sizeof($form_state['storage']['template'])),
  235. '#default_value' => $priority,
  236. ),
  237. 'old_priority' => array(
  238. '#type' => 'hidden',
  239. '#value' => $priority,
  240. ),
  241. 'id' => array(
  242. '#type' => 'hidden',
  243. '#value' => $priority,
  244. ),
  245. 'submit-edit_record' => array(
  246. '#type' => 'link',
  247. '#href' => str_replace(array('%template','%priority','%action'), array($template_id, $priority,'edit_record'), $record_href_template),
  248. '#title' => 'Edit',
  249. ),
  250. 'submit-delete_record' => array(
  251. '#type' => 'link',
  252. '#href' => str_replace(array('%template','%priority','%action'), array($template_id, $priority,'delete_record'), $record_href_template),
  253. '#options' => array(
  254. 'query' => array(
  255. 'record_name' => $table_array['record_id'],
  256. 'chado_table' => $table_array['table'],
  257. 'record_mode' => $mode_value
  258. )
  259. ),
  260. '#title' => 'Delete',
  261. ),
  262. 'submit-duplicate_record' => array(
  263. '#type' => 'link',
  264. '#href' => str_replace(array('%template','%priority','%action'), array($template_id, $priority,'duplicate_record'), $record_href_template),
  265. '#options' => array(
  266. 'query' => array(
  267. 'record_name' => $table_array['record_id'],
  268. 'chado_table' => $table_array['table'],
  269. 'record_mode' => $mode_value
  270. )
  271. ),
  272. '#title' => 'Duplicate'
  273. ),
  274. 'submit-add_field' => array(
  275. '#type' => 'link',
  276. '#href' => str_replace(array('%template','%priority','%action'), array($template_id, $priority,'add_field'), $record_href_template),
  277. '#title' => 'Add Field',
  278. ),
  279. );
  280. foreach ($table_array['fields'] as $field_index => $field) {
  281. $i = $num_fields;
  282. $fk_value = '';
  283. if (isset($field['foreign key'])) {
  284. if (isset($field['foreign field'])) {
  285. $fk_value = $field['foreign key'] . " (" . $field['foreign field'] . ")";
  286. }
  287. else {
  288. // for backwards compatibility we need to get the FK relationship to find
  289. // out what field we're joining on. For templates created using a
  290. // previous version this information isn't stored in the template
  291. // so we need to get it.
  292. $fk_priority = $form_state['storage']['record2priority'][$field['foreign key']];
  293. $fk_table = $form_state['storage']['template'][$fk_priority]['table'];
  294. $tbl_description = tripal_core_get_chado_table_schema($table_array['table']);
  295. foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
  296. foreach ($key_array['columns'] as $left_field => $right_field) {
  297. if ($key_table == $fk_table and $left_field == $field['field']) {
  298. $fk_value = $field['foreign key'] . " ($right_field)";
  299. }
  300. }
  301. }
  302. }
  303. }
  304. $form['fields']['fields-data'][$i] = array(
  305. 'record_id' => array(
  306. '#type' => 'item',
  307. '#markup' => $table_array['record_id'],
  308. '#prefix' => "<a name=\"fields_$priority\"></a>",
  309. ),
  310. 'view-record-link' => array(
  311. '#type' => 'markup',
  312. '#markup' => "<a href=\"#record_$priority\"> View Record</a>",
  313. ),
  314. 'priority_hidden' => array(
  315. '#type' => 'hidden',
  316. '#value' => $priority,
  317. ),
  318. 'field_name' => array(
  319. '#type' => 'item',
  320. '#markup' => $field['title'],
  321. ),
  322. 'chado_table_name' => array(
  323. '#type' => 'item',
  324. '#markup' => $table_array['table'],
  325. ),
  326. 'chado_table_hidden' => array(
  327. '#type' => 'hidden',
  328. '#value' => $table_array['table'],
  329. ),
  330. 'chado_field_name' => array(
  331. '#type' => 'item',
  332. '#markup' => $field['field'],
  333. ),
  334. 'column_num' => array(
  335. '#type' => 'item',
  336. '#markup' => (isset($field['spreadsheet column'])) ? $field['spreadsheet column'] : '',
  337. ),
  338. 'constant_value' => array(
  339. '#type' => 'item',
  340. '#markup' => (isset($field['constant value'])) ? $field['constant value'] : '',
  341. ),
  342. 'field_index' => array(
  343. '#type' => 'hidden',
  344. '#value' => $field_index
  345. ),
  346. 'foreign_record_id' => array(
  347. '#type' => 'item',
  348. '#markup' => $fk_value,
  349. ),
  350. 'edit_submit' => array(
  351. '#type' => 'link',
  352. '#href' => str_replace(array('%template','%priority', '%field', '%action'), array($template_id, $priority, $field_index, 'edit_field'), $field_href_template),
  353. '#title' => "Edit",
  354. ),
  355. 'delete_submit' => array(
  356. '#type' => 'link',
  357. '#href' => str_replace(array('%template','%priority', '%field', '%action'), array($template_id, $priority, $field_index,'delete_field'), $field_href_template),
  358. '#options' => array(
  359. 'query' => array(
  360. 'record_name' => $table_array['record_id'],
  361. 'field_name' => $field['title'],
  362. 'chado_table' => $table_array['table'],
  363. 'chado_field' => $field['field'],
  364. 'data_column' => (isset($field['spreadsheet column'])) ? $field['spreadsheet column'] : '',
  365. 'constant_value' => (isset($field['constant value'])) ? $field['constant value'] : '',
  366. 'foreign_record' => $fk_value
  367. )
  368. ),
  369. '#title' => "Delete",
  370. ),
  371. );
  372. $num_fields++;
  373. }
  374. }
  375. $form['fields']['total_fields'] = array(
  376. '#type' => 'hidden',
  377. '#value' => $num_fields,
  378. );
  379. }
  380. if ($form['records']['no_records']['#value']) {
  381. $form['records']['description'] = array(
  382. '#type' => 'item',
  383. '#markup' => 'There are currently no records. Click on the "New Record/Field" button below to add a record and the first field associated with it.',
  384. );
  385. unset($form['records']['submit-reorder']);
  386. $form['fields']['description'] = array(
  387. '#type' => 'item',
  388. '#markup' => 'There are currently no fields. Each field must be associated with a record, so, first, create a new record above.',
  389. );
  390. }
  391. return $form;
  392. }
  393. /**
  394. * Submit for tripal_bulk_loader_modify_template_base_form
  395. *
  396. * @ingroup tripal_bulk_loader
  397. */
  398. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state) {
  399. $save = TRUE;
  400. $form_state['rebuild'] = TRUE;
  401. if ($form_state['storage']['template_id']) {
  402. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  403. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  404. $form_state['storage']['template'] = unserialize($result->template_array);
  405. }
  406. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  407. // part of fix for 1st button set not working on records list (edit record, duplicate record, add field)
  408. if ($form_state['clicked_button']['#name'] === 'zero') {
  409. $form_state['clicked_button']['#name'] = '0';
  410. }
  411. switch ($op) {
  412. // Initialize after template is chosen ----------------------------------------
  413. case 'Edit Template':
  414. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  415. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  416. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  417. $form_state['storage']['template'] = unserialize($result->template_array);
  418. $form_state['storage']['template_name'] = $result->name;
  419. $form_state['storage']['record2priority'] = array();
  420. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  421. if (!is_array($record_array)) {
  422. continue;
  423. }
  424. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  425. }
  426. $form_state['rebuild'] = FALSE;
  427. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  428. break;
  429. case 'Create Template':
  430. $record = array(
  431. 'name' => $form_state['values']['new_template_name'],
  432. 'template_array' => array(),
  433. 'created' => time(),
  434. 'changed' => time()
  435. );
  436. $result = drupal_write_record('tripal_bulk_loader_template', $record);
  437. $form_state['storage']['template_id'] = $record['template_id'];
  438. $form_state['storage']['template_name'] = $record['name'];
  439. $form_state['storage']['template'] = array();
  440. $form_state['rebuild'] = FALSE;
  441. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$record['template_id'].'/edit';
  442. break;
  443. // Save Reordered Records -----------------------------------------------------
  444. case 'Save Order':
  445. $new_template = $form_state['storage']['template'];
  446. // unset old elements
  447. $form_state['storage']['record2priority'] = array();
  448. foreach ($new_template as $priority => $record_array) {
  449. if (preg_match('/\d+/', $priority)) {
  450. unset($new_template[$priority]);
  451. }
  452. }
  453. //set elements in new order
  454. foreach ($form_state['values']['records-data'] as $item) {
  455. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  456. $record_name = $new_template[$item['new_priority']]['record_id'];
  457. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  458. }
  459. ksort($new_template);
  460. $form_state['storage']['template'] = $new_template;
  461. break;
  462. case 'New Record/Field':
  463. $form_state['rebuild'] = FALSE;
  464. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/add_record';
  465. $save = FALSE;
  466. break;
  467. } //end of switch
  468. // Save Template
  469. if ($save) {
  470. $record = array(
  471. 'template_id' => $form_state['storage']['template_id'],
  472. 'template_array' => serialize($form_state['storage']['template']),
  473. 'changed' => time()
  474. );
  475. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  476. drupal_set_message(t('Template Saved.'));
  477. }
  478. }
  479. /**
  480. * @section
  481. * Delete Template
  482. */
  483. /**
  484. * Delete Template Form
  485. * This form allows admin to delete already existing templates
  486. *
  487. * @ingroup tripal_bulk_loader
  488. */
  489. function tripal_bulk_loader_delete_template_base_form($form, $form_state) {
  490. $form = array();
  491. // set the breadcrumb
  492. $breadcrumb = array();
  493. $breadcrumb[] = l('Home', '<front>');
  494. $breadcrumb[] = l('Administration', 'admin');
  495. $breadcrumb[] = l('Tripal', 'admin/tripal');
  496. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  497. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  498. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  499. drupal_set_breadcrumb($breadcrumb);
  500. $template_id = $form_state['build_info']['args'][0];
  501. $form_state['storage']['template_id'] = $template_id;
  502. $description = '';
  503. if (isset($_GET['template_name'])) {
  504. $form_state['storage']['template_name'] = $_GET['template_name'];
  505. $description .= '<p><strong>Template:</strong> '.$_GET['template_name'].'</p>';
  506. }
  507. $description .= '<p><strong>Are you sure you want to delete this template?</strong></p>';
  508. $yes = 'Delete Template';
  509. $no = 'Cancel';
  510. $form['#attributes']['class'][] = 'confirmation';
  511. $form['description'] = array('#markup' => $description);
  512. $form['actions'] = array('#type' => 'actions');
  513. $form['actions']['submit'] = array(
  514. '#type' => 'submit',
  515. '#value' => $yes ? $yes : t('Confirm'),
  516. );
  517. $form['actions']['cancel'] = array(
  518. '#type' => 'link',
  519. '#title' => $no ? $no : t('Cancel'),
  520. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  521. );
  522. // By default, render the form using theme_confirm_form().
  523. if (!isset($form['#theme'])) {
  524. $form['#theme'] = 'confirm_form';
  525. }
  526. return $form;
  527. }
  528. /**
  529. * Delete Template Form Submit
  530. *
  531. * @param $form
  532. * The form that was submitted
  533. * @param $form_state
  534. * The values and storage that were submitted
  535. *
  536. * @ingroup tripal_bulk_loader
  537. */
  538. function tripal_bulk_loader_delete_template_base_form_submit($form, &$form_state) {
  539. if (isset($form_state['storage']['template_id'])) {
  540. if ($form_state['storage']['template_id'] > 0) {
  541. db_delete('tripal_bulk_loader_template')
  542. ->condition('template_id', $form_state['storage']['template_id'])
  543. ->execute();
  544. if (isset($form_state['storage']['template_name'])) {
  545. drupal_set_message('Successfully deleted '.$form_state['storage']['template_name'].' template.');
  546. }
  547. else {
  548. drupal_set_message('Successfully deleted the template');
  549. }
  550. }
  551. }
  552. $form_state['rebuild'] = FALSE;
  553. $form_state['redirect'] = 'admin/tripal/loaders/bulk/templates';
  554. }
  555. /**
  556. * @section
  557. * Import/Export Template
  558. */
  559. /**
  560. * Import Template Form
  561. *
  562. * On export, simply selects the serialized array from the db for a given template
  563. * and presents it to the user. On import, a serialized template array and a name is
  564. * supplied and a template record is created.
  565. *
  566. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  567. *
  568. * @param $form_state
  569. * The values and storage for the form
  570. * @param $mode
  571. * Either 'import' or 'export' to indicate which function is being performed
  572. * @return
  573. * A form array to be rendered by drupal_get_form
  574. *
  575. * @ingroup tripal_bulk_loader
  576. */
  577. function tripal_bulk_loader_import_template_form($form, $form_state) {
  578. $form = array();
  579. // set the breadcrumb
  580. $breadcrumb = array();
  581. $breadcrumb[] = l('Home', '<front>');
  582. $breadcrumb[] = l('Administration', 'admin');
  583. $breadcrumb[] = l('Tripal', 'admin/tripal');
  584. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  585. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  586. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  587. drupal_set_breadcrumb($breadcrumb);
  588. $form['new_template_name'] = array(
  589. '#type' => 'textfield',
  590. '#title' => 'Template Name',
  591. '#weight' => 1,
  592. );
  593. $form['template_array'] = array(
  594. '#type' => 'textarea',
  595. '#title' => 'Template Definition',
  596. '#default_value' => (isset($form_state['values']['template_array'])) ? $form_state['values']['template_array'] : '',
  597. '#description' => t('A serialized array describing the template.'),
  598. '#rows' => 14,
  599. '#weight' => 5,
  600. );
  601. $form['submit'] = array(
  602. '#type' => 'submit',
  603. '#value' => 'Import',
  604. '#weight' => 10,
  605. );
  606. return $form;
  607. }
  608. /**
  609. * Import Template Form Submit
  610. *
  611. * @param $form
  612. * The form that was submitted
  613. * @param $form_state
  614. * The values and storage that were submitted
  615. *
  616. * @ingroup tripal_bulk_loader
  617. */
  618. function tripal_bulk_loader_import_template_form_submit($form, &$form_state) {
  619. // get the template array, and convert from text into an array.
  620. $t = array();
  621. eval("\$t = " . $form_state['values']['template_array'] . ";");
  622. $record = array(
  623. 'name' => $form_state['values']['new_template_name'],
  624. 'template_array' => serialize($t),
  625. 'created' => time(),
  626. 'changed' => time()
  627. );
  628. $result = drupal_write_record('tripal_bulk_loader_template', $record);
  629. if ($result) {
  630. drupal_set_message(t('Successfully imported Tripal Bulk Loader Template.'));
  631. }
  632. $form_state['rebuild'] = FALSE;
  633. $form_state['redirect'] = 'admin/tripal/loaders/bulk/templates';
  634. }
  635. /**
  636. * Export Template Form
  637. *
  638. * On export, simply selects the serialized array from the db for a given template
  639. * and presents it to the user. On import, a serialized template array and a name is
  640. * supplied and a template record is created.
  641. *
  642. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  643. *
  644. * @param $form_state
  645. * The values and storage for the form
  646. * @return
  647. * A form array to be rendered by drupal_get_form
  648. *
  649. * @ingroup tripal_bulk_loader
  650. */
  651. function tripal_bulk_loader_export_template_form($form, &$form_state) {
  652. // set the breadcrumb
  653. $breadcrumb = array();
  654. $breadcrumb[] = l('Home', '<front>');
  655. $breadcrumb[] = l('Administration', 'admin');
  656. $breadcrumb[] = l('Tripal', 'admin/tripal');
  657. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  658. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  659. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  660. drupal_set_breadcrumb($breadcrumb);
  661. $template_id = $form_state['build_info']['args'][0];
  662. $form_state['storage']['template_id'] = $template_id;
  663. if ($template_id > 0) {
  664. $result = db_select('tripal_bulk_loader_template','t')
  665. ->fields('t')
  666. ->condition('t.template_id',$template_id)
  667. ->execute();
  668. $template = $result->fetchObject();
  669. $t = var_export(unserialize($template->template_array), TRUE);
  670. $t = preg_replace("/\n\s+array/", "array", $t); // move array( to previous line
  671. $t = preg_replace("/true/", "TRUE", $t); // upper case true
  672. $t = preg_replace("/false/", "FALSE", $t); // upper case false
  673. $t = preg_replace("/array\(/", "array (", $t); // put a space between array and paren
  674. $form_state['storage']['template_array'] = $t;
  675. }
  676. $form['name'] = array(
  677. '#type' => 'item',
  678. '#title' => 'Template Name',
  679. '#markup' => $template->name,
  680. );
  681. $form['template_array'] = array(
  682. '#type' => 'textarea',
  683. '#title' => 'Export',
  684. '#default_value' => $form_state['storage']['template_array'],
  685. '#description' => t('Use this serialized array for import.'),
  686. '#rows' => 30,
  687. '#weight' => 5,
  688. );
  689. return $form;
  690. }
  691. /**
  692. * @section
  693. * Edit Record Form
  694. */
  695. /**
  696. * Edit Record Form
  697. *
  698. * D7 @todo: Needs to be debugged
  699. *
  700. * This form is meant to be called from a bulk loader form. The following should be set
  701. * in the query section of the path:
  702. * - template_id=\d+: the template which the edited record is part of
  703. * - record_id=\d+: the priority or key in the template array of the record to be edited
  704. *
  705. * @param $form_state
  706. * Contains the values and storage for the form
  707. * @return
  708. * A form array to be rendered by drupal_get_form
  709. *
  710. * @ingroup tripal_bulk_loader
  711. */
  712. function tripal_bulk_loader_edit_template_record_form($form, &$form_state) {
  713. $form['#cache'] = TRUE; // Make sure the form is cached.
  714. // get args from path
  715. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  716. $form_state['storage']['template_id'] = $template_id;
  717. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  718. $form_state['storage']['record_id'] = $record_id;
  719. $form_state['values']['field_group'] = $record_id;
  720. $form_state['storage']['original_priority'] = $record_id;
  721. // if there is no template supplied don't return rest of form
  722. if (!$template_id) {
  723. return $form;
  724. }
  725. // set the breadcrumb
  726. $breadcrumb = array();
  727. $breadcrumb[] = l('Home', '<front>');
  728. $breadcrumb[] = l('Administration', 'admin');
  729. $breadcrumb[] = l('Tripal', 'admin/tripal');
  730. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  731. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  732. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  733. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  734. drupal_set_breadcrumb($breadcrumb);
  735. // Pre-process values/defaults ---------------------------
  736. // If this is the first load of the form (no form state) we need to initialize some variables
  737. if (!isset($form_state['storage']['template'])) {
  738. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  739. $template = db_query($sql, array(':template' => $template_id))->fetchObject();
  740. $form_state['storage']['template_array'] = unserialize($template->template_array);
  741. $form_state['storage']['template'] = $template;
  742. $form_state['storage']['record2priority'] = array();
  743. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  744. if (!is_array($record_array)) {
  745. continue;
  746. }
  747. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  748. }
  749. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  750. }
  751. else {
  752. $template = $form_state['storage']['template'];
  753. }
  754. // Tables and default table
  755. $tables = tripal_core_get_chado_tables(TRUE);
  756. if (isset($form_state['values']['chado_table'])) {
  757. $table = $form_state['values']['chado_table'];
  758. }
  759. else {
  760. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  761. }
  762. // get the default mode
  763. if (isset($form_state['storage']['original_priority'])) {
  764. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['mode'])) {
  765. $mode = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['mode'];
  766. }
  767. else {
  768. $mode = 'insert';
  769. }
  770. // get default for the select optional
  771. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional'])) {
  772. $select_optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional'];
  773. }
  774. else {
  775. $select_optional = 0;
  776. }
  777. // get default for the select if duplicate
  778. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_if_duplicate'])) {
  779. $select_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_if_duplicate'];
  780. }
  781. else {
  782. $select_if_duplicate = 1;
  783. }
  784. // get default for the update if duplicate
  785. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['update_if_duplicate'])) {
  786. $update_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['update_if_duplicate'];
  787. }
  788. else {
  789. $update_if_duplicate = 0;
  790. }
  791. // get default for the select if duplicate
  792. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['optional'])) {
  793. $optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['optional'];
  794. }
  795. else {
  796. $optional = 0;
  797. }
  798. // get the default for disabling the record
  799. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['disable'])) {
  800. $disable = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['disable'];
  801. }
  802. else {
  803. $disable = 0;
  804. }
  805. }
  806. else {
  807. $mode = 'insert';
  808. $select_optional = 0;
  809. $select_if_duplicate = 1;
  810. $update_if_duplicate = 0;
  811. $optional = 0;
  812. $disable = 0;
  813. }
  814. // this is just for backwards compatibility. the insert_unique mode type is no longer available
  815. if (strcmp($mode, 'insert_unique')==0) {
  816. $mode = 'insert';
  817. $select_if_duplicate = 1;
  818. }
  819. // this is just for backwards compatibility. the insert_unique mode type is no longer available
  820. if (strcmp($mode, 'optional')==0) {
  821. $mode = 'insert';
  822. $optional = 1;
  823. }
  824. //dpm($form_state, 'form state');
  825. // Form Proper -------------------------------------------
  826. $form['template_name'] = array(
  827. '#type' => 'item',
  828. '#title' => 'Template',
  829. '#markup' => $template->name,
  830. );
  831. $form['template_id'] = array(
  832. '#type' => 'hidden',
  833. '#value' => $template_id,
  834. );
  835. $form['edit_record'] = array(
  836. '#type' => 'markup',
  837. );
  838. // check template array for records then add one more
  839. if (!$form_state['storage']['record2priority']) {
  840. $groups = array();
  841. }
  842. else {
  843. $groups = array_flip($form_state['storage']['record2priority']);
  844. }
  845. $priority_default = $form_state['values']['field_group'];
  846. $form['edit_record']['field_group'] = array(
  847. '#type' => 'select',
  848. '#title' => 'Record',
  849. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  850. '#options' => $groups,
  851. '#default_value' => $priority_default,
  852. '#required' => TRUE,
  853. );
  854. $form['edit_record']['record_name'] = array(
  855. '#type' => 'textfield',
  856. '#title' => 'Unique Record Name',
  857. '#default_value' => $groups[$priority_default],
  858. );
  859. $form['edit_record']['chado_table'] = array(
  860. '#type' => 'select',
  861. '#title' => t('Chado Table'),
  862. '#description' => 'This changes the chado table for all fields in this record.',
  863. '#options' => $tables,
  864. '#default_value' => $table,
  865. );
  866. $form['edit_record']['mode'] = array(
  867. '#type' => 'radios',
  868. '#title' => 'Action to take when Loading Record',
  869. '#options' => array(
  870. 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
  871. 'select_once' => 'SELECT ONCE: Select the record only once for each constant set.',
  872. 'insert' => 'INSERT: Insert the record',
  873. 'insert_once' => 'INSERT ONCE: Record will be inserted once for each constant set. If the record is the same across multiple constant sets, make sure to select "SELECT if duplicate" as well.',
  874. ),
  875. '#default_value' => $mode
  876. );
  877. $form['edit_record']['select_options'] = array(
  878. '#type' => 'markup',
  879. '#markup' => t('Additional Select Options:'),
  880. );
  881. $form['edit_record']['select_optional'] = array(
  882. '#type' => 'checkbox',
  883. '#title' => t('Continue if no record exists or too many exist.'),
  884. '#description' => t('By default if a select does not find a match the loader will fail, or if it finds too many matches it will fail. Check here to allow the loader to continue when no match is found. In either case no value is passed on.'),
  885. '#default_value' => $select_optional
  886. );
  887. $form['edit_record']['insert_options'] = array(
  888. '#type' => 'markup',
  889. '#prefix' => '<br>',
  890. '#markup' => t('Additional Insert Options:'),
  891. );
  892. $form['edit_record']['select_if_duplicate'] = array(
  893. '#type' => 'checkbox',
  894. '#title' => t('SELECT if duplicate (no insert)'),
  895. '#description' => t('If this is not the first time this record has been added then perform a select rather than an insert.'),
  896. '#default_value' => $select_if_duplicate
  897. );
  898. $form['edit_record']['update_if_duplicate'] = array(
  899. '#type' => 'checkbox',
  900. '#title' => t('UPDATE if duplicate (no insert)'),
  901. '#description' => t('If this is not the first time this record has been added then perform an update rather than an insert.'),
  902. '#default_value' => $update_if_duplicate
  903. );
  904. $form['edit_record']['optional'] = array(
  905. '#type' => 'checkbox',
  906. '#title' => t('Optional'),
  907. '#description' => t('The insert, update or select will only be performed only if all required data are present'),
  908. '#default_value' => $optional
  909. );
  910. $form['edit_record']['disable'] = array(
  911. '#type' => 'checkbox',
  912. '#title' => t('Disable this record'),
  913. '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"),
  914. '#default_value' => $disable,
  915. );
  916. $form['edit_record']['submit-edit_record'] = array(
  917. '#type' => 'submit',
  918. '#value' => 'Edit Record'
  919. );
  920. $form['edit_record']['submit-cancel'] = array(
  921. '#type' => 'submit',
  922. '#value' => 'Cancel'
  923. );
  924. return $form;
  925. }
  926. /**
  927. * Implements hook_form_validate()
  928. *
  929. * D7 @todo: Needs to be debugged
  930. *
  931. * @ingroup tripal_bulk_loader
  932. */
  933. function tripal_bulk_loader_edit_template_record_form_validate($form, $form_state) {
  934. // Don't worry about validation when Cancel button is clicked
  935. if ($form_state['clicked_button']['#value'] == 'Edit Record') {
  936. $is_unique = tripal_bulk_loader_is_record_name_unique(
  937. $form_state['values']['record_name'],
  938. $form_state['values']['template_id'],
  939. $form_state['storage']['template_array'],
  940. $form_state['values']['field_group']
  941. );
  942. if (!$is_unique) {
  943. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  944. }
  945. }
  946. }
  947. /**
  948. * Edit Record Form Submit
  949. *
  950. * D7 @todo: Needs to be debugged
  951. *
  952. * @param $form
  953. * The form that was submitted
  954. * @param $form_state
  955. * Contains the values and storage for the form
  956. *
  957. * @ingroup tripal_bulk_loader
  958. */
  959. function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
  960. if ($form_state['values']['op'] == 'Edit Record') {
  961. $template = $form_state['storage']['template_array'];
  962. $original_record_name = $template[ $form_state['storage']['original_priority'] ]['record_id'];
  963. // Edit Record
  964. $record = $template[ $form_state['storage']['original_priority'] ];
  965. $record['record_id'] = $form_state['values']['record_name'];
  966. $record['mode'] = $form_state['values']['mode'];
  967. $record['table'] = $form_state['values']['chado_table'];
  968. $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate'];
  969. $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate'];
  970. $record['select_optional'] = $form_state['values']['select_optional'];
  971. $record['disable'] = $form_state['values']['disable'];
  972. $record['optional'] = $form_state['values']['optional'];
  973. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  974. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  975. $template[ $form_state['values']['field_group'] ] = $record;
  976. unset($template[ $form_state['storage']['original_priority'] ]);
  977. }
  978. else {
  979. $template[ $form_state['storage']['original_priority'] ] = $record;
  980. }
  981. // Go through all records and update this record name where it was used for a foreign key
  982. // Foreach field in each record that is of type: foreign key
  983. foreach ($template as $priority => $record) {
  984. foreach ($record['fields'] as $field_index => $field) {
  985. if ($field['type'] === 'foreign key') {
  986. // Check if this points to the old record name and if so, update it
  987. if ($field['foreign key'] === $original_record_name) {
  988. $template[$priority]['fields'][$field_index]['foreign key'] = $form_state['values']['record_name'];
  989. }
  990. }
  991. }
  992. }
  993. // Save Template
  994. $form_state['storage']['template']->template_array = serialize($template);
  995. $form_state['storage']['template']->changed = time();
  996. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  997. if ($success) {
  998. drupal_set_message(t('Successfully Updated Template Record'));
  999. drupal_set_message(t('Template Saved.'));
  1000. $form_state['rebuild'] = FALSE;
  1001. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/' . $form_state['storage']['template']->template_id . '/edit';
  1002. }
  1003. else {
  1004. drupal_set_message(t('Unable to Save Template!'), 'error');
  1005. watchdog('T_bulk_loader',
  1006. 'Unable to save bulk loader template: %template',
  1007. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1008. WATCHDOG_ERROR
  1009. );
  1010. }
  1011. }
  1012. elseif ($form_state['values']['op'] == 'Cancel') {
  1013. $form_state['rebuild'] = FALSE;
  1014. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/' . $form_state['storage']['template']->template_id . '/edit';
  1015. }
  1016. }
  1017. /**
  1018. * Delete Record Form
  1019. *
  1020. * This form is meant to be called from a bulk loader form
  1021. *
  1022. * D7 @todo: Needs to be debugged
  1023. *
  1024. * @param $form_state
  1025. * Contains the values and storage for the form
  1026. * @return
  1027. * A form array to be rendered by drupal_get_form
  1028. *
  1029. * @ingroup tripal_bulk_loader
  1030. */
  1031. function tripal_bulk_loader_delete_template_record_form($form, &$form_state) {
  1032. $form['#cache'] = TRUE; // Make sure the form is cached.
  1033. // get args from path
  1034. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  1035. $form_state['storage']['template_id'] = $template_id;
  1036. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1037. $form_state['storage']['record_id'] = $record_id;
  1038. // if there is no template supplied don't return rest of form
  1039. if (!$template_id) {
  1040. return $form;
  1041. }
  1042. // set the breadcrumb
  1043. $breadcrumb = array();
  1044. $breadcrumb[] = l('Home', '<front>');
  1045. $breadcrumb[] = l('Administration', 'admin');
  1046. $breadcrumb[] = l('Tripal', 'admin/tripal');
  1047. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  1048. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  1049. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  1050. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  1051. drupal_set_breadcrumb($breadcrumb);
  1052. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  1053. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  1054. $_GET['record_mode'] = (isset($_GET['record_mode'])) ? $_GET['record_mode'] : '';
  1055. $description = '';
  1056. $description .= '<table>';
  1057. $description .= '<tr><th></th><th>Record Name</th><th>Chado Table</th><th>Mode</th></tr>';
  1058. $description .= '<tr><td>' . $record_id
  1059. . '</td><td>' . $_GET['record_name']
  1060. . '</td><td>' . $_GET['chado_table']
  1061. . '</td><td>' . $_GET['record_mode']
  1062. . '</td></tr>';
  1063. $description .= '</table>';
  1064. $description .= '<p><strong>Are you sure you want to delete this record?</strong></p>';
  1065. $yes = 'Delete Record';
  1066. $no = 'Cancel';
  1067. $form['#attributes']['class'][] = 'confirmation';
  1068. $form['description'] = array('#markup' => $description);
  1069. $form['actions'] = array('#type' => 'actions');
  1070. $form['actions']['submit'] = array(
  1071. '#type' => 'submit',
  1072. '#value' => $yes ? $yes : t('Confirm'),
  1073. );
  1074. $form['actions']['cancel'] = array(
  1075. '#type' => 'link',
  1076. '#title' => $no ? $no : t('Cancel'),
  1077. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  1078. );
  1079. // By default, render the form using theme_confirm_form().
  1080. if (!isset($form['#theme'])) {
  1081. $form['#theme'] = 'confirm_form';
  1082. }
  1083. return $form;
  1084. }
  1085. /**
  1086. * Delete Record Form
  1087. *
  1088. * This form is meant to be called from a bulk loader form
  1089. *
  1090. * D7 @todo: Needs to be debugged
  1091. *
  1092. * @param $form_state
  1093. * Contains the values and storage for the form
  1094. * @return
  1095. * A form array to be rendered by drupal_get_form
  1096. *
  1097. * @ingroup tripal_bulk_loader
  1098. */
  1099. function tripal_bulk_loader_delete_template_record_form_submit($form, &$form_state) {
  1100. $form_state['rebuild'] = FALSE;
  1101. $template_id = $form_state['storage']['template_id'];
  1102. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit';
  1103. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1104. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  1105. $form_state['storage']['template'] = unserialize($result->template_array);
  1106. $form_state['storage']['record2priority'] = array();
  1107. $new_template = tripal_bulk_loader_delete_record($form_state['storage']['record_id'], $form_state['storage']['template']);
  1108. if (!empty($new_template)) {
  1109. $form_state['storage']['template'] = $new_template;
  1110. }
  1111. drupal_set_message(t('Deleted Field from Template.'));
  1112. $record = array(
  1113. 'template_id' => $form_state['storage']['template_id'],
  1114. 'template_array' => serialize($form_state['storage']['template']),
  1115. 'changed' => time()
  1116. );
  1117. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  1118. drupal_set_message(t('Template Saved.'));
  1119. }
  1120. /**
  1121. * Duplicate Record Form
  1122. *
  1123. * This form is meant to be called from a bulk loader form
  1124. *
  1125. * D7 @todo: Needs to be debugged
  1126. *
  1127. * @param $form_state
  1128. * Contains the values and storage for the form
  1129. * @return
  1130. * A form array to be rendered by drupal_get_form
  1131. *
  1132. * @ingroup tripal_bulk_loader
  1133. */
  1134. function tripal_bulk_loader_duplicate_template_record_form($form, &$form_state) {
  1135. $form['#cache'] = TRUE; // Make sure the form is cached.
  1136. // get args from path
  1137. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  1138. $form_state['storage']['template_id'] = $template_id;
  1139. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1140. $form_state['storage']['record_id'] = $record_id;
  1141. // if there is no template supplied don't return rest of form
  1142. if (!$template_id) {
  1143. return $form;
  1144. }
  1145. // set the breadcrumb
  1146. $breadcrumb = array();
  1147. $breadcrumb[] = l('Home', '<front>');
  1148. $breadcrumb[] = l('Administration', 'admin');
  1149. $breadcrumb[] = l('Tripal', 'admin/tripal');
  1150. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  1151. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  1152. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  1153. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  1154. drupal_set_breadcrumb($breadcrumb);
  1155. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  1156. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  1157. $_GET['record_mode'] = (isset($_GET['record_mode'])) ? $_GET['record_mode'] : '';
  1158. $description = '';
  1159. $description .= '<table>';
  1160. $description .= '<caption>Record to be Duplicated</caption>';
  1161. $description .= '<tr><th></th><th>Record Name</th><th>Chado Table</th><th>Mode</th></tr>';
  1162. $description .= '<tr><td>' . $record_id
  1163. . '</td><td>' . $_GET['record_name']
  1164. . '</td><td>' . $_GET['chado_table']
  1165. . '</td><td>' . $_GET['record_mode']
  1166. . '</td></tr>';
  1167. $description .= '</table>';
  1168. $form['description'] = array(
  1169. '#type' => 'markup',
  1170. '#markup' => $description,
  1171. );
  1172. $form['new_record_name'] = array(
  1173. '#type' => 'textfield',
  1174. '#title' => t('New Record Name'),
  1175. '#default_value' => $record_id . '_' . date('YzHi')
  1176. );
  1177. $form['actions'] = array('#type' => 'actions');
  1178. $form['actions']['submit'] = array(
  1179. '#type' => 'submit',
  1180. '#value' => t('Duplicate Record'),
  1181. );
  1182. $form['actions']['cancel'] = array(
  1183. '#type' => 'link',
  1184. '#title' => t('Cancel'),
  1185. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  1186. );
  1187. return $form;
  1188. }
  1189. /**
  1190. * Delete Record Form
  1191. *
  1192. * This form is meant to be called from a bulk loader form
  1193. *
  1194. * D7 @todo: Needs to be debugged
  1195. *
  1196. * @param $form_state
  1197. * Contains the values and storage for the form
  1198. * @return
  1199. * A form array to be rendered by drupal_get_form
  1200. *
  1201. * @ingroup tripal_bulk_loader
  1202. */
  1203. function tripal_bulk_loader_duplicate_template_record_form_submit($form, &$form_state) {
  1204. $form_state['rebuild'] = FALSE;
  1205. $template_id = $form_state['storage']['template_id'];
  1206. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit';
  1207. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1208. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  1209. $form_state['storage']['template'] = unserialize($result->template_array);
  1210. // original record (one to be duplicated)
  1211. $orig_priority = $form_state['storage']['record_id'];
  1212. $record = $form_state['storage']['template'][ $orig_priority ];
  1213. // new record
  1214. $record['record_id'] = $form_state['values']['new_record_name'];
  1215. $form_state['storage']['template'][] = $record;
  1216. $record = array(
  1217. 'template_id' => $form_state['storage']['template_id'],
  1218. 'template_array' => serialize($form_state['storage']['template']),
  1219. 'changed' => time()
  1220. );
  1221. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  1222. drupal_set_message(t('Template Saved.'));
  1223. }
  1224. /**
  1225. * @section
  1226. * Add/Edit Field Forms
  1227. */
  1228. /**
  1229. * Determine default values needed for add/edit field form
  1230. */
  1231. function tripal_bulk_loader_template_field_form_default_values($mode, &$form_state) {
  1232. $v = array();
  1233. $v['mode'] = $mode;
  1234. $v['template_id'] = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1235. $form_state['storage']['template_id'] = $v['template_id'];
  1236. $template_id = $v['template_id'];
  1237. $v['record_id'] = (isset($form_state['build_info']['args'][2])) ? $form_state['build_info']['args'][2] : FALSE;
  1238. $v['no_record_id'] = ($mode == 'create_record') ? TRUE : FALSE;
  1239. $form_state['storage']['record_id'] = $v['record_id'];
  1240. $record_id = $v['record_id'];
  1241. $priority = $record_id;
  1242. $v['field_index'] = (isset($form_state['build_info']['args'][3])) ? $form_state['build_info']['args'][3] : FALSE;
  1243. $v['no_field_index'] = (isset($form_state['build_info']['args'][3])) ? FALSE : TRUE;
  1244. $form_state['storage']['field_index'] = $v['field_index'];
  1245. $field_index = $v['field_index'];
  1246. if (!isset($form_state['values'])) {
  1247. $form_state['values'] = array();
  1248. }
  1249. // Get the template if not already stored
  1250. if (!isset($form_state['storage']['template'])) {
  1251. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1252. $template = db_query($sql, array(':template' => $template_id))->fetchObject();
  1253. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1254. if (!$form_state['storage']['template_array']) {
  1255. $form_state['storage']['template_array'] = array();
  1256. }
  1257. $form_state['storage']['template'] = $template;
  1258. $form_state['storage']['record2priority'] = array();
  1259. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1260. if (!is_array($record_array)) {
  1261. continue;
  1262. }
  1263. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1264. }
  1265. }
  1266. else {
  1267. $template = $form_state['storage']['template'];
  1268. }
  1269. // If we are editing then get the original values
  1270. if ($mode == 'edit' && !$v['no_field_index']) {
  1271. $template_field = $form_state['storage']['template_array'][$v['record_id']]['fields'][$field_index];
  1272. $original_field = $template_field;
  1273. $form_state['storage']['original_field'] = $template_field;
  1274. $form_state['storage']['original_field']['priority'] = $priority;
  1275. $form_state['storage']['original_field']['field_index'] = $field_index;
  1276. }
  1277. $v['template_name'] = (isset($template->name)) ? $template->name : '';
  1278. if (isset($form_state['values']['field_type'])) {
  1279. $v['field_type'] = $form_state['values']['field_type'];
  1280. }
  1281. elseif (isset($original_field['type'])) {
  1282. $v['field_type'] = $original_field['type'];
  1283. }
  1284. else {
  1285. $v['field_type'] = 'table field';
  1286. }
  1287. $field_type = $v['field_type'];
  1288. // Set field type options
  1289. // First set original field values in $form_state['values'] if not already set
  1290. switch ($v['field_type']) {
  1291. case 'table field':
  1292. if (!isset($form_state['values']['column_number'])) {
  1293. $form_state['values']['column_number'] = (isset($original_field['spreadsheet column'])) ? $original_field['spreadsheet column'] : '';
  1294. $form_state['values']['column_exposed'] = (isset($original_field['exposed'])) ? $original_field['exposed'] : FALSE;
  1295. $form_state['values']['column_exposed_desc'] = (isset($original_field['exposed_description'])) ? $original_field['exposed_description'] : FALSE;
  1296. }
  1297. break;
  1298. case 'constant':
  1299. if (!isset($form_state['values']['constant_value'])) {
  1300. $form_state['values']['constant_value'] = (isset($original_field['constant value'])) ? $original_field['constant value'] : '';
  1301. $form_state['values']['constant_exposed'] = (isset($original_field['exposed'])) ? $original_field['exposed'] : FALSE;
  1302. $form_state['values']['constant_validate'] = (isset($original_field['exposed_validate'])) ? $original_field['exposed_validate'] : FALSE;
  1303. }
  1304. break;
  1305. case 'foreign key':
  1306. if (!isset($form_state['values']['foreign_record'])) {
  1307. $form_state['values']['foreign_record'] = (isset($original_field['foreign key'])) ? $original_field['foreign key'] : FALSE;
  1308. $form_state['values']['foreign_field'] = (isset($original_field['foreign field'])) ? $original_field['foreign field'] : FALSE;
  1309. }
  1310. break;
  1311. }
  1312. // Ensure other field type fields are reset/cleared except for those of the current type
  1313. // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column
  1314. // fields should be cleared since they don't apply to a constant
  1315. switch ($v['field_type']) {
  1316. case 'table field':
  1317. $v['column_number'] = (isset($form_state['values']['column_number'])) ? $form_state['values']['column_number'] : '';
  1318. $v['column_exposed'] = (isset($form_state['values']['column_exposed'])) ? $form_state['values']['column_exposed'] : FALSE;
  1319. $v['column_exposed_desc'] = (isset($form_state['values']['column_exposed_desc'])) ? $form_state['values']['column_exposed_desc'] : FALSE;
  1320. $v['constant_value'] = '';
  1321. $v['constant_exposed'] = FALSE;
  1322. $v['constant_validate'] = FALSE;
  1323. break;
  1324. case 'constant':
  1325. $v['column_number'] = '';
  1326. $v['column_exposed'] = FALSE;
  1327. $v['column_exposed_desc'] = FALSE;
  1328. $v['constant_value'] = (isset($form_state['values']['constant_value'])) ? $form_state['values']['constant_value'] : '';
  1329. $v['constant_exposed'] = (isset($form_state['values']['constant_exposed'])) ? $form_state['values']['constant_exposed'] : FALSE;
  1330. $v['constant_validate'] = (isset($form_state['values']['constant_validate'])) ? $form_state['values']['constant_validate'] : FALSE;
  1331. break;
  1332. case 'foreign key':
  1333. $v['column_number'] = '';
  1334. $v['column_exposed'] = FALSE;
  1335. $v['column_exposed_desc'] = FALSE;
  1336. $v['constant_value'] = '';
  1337. $v['constant_exposed'] = FALSE;
  1338. $v['constant_validate'] = FALSE;
  1339. break;
  1340. }
  1341. // Use the record_id as the priority/field_group
  1342. if ($v['no_record_id']) {
  1343. $form_state['values']['field_group'] = 'NEW';
  1344. $v['field_group'] = 'NEW';
  1345. }
  1346. else {
  1347. $form_state['values']['field_group'] = $record_id;
  1348. $v['field_group'] = $record_id;
  1349. if (preg_match('/\d+/', $record_id)) {
  1350. $priority = $form_state['values']['field_group'];
  1351. $table = $form_state['storage']['template_array'][$priority]['table'];
  1352. }
  1353. }
  1354. // Chado Tables and default table
  1355. $tables = tripal_core_get_chado_tables(TRUE);
  1356. if (isset($form_state['values']['chado_table'])) {
  1357. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  1358. $table = $form_state['values']['chado_table'];
  1359. }
  1360. elseif (isset($form_state['values']['record_name'])) {
  1361. $record_name = $form_state['values']['record_name'];
  1362. $priority = $form_state['storage']['record2priority'][$record_name];
  1363. $table = $form_state['storage']['template_array'][$priority]['table'];
  1364. }
  1365. else {
  1366. $priority = $form_state['values']['field_group'];
  1367. $table = $form_state['storage']['template_array'][$priority]['table'];
  1368. }
  1369. }
  1370. if (!isset($table)) {
  1371. $table = reset($tables);
  1372. }
  1373. // Chado fields
  1374. $chado_fields = array();
  1375. $table_description = tripal_core_get_chado_table_schema($table);
  1376. foreach ($table_description['fields'] as $field_name => $field_array) {
  1377. $chado_fields[$field_name] = $field_name;
  1378. }
  1379. // Chado Default Field
  1380. if (empty($chado_fields)) {
  1381. $field = NULL;
  1382. }
  1383. elseif (isset($form_state['values']['chado_field'])) {
  1384. if (isset($chado_fields[$form_state['values']['chado_field']])) {
  1385. $field = $form_state['values']['chado_field'];
  1386. }
  1387. else {
  1388. $field = current($chado_fields);
  1389. }
  1390. }
  1391. elseif (isset($original_field['field'])) {
  1392. $field = $original_field['field'];
  1393. }
  1394. else {
  1395. $field = current($chado_fields);
  1396. }
  1397. // Field group options
  1398. if (!$form_state['storage']['record2priority']) {
  1399. $groups = array();
  1400. }
  1401. else {
  1402. $groups = array_flip($form_state['storage']['record2priority']);
  1403. }
  1404. $groups['NONE'] = 'Select a Record';
  1405. $groups['NEW'] = 'New Record';
  1406. // Field Group Default
  1407. if ($record_id) {
  1408. $field_group = $record_id;
  1409. }
  1410. elseif (isset($form_state['values']['field_group'])) {
  1411. $field_group = $form_state['values']['field_group'];
  1412. }
  1413. else {
  1414. if ($mode == 'add') {
  1415. $field_group = 'NEW';
  1416. }
  1417. else {
  1418. $field_group = 'NONE';
  1419. }
  1420. }
  1421. $v['field_group'] = $field_group;
  1422. $v['field_group_options'] = $groups;
  1423. $v['chado_table'] = $table;
  1424. $v['chado_table_options'] = $tables;
  1425. $v['chado_field'] = $field;
  1426. $v['chado_field_options'] = $chado_fields;
  1427. $v['record_name'] = (isset($form_state['values']['record_name'])) ? $form_state['values']['record_name'] : '';
  1428. if (isset($form_state['values']['field_title'])) {
  1429. $v['field_title'] = $form_state['values']['field_title'];
  1430. }
  1431. elseif (isset($original_field['title'])) {
  1432. $v['field_title'] = $original_field['title'];
  1433. }
  1434. else {
  1435. $v['field_title'] = '';
  1436. }
  1437. $v['show_all_records'] = (isset($form_state['values']['show_all_records'])) ? $form_state['values']['show_all_records'] : FALSE;
  1438. // Foreign key options
  1439. $fk_options = array();
  1440. $fk_options['NULL'] = 'None';
  1441. $show_all = $v['show_all_records'];
  1442. if ($field_type == 'foreign key' and !$show_all) {
  1443. $foreign_field2table = array();
  1444. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1445. foreach ($key_array['columns'] as $left_field => $right_field) {
  1446. //$chado_fields[$left_field] = $left_field;
  1447. $foreign_field2table[$left_field] = $key_table;
  1448. }
  1449. }
  1450. // Foreign key options
  1451. $foreign_table = $foreign_field2table[$field];
  1452. if ($foreign_table) {
  1453. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1454. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  1455. $fk_options[$record_name] = $record_name;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. // the user wants to see all of the records
  1461. elseif ($field_type == 'foreign key' and $show_all) {
  1462. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1463. $fk_options[$record_name] = $record_name;
  1464. }
  1465. }
  1466. // build the list of referrer table fields. This list is used
  1467. // for the referring table field drop down. It has two groups
  1468. // one for foreign keys and another for additional fields.
  1469. $ref_chado_fields = array();
  1470. if ($field_type == 'foreign key') {
  1471. $fk_rec = $form_state['values']['foreign_record'];
  1472. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  1473. // first add in the foreign key field in the referring table
  1474. // that corresponds to this field (if one exists).
  1475. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  1476. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  1477. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1478. foreach ($key_array['columns'] as $left_field => $right_field) {
  1479. if ($key_table == $fk_table and $left_field == $field) {
  1480. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  1481. }
  1482. }
  1483. }
  1484. // now add in the remaining fields of the referring record's table.
  1485. $fk_description = tripal_core_get_chado_table_schema($fk_table);
  1486. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
  1487. // don't include the FK field it's included above
  1488. if (in_array('Foreign Key', $ref_chado_fields)) {
  1489. if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  1490. continue;
  1491. }
  1492. }
  1493. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  1494. }
  1495. }
  1496. }
  1497. $ref_chado_fields = (!empty($ref_chado_fields)) ? $ref_chado_fields : array('NULL', 'None');
  1498. $v['foreign_record_options'] = $fk_options;
  1499. $v['foreign_record'] = (isset($form_state['values']['foreign_record'])) ? $form_state['values']['foreign_record'] : 'NULL';
  1500. $v['foreign_field_options'] = $ref_chado_fields;
  1501. $v['foreign_field'] = (isset($form_state['values']['foreign_field'])) ? $form_state['values']['foreign_field'] : current($ref_chado_fields);
  1502. $v['required'] = (isset($form_state['values']['required'])) ? $form_state['values']['required'] : FALSE;
  1503. if (isset($original_field['regex']) && empty($form_state['storage']['regex']['pattern'])) {
  1504. $form_state['storage']['regex']['pattern'] = $original_field['regex']['pattern'];
  1505. $form_state['storage']['regex']['replace'] = $original_field['regex']['replace'];
  1506. }
  1507. $v['regex_are_set'] = (isset($form_state['storage']['regex']['pattern'])) ? TRUE : FALSE;
  1508. $v['regex-pattern'] = (isset($form_state['storage']['regex']['pattern'])) ? $form_state['storage']['regex']['pattern'] : array();
  1509. $v['regex-replace'] = (isset($form_state['storage']['regex']['replace'])) ? $form_state['storage']['regex']['replace'] : array();
  1510. $v['regex-test-string'] = (isset($form_state['storage']['test_regex_test'])) ? $form_state['storage']['test_regex_test'] : '';
  1511. $v['regex-test-result'] = (isset($form_state['storage']['test_regex_result'])) ? $form_state['storage']['test_regex_result'] : '';
  1512. return $v;
  1513. }
  1514. /**
  1515. * Add Field Form
  1516. *
  1517. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  1518. * can use the following in the query of the path to set defaults for a given template:
  1519. * - template_id=\d+: the template to add the field to
  1520. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  1521. *
  1522. * @param $form_state
  1523. * Contains the values and storage for the form
  1524. * @return
  1525. * A form array to be rendered by drupal_get_form
  1526. *
  1527. * @ingroup tripal_bulk_loader
  1528. */
  1529. function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
  1530. $form['#cache'] = TRUE; // Make sure the form is cached.
  1531. // if there is no template supplied don't return rest of form
  1532. if (!isset($form_state['build_info']['args'][1])) {
  1533. return $form;
  1534. }
  1535. $v['mode'] = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : 'create';
  1536. $form_state['storage']['mode'] = $v['mode'];
  1537. $mode = $v['mode'];
  1538. $values = tripal_bulk_loader_template_field_form_default_values($mode, $form_state);
  1539. // set the breadcrumb
  1540. $breadcrumb = array();
  1541. $breadcrumb[] = l('Home', '<front>');
  1542. $breadcrumb[] = l('Administration', 'admin');
  1543. $breadcrumb[] = l('Tripal', 'admin/tripal');
  1544. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  1545. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  1546. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  1547. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $values['template_id'] . '/edit');
  1548. drupal_set_breadcrumb($breadcrumb);
  1549. $form['template_name'] = array(
  1550. '#type' => 'item',
  1551. '#title' => 'Template',
  1552. '#markup' => $values['template_name'],
  1553. );
  1554. $form['template_id'] = array(
  1555. '#type' => 'hidden',
  1556. '#value' => $values['template_id'],
  1557. );
  1558. $form['fields'] = array(
  1559. '#type' => 'markup',
  1560. '#prefix' => '<div id="tripal_bulk_loader_template-template_fields">',
  1561. '#suffix' => '</div>',
  1562. );
  1563. $form['fields']['basic'] = array(
  1564. '#type' => 'markup',
  1565. '#prefix' => '<div class="basic">',
  1566. '#suffix' => '</div>'
  1567. );
  1568. $form['fields']['basic']['field_type'] = array(
  1569. '#type' => 'radios',
  1570. '#title' => t('Type of Field'),
  1571. '#options' => array(
  1572. 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
  1573. 'constant' => t('Constant: Field which remains Constant throughout the file'),
  1574. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  1575. ),
  1576. '#required' => TRUE,
  1577. '#default_value' => $values['field_type'],
  1578. '#ajax' => array(
  1579. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1580. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1581. 'effect' => 'fade'
  1582. ),
  1583. );
  1584. $form['fields']['basic']['field_group'] = array(
  1585. '#type' => 'select',
  1586. '#title' => 'Record',
  1587. '#description' => 'This is used to group a set of fields together allowing '
  1588. .'multiple records to be inserted into the same table per line of the file',
  1589. '#options' => $values['field_group_options'],
  1590. '#default_value' => $values['field_group'],
  1591. '#ajax' => array(
  1592. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1593. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1594. 'effect' => 'fade'
  1595. ),
  1596. '#required' => TRUE,
  1597. );
  1598. $form['fields']['basic']['record_name'] = array(
  1599. '#type' => ($values['no_record_id']) ? 'textfield' : 'hidden',
  1600. '#title' => 'Unique Record Name',
  1601. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  1602. '#suffix' => '</div>',
  1603. '#description' => 'A human-readable name for the record; it should be unique.',
  1604. '#default_value' => $values['record_name'],
  1605. );
  1606. $form['fields']['basic']['field_title'] = array(
  1607. '#type' => 'textfield',
  1608. '#title' => t('Human-readable Title for Field'),
  1609. '#default_value' => $values['field_title'],
  1610. );
  1611. // Chado Field
  1612. $form['fields']['chado'] = array(
  1613. '#type' => 'fieldset',
  1614. '#title' => t('Chado Field/Column Details'),
  1615. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1616. );
  1617. $form['fields']['chado']['chado_table'] = array(
  1618. '#type' => 'select',
  1619. '#title' => t('Chado Table'),
  1620. '#options' => $values['chado_table_options'],
  1621. '#default_value' => $values['chado_table'],
  1622. '#ajax' => array(
  1623. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1624. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1625. 'effect' => 'fade'
  1626. ),
  1627. );
  1628. $form['fields']['chado']['chado_field'] = array(
  1629. '#type' => 'select',
  1630. '#title' => t('Chado Field/Column'),
  1631. '#options' => $values['chado_field_options'],
  1632. '#default_value' => $values['chado_field'],
  1633. '#ajax' => array(
  1634. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1635. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1636. 'effect' => 'fade'
  1637. ),
  1638. );
  1639. // loading file data column
  1640. $form['fields']['columns'] = array(
  1641. '#type' => 'fieldset',
  1642. '#title' => t('Data File Column'),
  1643. '#collapsible' => TRUE,
  1644. '#collapsed' => ($values['field_type'] == 'table field')? FALSE : TRUE,
  1645. );
  1646. $form['fields']['columns']['column_number'] = array(
  1647. '#type' => 'textfield',
  1648. '#title' => t('Column'),
  1649. '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
  1650. '#size' => 5,
  1651. '#default_value' => $values['column_number'],
  1652. );
  1653. $form['fields']['columns']['column_exposed'] = array(
  1654. '#type' => 'checkbox',
  1655. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1656. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1657. '#default_value' => $values['column_exposed'],
  1658. );
  1659. $form['fields']['columns']['column_exposed_desc'] = array(
  1660. '#type' => 'textfield',
  1661. '#title' => t('Description for exposed field on bulk loading job'),
  1662. '#description' => t('This description should tell the user what column should be entered here.'),
  1663. '#default_value' => $values['column_exposed_desc'],
  1664. );
  1665. // Global Value
  1666. $form['fields']['constant'] = array(
  1667. '#type' => 'fieldset',
  1668. '#title' => t('Constant'),
  1669. '#collapsible' => TRUE,
  1670. '#collapsed' => ($values['field_type'] == 'constant')? FALSE : TRUE,
  1671. );
  1672. $form['fields']['constant']['constant_value'] = array(
  1673. '#type' => 'textfield',
  1674. '#title' => t('Constant Value'),
  1675. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1676. '#default_value' => $values['constant_value']
  1677. );
  1678. $form['fields']['constant']['constant_exposed'] = array(
  1679. '#type' => 'checkbox',
  1680. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1681. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1682. '#default_value' => $values['constant_exposed'],
  1683. );
  1684. $form['fields']['constant']['constant_validate'] = array(
  1685. '#type' => 'checkbox',
  1686. '#title' => t('Ensure value is in table'),
  1687. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1688. '#default_value' => $values['constant_validate'],
  1689. );
  1690. // Foreign Key / Referrer
  1691. $form['fields']['foreign_key'] = array(
  1692. '#type' => 'fieldset',
  1693. '#title' => 'Record Referral',
  1694. '#collapsible' => TRUE,
  1695. '#collapsed' => ($values['field_type'] == 'foreign key')? FALSE : TRUE,
  1696. );
  1697. $form['fields']['foreign_key']['show_all_records'] = array(
  1698. '#type' => 'checkbox',
  1699. '#title' => 'Refer to any record',
  1700. '#description' => t('By default, the bulk loader will only allow referral to records in a foreign key relationship. To allow referral to any previous record, check this box'),
  1701. '#default_value' => $values['show_all_records'],
  1702. '#ajax' => array(
  1703. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1704. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1705. 'effect' => 'fade'
  1706. ),
  1707. );
  1708. $form['fields']['foreign_key']['foreign_record'] = array(
  1709. '#type' => 'select',
  1710. '#title' => 'Record to refer to',
  1711. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1712. '#options' => $values['foreign_record_options'],
  1713. '#ajax' => array(
  1714. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1715. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1716. 'effect' => 'fade'
  1717. ),
  1718. '#default_value' => $values['foreign_record'],
  1719. );
  1720. $form['fields']['foreign_key']['foreign_field'] = array(
  1721. '#type' => 'select',
  1722. '#title' => 'Field to refer to',
  1723. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1724. '#options' => $values['foreign_field_options'],
  1725. '#default_value' => $values['foreign_field'],
  1726. );
  1727. $form['fields']['additional'] = array(
  1728. '#type' => 'fieldset',
  1729. '#title' => 'Additional Options',
  1730. '#collapsible' => TRUE,
  1731. '#collapsed' => FALSE
  1732. );
  1733. $form['fields']['additional']['required'] = array(
  1734. '#type' => 'checkbox',
  1735. '#title' => 'Make this field required',
  1736. '#default_value' => $values['required'],
  1737. );
  1738. $form['fields']['additional']['regex_transform'] = array(
  1739. '#type' => 'fieldset',
  1740. '#title' => 'Transform Data File Value Rules',
  1741. '#collapsible' => TRUE,
  1742. '#collapsed' => FALSE//$values['regex_are_set'],
  1743. );
  1744. $form['fields']['additional']['regex_transform']['regex_description'] = array(
  1745. '#type' => 'item',
  1746. '#markup' => 'A transformation rule allows you to transform the original value '
  1747. .'(usually from a user submitted data file) into the form you would like it stored '
  1748. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1749. .'which determines which replacement patterns are applied and captures regions of the '
  1750. .'original value) and a replacement pattern (a string which may contain capture references '
  1751. .'that describes what the new value should be). Each rule is applied to the result of the '
  1752. .'previous rule.'
  1753. );
  1754. $form['fields']['additional']['regex_transform']['regex-data'] = array(
  1755. '#tree' => TRUE,
  1756. '#theme' => 'tripal_bulk_loader_field_regex_fieldset'
  1757. );
  1758. foreach ($values['regex-pattern'] as $index => $pattern) {
  1759. $data_element = array(
  1760. 'pattern' => array(
  1761. '#type' => 'item',
  1762. '#markup' => check_plain($pattern),
  1763. ),
  1764. 'replace' => array(
  1765. '#type' => 'item',
  1766. '#markup' => check_plain($values['regex-replace'][$index]),
  1767. ),
  1768. 'old_index' => array(
  1769. '#type' => 'hidden',
  1770. '#value' => $index,
  1771. ),
  1772. 'new_index' => array(
  1773. '#type' => 'select',
  1774. '#options' => range(0, sizeof($values['regex-pattern'])-1),
  1775. '#default_value' => $index,
  1776. '#attributes' => array('class' => array('rank-weight')), // needed for table dragging
  1777. ),
  1778. 'id' => array(
  1779. '#type' => 'hidden',
  1780. '#value' => $index,
  1781. ),
  1782. 'submit-delete' => array(
  1783. '#type' => 'submit',
  1784. '#value' => 'Delete Transformation',
  1785. '#name' => $index,
  1786. ),
  1787. );
  1788. $form['fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1789. }
  1790. $form['fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1791. '#type' => ($values['regex_are_set']) ? 'submit' : 'hidden',
  1792. '#value' => 'Save Transformation Rule Order'
  1793. );
  1794. $form['fields']['additional']['regex_transform']['new_regex'] = array(
  1795. '#type' => 'fieldset',
  1796. '#title' => 'Add a new Transformation Rule',
  1797. );
  1798. $form['fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1799. '#type' => 'textfield',
  1800. '#title' => 'Match Pattern',
  1801. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1802. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1803. .'pattern get applied to the value. To capture a section of your value for use in the '
  1804. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1805. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1806. .' To match and capture any value use <i>.*</i>',
  1807. );
  1808. $form['fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1809. '#type' => 'textfield',
  1810. '#title' => 'Replacement Pattern',
  1811. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1812. .'mentioned above. It can include references of the form \n where n is the number of the '
  1813. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1814. .'first set of round brackets.',
  1815. );
  1816. if ($values['field_type'] == 'table field') {
  1817. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1818. $form['fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1819. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1820. .'This allows you to substitute other data file values into the current field. For example, '
  1821. .'if you had the following line:<br />'
  1822. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1823. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1824. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1825. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1826. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1827. }
  1828. $form['fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1829. '#type' => 'submit',
  1830. '#value' => 'Add Transformation',
  1831. );
  1832. $form['fields']['additional']['regex_transform']['test_regex'] = array(
  1833. '#type' => 'fieldset',
  1834. '#title' => 'Test Transformation Rules',
  1835. );
  1836. $form['fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1837. '#type' => 'textfield',
  1838. '#title' => 'Test Value',
  1839. '#description' => 'This should be a value that you expect the above transformation rules '
  1840. .'to be applied to.',
  1841. '#default_value' => $values['regex-test-string'],
  1842. );
  1843. $form['fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1844. '#type' => 'textfield',
  1845. '#title' => 'Test Result',
  1846. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1847. .'riles were applied to the Test Value.',
  1848. '#default_value' => $values['regex-test-result'],
  1849. );
  1850. $form['fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1851. '#type' => 'submit',
  1852. '#value' => 'Test Transformation Rules'
  1853. );
  1854. $form['submit-save'] = array(
  1855. '#type' => 'submit',
  1856. '#value' => 'Save Changes'
  1857. );
  1858. $form['submit-cancel'] = array(
  1859. '#type' => 'submit',
  1860. '#value' => 'Cancel'
  1861. );
  1862. return $form;
  1863. }
  1864. /**
  1865. * Implements hook_form_validate().
  1866. *
  1867. * @ingroup tripal_bulk_loader
  1868. */
  1869. function tripal_bulk_loader_template_field_form_validate($form, $form_state) {
  1870. // Don't worry about validation when Cancel button is clicked
  1871. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1872. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1873. $form_state['values']['record_name'],
  1874. $form_state['values']['template_id'],
  1875. $form_state['storage']['template_array']
  1876. );
  1877. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1878. if ((!$is_unique) AND $new_record) {
  1879. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  1880. }
  1881. }
  1882. }
  1883. /**
  1884. * Add Field Submit
  1885. *
  1886. * @param $form
  1887. * The form that was submitted
  1888. * @param $form_state
  1889. * The values and storage for the form
  1890. *
  1891. * @ingroup tripal_bulk_loader
  1892. */
  1893. function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
  1894. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1895. if ($op == 'Save Changes') {
  1896. $template = $form_state['storage']['template_array'];
  1897. // If new record
  1898. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1899. $record_name = $form_state['values']['record_name'];
  1900. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1901. $record2priority[$record_name] = $priority;
  1902. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1903. $template[$priority]['record_id'] = $record_name;
  1904. }
  1905. else {
  1906. $priority = $form_state['values']['field_group'];
  1907. }
  1908. // Add field to template array
  1909. if ($form_state['values']['field_type'] == 'table field') {
  1910. $field = array(
  1911. 'type' => 'table field',
  1912. 'title' => $form_state['values']['field_title'],
  1913. 'field' => $form_state['values']['chado_field'],
  1914. 'required' => $form_state['values']['required'],
  1915. //'allowed values' => empty by default,
  1916. 'spreadsheet column' => $form_state['values']['column_number'],
  1917. 'exposed' => $form_state['values']['column_exposed'],
  1918. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1919. );
  1920. }
  1921. elseif ($form_state['values']['field_type'] == 'constant') {
  1922. $field = array(
  1923. 'type' => 'constant',
  1924. 'title' => $form_state['values']['field_title'],
  1925. 'field' => $form_state['values']['chado_field'],
  1926. 'required' => $form_state['values']['required'],
  1927. //'allowed values' => empty by default,
  1928. 'constant value' => $form_state['values']['constant_value'],
  1929. 'exposed' => $form_state['values']['constant_exposed'],
  1930. 'exposed_validate' => $form_state['values']['constant_validate'],
  1931. );
  1932. }
  1933. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1934. $field = array(
  1935. 'type' => 'foreign key',
  1936. 'title' => $form_state['values']['field_title'],
  1937. 'field' => $form_state['values']['chado_field'],
  1938. 'show_all_records' => $form_state['values']['show_all_records'],
  1939. 'foreign key' => $form_state['values']['foreign_record'],
  1940. 'foreign field' => $form_state['values']['foreign_field'],
  1941. 'required' => $form_state['values']['required'],
  1942. );
  1943. }
  1944. // Deal with any additional options
  1945. if (isset($form_state['storage']['regex'])) {
  1946. $field['regex'] = $form_state['storage']['regex'];
  1947. }
  1948. // Save Template
  1949. if ($form_state['storage']['mode'] == 'create') {
  1950. $template[$priority]['fields'][] = $field;
  1951. }
  1952. else {
  1953. $template[$priority]['fields'][$form_state['storage']['field_index']] = $field;
  1954. }
  1955. $form_state['storage']['template']->template_array = serialize($template);
  1956. $form_state['storage']['template']->changed = time();
  1957. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1958. if ($success) {
  1959. drupal_set_message(t('Successfully Added Field to Template'));
  1960. drupal_set_message(t('Template Saved.'));
  1961. $form_state['rebuild'] = FALSE;
  1962. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  1963. }
  1964. else {
  1965. drupal_set_message(t('Unable to Save Template!'), 'error');
  1966. watchdog('T_bulk_loader',
  1967. 'Unable to save bulk loader template: %template',
  1968. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1969. WATCHDOG_ERROR
  1970. );
  1971. }
  1972. }
  1973. elseif ($op == 'Cancel') {
  1974. $form_state['rebuild'] = FALSE;
  1975. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  1976. }
  1977. elseif ($op == 'Add Transformation') {
  1978. // Add transformation rule to original field
  1979. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1980. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1981. $form_state['rebuild'] = TRUE;
  1982. drupal_set_message(t('Successfully Added Transformation Rule'));
  1983. }
  1984. elseif ($op == 'Save Transformation Rule Order') {
  1985. // Generate new regex array
  1986. $new_regex = array();
  1987. $old_regex = $form_state['storage']['regex'];
  1988. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1989. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1990. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1991. }
  1992. // sort new regex arrays
  1993. ksort($new_regex['pattern']);
  1994. ksort($new_regex['replace']);
  1995. $form_state['storage']['regex'] = $new_regex;
  1996. }
  1997. elseif ($op == 'Delete Transformation') {
  1998. // Unset regex rule
  1999. $index = $form_state['clicked_button']['#name'];
  2000. unset($form_state['storage']['regex']['pattern'][$index]);
  2001. unset($form_state['storage']['regex']['replace'][$index]);
  2002. }
  2003. elseif ($op == 'Test Transformation Rules') {
  2004. $patterns = $form_state['storage']['regex']['pattern'];
  2005. $replaces = $form_state['storage']['regex']['replace'];
  2006. $test_string = $form_state['values']['test_string'];
  2007. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  2008. $form_state['storage']['test_regex_test'] = $test_string;
  2009. }
  2010. }
  2011. /**
  2012. * Delete Field Form
  2013. *
  2014. * This form is meant to be called from a bulk loader form
  2015. *
  2016. * @param $form_state
  2017. * Contains the values and storage for the form
  2018. * @return
  2019. * A form array to be rendered by drupal_get_form
  2020. *
  2021. * @ingroup tripal_bulk_loader
  2022. */
  2023. function tripal_bulk_loader_delete_template_field_form($form, &$form_state) {
  2024. $form['#cache'] = TRUE; // Make sure the form is cached.
  2025. // get args from path
  2026. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  2027. $form_state['storage']['template_id'] = $template_id;
  2028. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  2029. $form_state['storage']['record_id'] = $record_id;
  2030. $field_id = (isset($form_state['build_info']['args'][2])) ? $form_state['build_info']['args'][2] : FALSE;
  2031. $form_state['storage']['field_id'] = $field_id;
  2032. // if there is no template supplied don't return rest of form
  2033. if (!$template_id) {
  2034. return $form;
  2035. }
  2036. // set the breadcrumb
  2037. $breadcrumb = array();
  2038. $breadcrumb[] = l('Home', '<front>');
  2039. $breadcrumb[] = l('Administration', 'admin');
  2040. $breadcrumb[] = l('Tripal', 'admin/tripal');
  2041. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  2042. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  2043. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  2044. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  2045. drupal_set_breadcrumb($breadcrumb);
  2046. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  2047. $_GET['field_name'] = (isset($_GET['field_name'])) ? $_GET['field_name'] : '';
  2048. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  2049. $_GET['chado_field'] = (isset($_GET['chado_field'])) ? $_GET['chado_field'] : '';
  2050. $_GET['data_column'] = (isset($_GET['data_column'])) ? $_GET['data_column'] : '';
  2051. $_GET['constant_value'] = (isset($_GET['constant_value'])) ? $_GET['constant_value'] : '';
  2052. $_GET['foreign_record'] = (isset($_GET['foreign_record'])) ? $_GET['foreign_record'] : '';
  2053. $description = '';
  2054. $description .= '<table>';
  2055. $description .= '<tr><th>Record Name</th><th>Field Name</th><th>Chado Table</th><th>Chado Field</th><th>Data Column</th><th>Constant Value</th><th>Foreign Key</th></tr>';
  2056. $description .= '<tr><td>' . $_GET['record_name']
  2057. . '</td><td>' . $_GET['field_name']
  2058. . '</td><td>' . $_GET['chado_table']
  2059. . '</td><td>' . $_GET['chado_field']
  2060. . '</td><td>' . $_GET['data_column']
  2061. . '</td><td>' . $_GET['constant_value']
  2062. . '</td><td>' . $_GET['foreign_record']
  2063. . '</td></tr>';
  2064. $description .= '</table>';
  2065. $description .= '<p><strong>Are you sure you want to delete this field?</strong></p>';
  2066. $yes = 'Delete Field';
  2067. $no = 'Cancel';
  2068. $form['#attributes']['class'][] = 'confirmation';
  2069. $form['description'] = array('#markup' => $description);
  2070. $form['actions'] = array('#type' => 'actions');
  2071. $form['actions']['submit'] = array(
  2072. '#type' => 'submit',
  2073. '#value' => $yes ? $yes : t('Confirm'),
  2074. );
  2075. $form['actions']['cancel'] = array(
  2076. '#type' => 'link',
  2077. '#title' => $no ? $no : t('Cancel'),
  2078. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  2079. );
  2080. // By default, render the form using theme_confirm_form().
  2081. if (!isset($form['#theme'])) {
  2082. $form['#theme'] = 'confirm_form';
  2083. }
  2084. return $form;
  2085. }
  2086. /**
  2087. * Delete Field Form
  2088. *
  2089. * This form is meant to be called from a bulk loader form
  2090. *
  2091. * @param $form_state
  2092. * Contains the values and storage for the form
  2093. * @return
  2094. * A form array to be rendered by drupal_get_form
  2095. *
  2096. * @ingroup tripal_bulk_loader
  2097. */
  2098. function tripal_bulk_loader_delete_template_field_form_submit($form, &$form_state) {
  2099. $form_state['rebuild'] = FALSE;
  2100. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  2101. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  2102. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  2103. $form_state['storage']['template'] = unserialize($result->template_array);
  2104. $new_template = tripal_bulk_loader_delete_field(
  2105. $form_state['storage']['record_id'],
  2106. $form_state['storage']['field_id'],
  2107. $form_state['storage']['template']
  2108. );
  2109. if (!empty($new_template)) {
  2110. $form_state['storage']['template'] = $new_template;
  2111. }
  2112. drupal_set_message(t('Deleted Field from Template.'));
  2113. $record = array(
  2114. 'template_id' => $form_state['storage']['template_id'],
  2115. 'template_array' => serialize($form_state['storage']['template']),
  2116. 'changed' => time()
  2117. );
  2118. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  2119. drupal_set_message(t('Template Saved.'));
  2120. }
  2121. /**
  2122. * @section
  2123. * AHAH Callbacks
  2124. */
  2125. /**
  2126. * AHAH Function: Replace $form['fields'] in tripal_bulk_loader_add_template_field_form
  2127. *
  2128. * @return
  2129. * JSON Data printed to the screen
  2130. *
  2131. * @ingroup tripal_bulk_loader
  2132. */
  2133. function tripal_bulk_loader_template_fields_ahah($form, $form_state) {
  2134. return $form['fields'];
  2135. }