tripal_bulk_loader.admin.templates.inc 91 KB

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