tripal_bulk_loader.admin.templates.inc 94 KB

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