tripal_bulk_loader.admin.templates.inc 89 KB

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