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. */
  742. $form['edit_record']['optional'] = array(
  743. '#type' => 'checkbox',
  744. '#title' => t('Optional'),
  745. '#description' => t('The insert, update or select will only be performed only if all required data are present'),
  746. '#default_value' => $optional
  747. );
  748. $form['edit_record']['disable'] = array(
  749. '#type' => 'checkbox',
  750. '#title' => t('Disable this record'),
  751. '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"),
  752. '#default_value' => $disable,
  753. );
  754. $form['edit_record']['submit-edit_record'] = array(
  755. '#type' => 'submit',
  756. '#value' => 'Edit Record'
  757. );
  758. $form['edit_record']['submit-cancel'] = array(
  759. '#type' => 'submit',
  760. '#value' => 'Cancel'
  761. );
  762. return $form;
  763. }
  764. /**
  765. * Edit Record Form Submit
  766. *
  767. * @param $form
  768. * The form that was submitted
  769. * @param $form_state
  770. * Contains the values and storage for the form
  771. */
  772. function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
  773. //dpm($form_state, 'form state -start submit');
  774. if (!$form_state['ahah_submission']) {
  775. if ($form_state['values']['op'] == 'Edit Record') {
  776. $template = $form_state['storage']['template_array'];
  777. // Edit Record
  778. $record = $template[ $form_state['storage']['original_priority'] ];
  779. $record['record_id'] = $form_state['values']['record_name'];
  780. $record['mode'] = $form_state['values']['mode'];
  781. $record['table'] = $form_state['values']['chado_table'];
  782. $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate'];
  783. $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate'];
  784. $record['select_optional'] = $form_state['values']['select_optional'];
  785. $record['disable'] = $form_state['values']['disable'];
  786. $record['optional'] = $form_state['values']['optional'];
  787. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  788. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  789. $template[ $form_state['values']['field_group'] ] = $record;
  790. unset($template[ $form_state['storage']['original_priority'] ]);
  791. }
  792. else {
  793. $template[ $form_state['storage']['original_priority'] ] = $record;
  794. }
  795. // Save Template
  796. $form_state['storage']['template']->template_array = serialize($template);
  797. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  798. if ($success) {
  799. drupal_set_message(t('Successfully Updated Template Record'));
  800. drupal_set_message(t('Template Saved.'));
  801. $path = explode('?', $form_state['storage']['referring URL']);
  802. parse_str($path[1], $query);
  803. $query['template_id'] = $form_state['storage']['template']->template_id;
  804. drupal_goto($path[0], $query);
  805. }
  806. else {
  807. drupal_set_message(t('Unable to Save Template!'), 'error');
  808. watchdog('T_bulk_loader',
  809. 'Unable to save bulk loader template: %template',
  810. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  811. WATCHDOG_ERROR
  812. );
  813. }
  814. }
  815. elseif ($form_state['values']['op'] == 'Cancel') {
  816. $path = explode('?', $form_state['storage']['referring URL']);
  817. parse_str($path[1], $query);
  818. $query['template_id'] = $form_state['storage']['template']->template_id;
  819. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  820. drupal_goto($path[0], $query);
  821. }
  822. }
  823. }
  824. /**
  825. * @section
  826. * Add/Edit Field Forms
  827. */
  828. /**
  829. * Add Field Form
  830. *
  831. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  832. * can use the following in the query of the path to set defaults for a given template:
  833. * - template_id=\d+: the template to add the field to
  834. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  835. *
  836. * @param $form_state
  837. * Contains the values and storage for the form
  838. * @return
  839. * A form array to be rendered by drupal_get_form
  840. */
  841. function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
  842. $form = array();
  843. $form['#cache'] = TRUE; // Make sure the form is cached.
  844. // get template id from path
  845. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  846. // if there is no template supplied don't return rest of form
  847. if (!$template_id) {
  848. return $form;
  849. }
  850. // Pre-set Variables needed for form proper------------------------------------------
  851. // If this is the first load of the form (no form state) we need to initialize some variables
  852. if (!$form_state['storage']['template']) {
  853. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  854. $template = db_fetch_object(db_query($sql, $template_id));
  855. $form_state['storage']['template_array'] = unserialize($template->template_array);
  856. $form_state['storage']['template'] = $template;
  857. $form_state['storage']['record2priority'] = array();
  858. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  859. if (!is_array($record_array)) {
  860. continue;
  861. }
  862. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  863. }
  864. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  865. }
  866. else {
  867. $template = $form_state['storage']['template'];
  868. }
  869. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  870. // Tables and default table
  871. $tables = tripal_core_get_chado_tables(TRUE);
  872. if ($form_state['values']) {
  873. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  874. $table = $form_state['values']['chado_table'];
  875. }
  876. elseif ($form_state['values']['record_name']) {
  877. $record_name = $form_state['values']['record_name'];
  878. $priority = $form_state['storage']['record2priority'][$record_name];
  879. $table = $form_state['storage']['template_array'][$priority]['table'];
  880. }
  881. else {
  882. $priority = $form_state['values']['field_group'];
  883. $table = $form_state['storage']['template_array'][$priority]['table'];
  884. }
  885. }
  886. if (!$table) {
  887. $table = reset($tables);
  888. }
  889. // get the record_id from the path
  890. if ($_GET['record_id'] !== NULL) {
  891. $form_state['values']['field_group'] = $_GET['record_id'];
  892. if (preg_match('/\d+/', $_GET['record_id'])) {
  893. $priority = $form_state['values']['field_group'];
  894. $table = $form_state['storage']['template_array'][$priority]['table'];
  895. }
  896. }
  897. $show_all = $form_state['values']['show_all_records'];
  898. $table_description = tripal_core_get_chado_table_schema($table);
  899. // Fields and foreign key / referral mappings
  900. // build the fields array
  901. $chado_fields = array();
  902. foreach ($table_description['fields'] as $field_name => $field_array) {
  903. $chado_fields[$field_name] = $field_name;
  904. }
  905. $fk_options = array();
  906. $fk_options['NULL'] = 'None';
  907. if ($field_type == 'foreign key' and !$show_all) {
  908. $foreign_field2table = array();
  909. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  910. foreach ($key_array['columns'] as $left_field => $right_field) {
  911. //$chado_fields[$left_field] = $left_field;
  912. $foreign_field2table[$left_field] = $key_table;
  913. }
  914. }
  915. // reset($chado_fields);
  916. // set default field
  917. if (empty($chado_fields)) {
  918. $field = NULL;
  919. }
  920. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  921. $field = $form_state['values']['chado_field'];
  922. }
  923. else {
  924. $field = current($chado_fields);
  925. }
  926. // Foreign key options
  927. $foreign_table = $foreign_field2table[$field];
  928. if ($foreign_table) {
  929. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  930. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  931. $fk_options[$record_name] = $record_name;
  932. }
  933. }
  934. }
  935. }
  936. // the user wants to see all of the records
  937. elseif ($field_type == 'foreign key' and $show_all) {
  938. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  939. $fk_options[$record_name] = $record_name;
  940. }
  941. }
  942. // build the list of referrer table fields. This list is used
  943. // for the referring table field drop down. It has two groups
  944. // one for foreign keys and another for additional fields.
  945. $ref_chado_fields = array();
  946. if ($field_type == 'foreign key') {
  947. $fk_rec = $form_state['values']['foreign_record'];
  948. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  949. // first add in the foreign key field in the referring table
  950. // that corresponds to this field (if one exists).
  951. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  952. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  953. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  954. foreach ($key_array['columns'] as $left_field => $right_field) {
  955. if($key_table == $fk_table and $left_field == $field){
  956. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  957. }
  958. }
  959. }
  960. // now add in the remaining fields of the referring record's table.
  961. $fk_description = tripal_core_get_chado_table_schema($fk_table);
  962. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray){
  963. // don't include the FK field it's included above
  964. if (in_array('Foreign Key', $ref_chado_fields)) {
  965. if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  966. continue;
  967. }
  968. }
  969. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  970. }
  971. }
  972. }
  973. // Start of Form Proper-------------------------------------------------------------
  974. $form['template_name'] = array(
  975. '#type' => 'item',
  976. '#title' => 'Template',
  977. '#value' => $template->name,
  978. );
  979. $form['template_id'] = array(
  980. '#type' => 'hidden',
  981. '#value' => $template_id,
  982. );
  983. $form['add_fields'] = array(
  984. '#type' => 'fieldset',
  985. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  986. '#suffix' => '</div>',
  987. );
  988. $form['add_fields']['field_type'] = array(
  989. '#type' => 'radios',
  990. '#title' => t('Type of Field'),
  991. '#options' => array(
  992. 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
  993. 'constant' => t('Constant: Field which remains Constant throughout the file'),
  994. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  995. ),
  996. '#required' => TRUE,
  997. '#default_value' => $field_type,
  998. '#ahah' => array(
  999. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1000. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1001. 'effect' => 'fade'
  1002. ),
  1003. );
  1004. // check template array for records then add one more
  1005. if (!$form_state['storage']['record2priority']) {
  1006. $groups = array();
  1007. }
  1008. else {
  1009. $groups = array_flip($form_state['storage']['record2priority']);
  1010. }
  1011. $groups['NONE'] = 'Select a Record';
  1012. $groups['NEW'] = 'New Record';
  1013. $form['add_fields']['field_group'] = array(
  1014. '#type' => 'select',
  1015. '#title' => 'Record',
  1016. '#description' => 'This is used to group a set of fields together allowing '
  1017. .'multiple records to be inserted into the same table per line of the file',
  1018. '#options' => $groups,
  1019. '#default_value' => (preg_match('/\w+.*/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  1020. '#ahah' => array(
  1021. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1022. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1023. 'effect' => 'fade'
  1024. ),
  1025. '#required' => TRUE,
  1026. );
  1027. $form['add_fields']['record_name'] = array(
  1028. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1029. '#title' => 'Unique Record Name',
  1030. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  1031. '#suffix' => '</div>',
  1032. '#default_value' => $form_state['values']['record_name'],
  1033. );
  1034. $form['add_fields']['field_title'] = array(
  1035. '#type' => 'textfield',
  1036. '#title' => t('Human-readable Title for Field'),
  1037. '#default_value' => $form_state['values']['field_title'],
  1038. );
  1039. // Chado Field
  1040. $form['add_fields']['chado'] = array(
  1041. '#type' => 'fieldset',
  1042. '#title' => t('Chado Field/Column Details'),
  1043. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1044. );
  1045. $form['add_fields']['chado']['chado_table'] = array(
  1046. '#type' => 'select',
  1047. '#title' => t('Chado Table'),
  1048. '#options' => $tables,
  1049. '#default_value' => $table,
  1050. '#ahah' => array(
  1051. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1052. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1053. 'effect' => 'fade'
  1054. ),
  1055. );
  1056. $form['add_fields']['chado']['chado_field'] = array(
  1057. '#type' => 'select',
  1058. '#title' => t('Chado Field/Column'),
  1059. '#options' => $chado_fields,
  1060. '#default_value' => $form_state['values']['chado_field'],
  1061. '#ahah' => array(
  1062. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1063. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1064. 'effect' => 'fade'
  1065. ),
  1066. );
  1067. // loading file data column
  1068. $form['add_fields']['columns'] = array(
  1069. '#type' => 'fieldset',
  1070. '#title' => t('Data File Column'),
  1071. '#collapsible' => TRUE,
  1072. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1073. );
  1074. /**
  1075. $form['add_fields']['columns']['sheet_name'] = array(
  1076. '#type' => 'textfield',
  1077. '#title' => t('Worksheet'),
  1078. '#description' => t('Specify the name of the worksheet.'),
  1079. '#size' => 5,
  1080. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  1081. );
  1082. */
  1083. $form['add_fields']['columns']['column_number'] = array(
  1084. '#type' => 'textfield',
  1085. '#title' => t('Column'),
  1086. '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
  1087. '#size' => 5,
  1088. '#default_value' => $form_state['values']['column_number'],
  1089. );
  1090. $form['add_fields']['columns']['column_exposed'] = array(
  1091. '#type' => 'checkbox',
  1092. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1093. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1094. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1095. );
  1096. $form['add_fields']['columns']['column_exposed_desc'] = array(
  1097. '#type' => 'textfield',
  1098. '#title' => t('Description for exposed field on bulk loading job'),
  1099. '#description' => t('This description should tell the user what column should be entered here.'),
  1100. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1101. );
  1102. // Global Value
  1103. $form['add_fields']['constant'] = array(
  1104. '#type' => 'fieldset',
  1105. '#title' => t('Constant'),
  1106. '#collapsible' => TRUE,
  1107. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1108. );
  1109. $form['add_fields']['constant']['constant_value'] = array(
  1110. '#type' => 'textfield',
  1111. '#title' => t('Constant Value'),
  1112. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1113. '#default_value' => $form_state['values']['constant_value']
  1114. );
  1115. $form['add_fields']['constant']['constant_exposed'] = array(
  1116. '#type' => 'checkbox',
  1117. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1118. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.')
  1119. );
  1120. $form['add_fields']['constant']['constant_validate'] = array(
  1121. '#type' => 'checkbox',
  1122. '#title' => t('Ensure value is in table'),
  1123. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1124. );
  1125. // Foreign Key / Referrer
  1126. $form['add_fields']['foreign_key'] = array(
  1127. '#type' => 'fieldset',
  1128. '#title' => 'Record Referral',
  1129. '#collapsible' => TRUE,
  1130. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1131. );
  1132. $form['add_fields']['foreign_key']['show_all_records'] = array(
  1133. '#type' => 'checkbox',
  1134. '#title' => 'Refer to any record',
  1135. '#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'),
  1136. '#default_value' => $show_all,
  1137. '#ahah' => array(
  1138. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1139. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1140. 'effect' => 'fade'
  1141. ),
  1142. );
  1143. $form['add_fields']['foreign_key']['foreign_record'] = array(
  1144. '#type' => 'select',
  1145. '#title' => 'Record to refer to',
  1146. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1147. '#options' => $fk_options,
  1148. '#ahah' => array(
  1149. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1150. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1151. 'effect' => 'fade'
  1152. ),
  1153. '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
  1154. );
  1155. $form['add_fields']['foreign_key']['foreign_field'] = array(
  1156. '#type' => 'select',
  1157. '#title' => 'Field to refer to',
  1158. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1159. '#options' => $ref_chado_fields,
  1160. '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign_field'],
  1161. );
  1162. $form['add_fields']['additional'] = array(
  1163. '#type' => 'fieldset',
  1164. '#title' => 'Additional Options',
  1165. );
  1166. $form['add_fields']['additional']['required'] = array(
  1167. '#type' => 'checkbox',
  1168. '#title' => 'Make this file required',
  1169. );
  1170. $form['add_fields']['additional']['regex_transform'] = array(
  1171. '#type' => 'fieldset',
  1172. '#title' => 'Transform Data File Value Rules',
  1173. '#collapsible' => TRUE,
  1174. '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
  1175. );
  1176. $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
  1177. '#type' => 'item',
  1178. '#value' => 'A transformation rule allows you to transform the original value '
  1179. .'(usually from a user submitted data file) into the form you would like it stored '
  1180. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1181. .'which determines which replacement patterns are applied and captures regions of the '
  1182. .'original value) and a replacement pattern (a string which may contain capture references '
  1183. .'that describes what the new value should be). Each rule is applied to the result of the '
  1184. .'previous rule.'
  1185. );
  1186. $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
  1187. '#tree' => TRUE,
  1188. );
  1189. if (!$form_state['storage']['regex']['pattern']) {
  1190. $form_state['storage']['regex']['pattern'] = array();
  1191. }
  1192. foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
  1193. $data_element = array(
  1194. 'pattern' => array(
  1195. '#type' => 'item',
  1196. '#value' => $pattern,
  1197. ),
  1198. 'replace' => array(
  1199. '#type' => 'item',
  1200. '#value' => $form_state['storage']['regex']['replace'][$index],
  1201. ),
  1202. 'old_index' => array(
  1203. '#type' => 'hidden',
  1204. '#value' => $index,
  1205. ),
  1206. 'new_index' => array(
  1207. '#type' => 'select',
  1208. '#options' => range(0, sizeof($form_state['storage']['regex']['pattern'])-1),
  1209. '#default_value' => $index,
  1210. ),
  1211. 'id' => array(
  1212. '#type' => 'hidden',
  1213. '#value' => $index,
  1214. ),
  1215. 'submit-delete' => array(
  1216. '#type' => 'submit',
  1217. '#value' => 'Delete Transformation',
  1218. '#name' => $index,
  1219. ),
  1220. );
  1221. $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1222. }
  1223. $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1224. '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
  1225. '#value' => 'Save Transformation Rule Order'
  1226. );
  1227. $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
  1228. '#type' => 'fieldset',
  1229. '#title' => 'Add a new Transformation Rule',
  1230. );
  1231. $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1232. '#type' => 'textfield',
  1233. '#title' => 'Match Pattern',
  1234. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1235. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1236. .'pattern get applied to the value. To capture a section of your value for use in the '
  1237. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1238. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1239. .' To match and capture any value use <i>.*</i>',
  1240. );
  1241. $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1242. '#type' => 'textfield',
  1243. '#title' => 'Replacement Pattern',
  1244. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1245. .'mentioned above. It can include references of the form \n where n is the number of the '
  1246. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1247. .'first set of round brackets.',
  1248. );
  1249. if ($field_type == 'table field') {
  1250. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1251. $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1252. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1253. .'This allows you to substitute other data file values into the current field. For example, '
  1254. .'if you had the following line:<br />'
  1255. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1256. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1257. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1258. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1259. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1260. }
  1261. $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1262. '#type' => 'submit',
  1263. '#value' => 'Add Transformation',
  1264. );
  1265. $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
  1266. '#type' => 'fieldset',
  1267. '#title' => 'Test Transformation Rules',
  1268. );
  1269. $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1270. '#type' => 'textfield',
  1271. '#title' => 'Test Value',
  1272. '#description' => 'This should be a value that you expect the above transformation rules '
  1273. .'to be applied to.',
  1274. '#default_value' => $form_state['storage']['test_regex_test'],
  1275. );
  1276. $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1277. '#type' => 'textfield',
  1278. '#title' => 'Test Result',
  1279. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1280. .'riles were applied to the Test Value.',
  1281. '#default_value' => $form_state['storage']['test_regex_result'],
  1282. );
  1283. $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1284. '#type' => 'submit',
  1285. '#value' => 'Test Transformation Rules'
  1286. );
  1287. $form['add_fields']['submit-add_field'] = array(
  1288. '#type' => 'submit',
  1289. '#value' => 'Save Changes'
  1290. );
  1291. $form['add_fields']['submit-cancel'] = array(
  1292. '#type' => 'submit',
  1293. '#value' => 'Cancel'
  1294. );
  1295. return $form;
  1296. }
  1297. function tripal_bulk_loader_add_template_field_form_validate($form, $form_state) {
  1298. // Don't worry about validation when Cancel button is clicked
  1299. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1300. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1301. $form_state['values']['record_name'],
  1302. $form_state['values']['template_id'],
  1303. $form_state['storage']['template_array']
  1304. );
  1305. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1306. if ((!$is_unique) AND $new_record) {
  1307. form_set_error('record_name', "New Record Name must be unique. '".$form_state['values']['record_name']."' is not unique.");
  1308. }
  1309. }
  1310. }
  1311. /**
  1312. * Add Field Submit
  1313. *
  1314. * @param $form
  1315. * The form that was submitted
  1316. * @param $form_state
  1317. * The values and storage for the form
  1318. */
  1319. function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state) {
  1320. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1321. if (!$form_state['ahah_submission']) {
  1322. if ($op == 'Save Changes') {
  1323. $template = $form_state['storage']['template_array'];
  1324. // If new record
  1325. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1326. $record_name = $form_state['values']['record_name'];
  1327. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1328. $record2priority[$record_name] = $priority;
  1329. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1330. $template[$priority]['record_id'] = $record_name;
  1331. }
  1332. else {
  1333. $priority = $form_state['values']['field_group'];
  1334. $record_name = $record2priority[$priority];
  1335. }
  1336. // Add field to template array
  1337. if ($form_state['values']['field_type'] == 'table field') {
  1338. $field = array(
  1339. 'type' => 'table field',
  1340. 'title' => $form_state['values']['field_title'],
  1341. 'field' => $form_state['values']['chado_field'],
  1342. 'required' => $form_state['values']['required'],
  1343. //'allowed values' => empty by default,
  1344. 'spreadsheet column' => $form_state['values']['column_number'],
  1345. 'exposed' => $form_state['values']['column_exposed'],
  1346. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1347. );
  1348. }
  1349. elseif ($form_state['values']['field_type'] == 'constant') {
  1350. $field = array(
  1351. 'type' => 'constant',
  1352. 'title' => $form_state['values']['field_title'],
  1353. 'field' => $form_state['values']['chado_field'],
  1354. 'required' => $form_state['values']['required'],
  1355. //'allowed values' => empty by default,
  1356. 'constant value' => $form_state['values']['constant_value'],
  1357. 'exposed' => $form_state['values']['constant_exposed'],
  1358. 'exposed_validate' => $form_state['values']['constant_validate'],
  1359. );
  1360. }
  1361. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1362. $field = array(
  1363. 'type' => 'foreign key',
  1364. 'title' => $form_state['values']['field_title'],
  1365. 'field' => $form_state['values']['chado_field'],
  1366. 'show_all_records' => $form_state['values']['show_all_records'],
  1367. 'foreign key' => $form_state['values']['foreign_record'],
  1368. 'foreign field' => $form_state['values']['foreign_field'],
  1369. 'required' => $form_state['values']['required'],
  1370. );
  1371. }
  1372. // Deal with any additional options
  1373. if ($form_state['storage']['regex']) {
  1374. $field['regex'] = $form_state['storage']['regex'];
  1375. }
  1376. // Save Template
  1377. $template[$priority]['fields'][] = $field;
  1378. $form_state['storage']['template']->template_array = serialize($template);
  1379. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1380. if ($success) {
  1381. drupal_set_message(t('Successfully Added Field to Template'));
  1382. drupal_set_message(t('Template Saved.'));
  1383. $path = explode('?', $form_state['storage']['referring URL']);
  1384. parse_str($path[1], $query);
  1385. $query['template_id'] = $form_state['storage']['template']->template_id;
  1386. drupal_goto($path[0], $query);
  1387. }
  1388. else {
  1389. drupal_set_message(t('Unable to Save Template!'), 'error');
  1390. watchdog('T_bulk_loader',
  1391. 'Unable to save bulk loader template: %template',
  1392. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1393. WATCHDOG_ERROR
  1394. );
  1395. }
  1396. }
  1397. elseif ($op == 'Cancel') {
  1398. $path = explode('?', $form_state['storage']['referring URL']);
  1399. parse_str($path[1], $query);
  1400. $query['template_id'] = $form_state['storage']['template']->template_id;
  1401. drupal_goto($path[0], $query);
  1402. }
  1403. elseif ($op == 'Add Transformation') {
  1404. // Add transformation rule to original field
  1405. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1406. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1407. drupal_set_message(t('Successfully Added Transformation Rule'));
  1408. }
  1409. elseif ($op == 'Save Transformation Rule Order') {
  1410. // Generate new regex array
  1411. $new_regex = array();
  1412. $old_regex = $form_state['storage']['regex'];
  1413. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1414. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1415. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1416. }
  1417. // sort new regex arrays
  1418. asort($new_regex['pattern']);
  1419. asort($new_regex['replace']);
  1420. $form_state['storage']['regex'] = $new_regex;
  1421. }
  1422. elseif ($op == 'Delete Transformation') {
  1423. // Unset regex rule
  1424. $index = $form_state['clicked_button']['#name'];
  1425. unset($form_state['storage']['regex']['pattern'][$index]);
  1426. unset($form_state['storage']['regex']['replace'][$index]);
  1427. }
  1428. elseif ($op == 'Test Transformation Rules') {
  1429. $patterns = $form_state['storage']['regex']['pattern'];
  1430. $replaces = $form_state['storage']['regex']['replace'];
  1431. $test_string = $form_state['values']['test_string'];
  1432. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1433. $form_state['storage']['test_regex_test'] = $test_string;
  1434. }
  1435. }
  1436. }
  1437. /**
  1438. * Edit Field Form
  1439. *
  1440. * This form is meant to be called from a bulk loader form. The following should be set
  1441. * in the query section of the path:
  1442. * - template_id=\d+: the template which the edited field is part of
  1443. * - record_id=\d+: the priority or key in the template array of the record the field
  1444. * is currently part of
  1445. * - field_index=\d+: the key of the field in the fields array of the previously
  1446. * specified record
  1447. *
  1448. * @param $form_state
  1449. * Contains the values and storage for the form
  1450. * @return
  1451. * A form array to be rendered by drupal_get_form
  1452. */
  1453. function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
  1454. $form = array();
  1455. $form['#cache'] = TRUE; // Make sure the form is cached.
  1456. // get template id from path
  1457. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1458. // if there is no template supplied don't return rest of form
  1459. if (!$template_id) {
  1460. return $form;
  1461. }
  1462. // Pre-set Variables needed for form proper------------------------------------------
  1463. // If this is the first load of the form (no form state) we need to initialize some variables
  1464. if (!$form_state['storage']['template']) {
  1465. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1466. $template = db_fetch_object(db_query($sql, $template_id));
  1467. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1468. $form_state['storage']['template'] = $template;
  1469. $form_state['storage']['record2priority'] = array();
  1470. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1471. if (!is_array($record_array)) {
  1472. continue;
  1473. }
  1474. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1475. }
  1476. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1477. }
  1478. else {
  1479. $template = $form_state['storage']['template'];
  1480. }
  1481. // get the field from the path
  1482. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1483. $priority = $_GET['record_id'];
  1484. $field_index = $_GET['field_index'];
  1485. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1486. $form_state['storage']['original_field'] = $template_field;
  1487. $form_state['storage']['original_field']['priority'] = $priority;
  1488. $form_state['storage']['original_field']['field_index'] = $field_index;
  1489. }
  1490. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1491. // Get the tables array and default table
  1492. $tables = tripal_core_get_chado_tables(TRUE);
  1493. if ($form_state['values']) {
  1494. $table = $form_state['values']['chado_table'];
  1495. }
  1496. else {
  1497. $table = $form_state['storage']['template_array'][$priority]['table'];
  1498. }
  1499. $show_all = ($form_state['values']['show_all_records'] ? $form_state['values']['show_all_records'] : $template_field['show_all_records']);
  1500. $table_description = tripal_core_get_chado_table_schema($table);
  1501. // Fields and foreign key / referral mappings
  1502. // build the fields array
  1503. $chado_fields = array();
  1504. foreach ($table_description['fields'] as $field_name => $field_array) {
  1505. $chado_fields[$field_name] = $field_name;
  1506. }
  1507. $ref_chado_fields = array();
  1508. $fk_options = array();
  1509. $fk_options['NULL'] = 'None';
  1510. if ($field_type == 'foreign key' and !$show_all) {
  1511. $foreign_field2table = array();
  1512. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1513. foreach ($key_array['columns'] as $left_field => $right_field) {
  1514. //$chado_fields[$left_field] = $left_field;
  1515. $foreign_field2table[$left_field] = $key_table;
  1516. }
  1517. }
  1518. // reset($chado_fields);
  1519. // set default field
  1520. if (empty($chado_fields)) {
  1521. $field = NULL;
  1522. }
  1523. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1524. $field = $form_state['values']['chado_field'];
  1525. }
  1526. elseif ($template_field['field']) {
  1527. $field = $template_field['field'];
  1528. }
  1529. else {
  1530. $field = current($chado_fields);
  1531. }
  1532. // Foreign key options
  1533. $foreign_table = $foreign_field2table[$field];
  1534. if ($foreign_table) {
  1535. foreach ($form_state['storage']['record2priority'] as $record_name_ => $priority_ ) {
  1536. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority_]['table'])) {
  1537. $fk_options[$record_name_] = $record_name_;
  1538. }
  1539. }
  1540. }
  1541. }
  1542. // the user wants to see all of the records
  1543. elseif ($field_type == 'foreign key' and $show_all) {
  1544. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1545. $fk_options[$record_name] = $record_name;
  1546. }
  1547. }
  1548. // build the list of referrer table fields
  1549. if ($field_type == 'foreign key') {
  1550. $fk_rec = $form_state['values']['foreign_record'] ? $form_state['values']['foreign_record'] : $template_field['foreign key'];
  1551. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  1552. // first add in the foreign keys
  1553. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  1554. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  1555. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1556. foreach ($key_array['columns'] as $left_field => $right_field) {
  1557. if($key_table == $fk_table and $left_field == $field){
  1558. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  1559. }
  1560. }
  1561. }
  1562. $fk_description = tripal_core_get_chado_table_schema($fk_table);
  1563. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray){
  1564. // don't include the FK field it's included above
  1565. if (in_array('Foreign Key',$ref_chado_fields) and
  1566. in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  1567. continue;
  1568. }
  1569. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  1570. }
  1571. }
  1572. }
  1573. // Start of Form Proper--------------------------------------------------------------
  1574. $form['template_name'] = array(
  1575. '#type' => 'item',
  1576. '#title' => 'Template',
  1577. '#value' => $template->name,
  1578. );
  1579. $form['template_id'] = array(
  1580. '#type' => 'hidden',
  1581. '#value' => $template_id,
  1582. );
  1583. $form['edit_fields'] = array(
  1584. '#type' => 'fieldset',
  1585. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1586. '#suffix' => '</div>',
  1587. );
  1588. $form['edit_fields']['field_type'] = array(
  1589. '#type' => 'radios',
  1590. '#title' => t('Type of Field'),
  1591. '#options' => array(
  1592. 'table field' => t('Data Field: Fields which maps to a data file column'),
  1593. 'constant' => t('Constant: Field which remains Constant throughout the data file'),
  1594. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  1595. ),
  1596. '#required' => TRUE,
  1597. '#default_value' => $field_type,
  1598. '#ahah' => array(
  1599. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1600. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1601. 'effect' => 'fade'
  1602. ),
  1603. );
  1604. // check template array for records then edit one more
  1605. if (!$form_state['storage']['record2priority']) {
  1606. $groups = array();
  1607. }
  1608. else {
  1609. $groups = array_flip($form_state['storage']['record2priority']);
  1610. }
  1611. $groups['NONE'] = 'Select a Record';
  1612. $groups['NEW'] = 'New Record';
  1613. $form['edit_fields']['field_group'] = array(
  1614. '#type' => 'select',
  1615. '#title' => 'Record',
  1616. '#description' => 'This is used to group a set of fields together allowing '
  1617. .'multiple records to be inserted into the same table per line of the data file',
  1618. '#options' => $groups,
  1619. '#default_value' => (preg_match('/(\d+|\w+)/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1620. '#ahah' => array(
  1621. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1622. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1623. 'effect' => 'fade'
  1624. ),
  1625. '#required' => TRUE,
  1626. );
  1627. $form['edit_fields']['record_name'] = array(
  1628. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1629. '#title' => 'Unique Record Name',
  1630. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1631. '#suffix' => '</div>',
  1632. '#default_value' => $form_state['values']['record_name'],
  1633. );
  1634. $form['edit_fields']['field_title'] = array(
  1635. '#type' => 'textfield',
  1636. '#title' => t('Human-readable Title for Field'),
  1637. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1638. );
  1639. // Chado Field
  1640. $form['edit_fields']['chado'] = array(
  1641. '#type' => 'fieldset',
  1642. '#title' => t('Chado Field/Column Details'),
  1643. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1644. );
  1645. $form['edit_fields']['chado']['chado_table'] = array(
  1646. '#type' => 'select',
  1647. '#title' => t('Chado Table'),
  1648. '#options' => $tables,
  1649. '#default_value' => $table,
  1650. '#ahah' => array(
  1651. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1652. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1653. 'effect' => 'fade'
  1654. ),
  1655. );
  1656. $form['edit_fields']['chado']['chado_field'] = array(
  1657. '#type' => 'select',
  1658. '#title' => t('Chado Field/Column'),
  1659. '#options' => $chado_fields,
  1660. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1661. '#ahah' => array(
  1662. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1663. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1664. 'effect' => 'fade'
  1665. ),
  1666. );
  1667. // data file column
  1668. $form['edit_fields']['columns'] = array(
  1669. '#type' => 'fieldset',
  1670. '#title' => t('Data File Column'),
  1671. '#collapsible' => TRUE,
  1672. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1673. );
  1674. /**
  1675. $form['edit_fields']['columns']['sheet_name'] = array(
  1676. '#type' => 'textfield',
  1677. '#title' => t('Worksheet'),
  1678. '#description' => t('Specify the name of the worksheet.'),
  1679. '#size' => 5,
  1680. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1681. );
  1682. */
  1683. $form['edit_fields']['columns']['column_number'] = array(
  1684. '#type' => 'textfield',
  1685. '#title' => t('Column'),
  1686. '#description' => t('Specify the column in the data file that this field maps to where the first column is 1.'),
  1687. '#size' => 5,
  1688. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1689. );
  1690. $form['edit_fields']['columns']['column_exposed'] = array(
  1691. '#type' => 'checkbox',
  1692. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1693. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1694. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1695. );
  1696. $form['edit_fields']['columns']['column_exposed_desc'] = array(
  1697. '#type' => 'textfield',
  1698. '#title' => t('Description for exposed field on bulk loading job'),
  1699. '#description' => t('This description should tell the user what column should be entered here.'),
  1700. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1701. );
  1702. // Global Value
  1703. $form['edit_fields']['constant'] = array(
  1704. '#type' => 'fieldset',
  1705. '#title' => t('Constant'),
  1706. '#collapsible' => TRUE,
  1707. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1708. );
  1709. $form['edit_fields']['constant']['constant_value'] = array(
  1710. '#type' => 'textfield',
  1711. '#title' => t('Constant Value'),
  1712. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1713. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1714. );
  1715. $form['edit_fields']['constant']['constant_exposed'] = array(
  1716. '#type' => 'checkbox',
  1717. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1718. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1719. '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'],
  1720. );
  1721. $form['edit_fields']['constant']['constant_validate'] = array(
  1722. '#type' => 'checkbox',
  1723. '#title' => t('Ensure value is in table'),
  1724. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1725. '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'],
  1726. );
  1727. // Foreign Key / Referrer
  1728. $form['edit_fields']['foreign_key'] = array(
  1729. '#type' => 'fieldset',
  1730. '#title' => 'Record Referral',
  1731. '#collapsible' => TRUE,
  1732. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1733. );
  1734. $form['edit_fields']['foreign_key']['show_all_records'] = array(
  1735. '#type' => 'checkbox',
  1736. '#title' => 'Refer to any record',
  1737. '#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'),
  1738. '#default_value' => $show_all,
  1739. '#ahah' => array(
  1740. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1741. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1742. 'effect' => 'fade'
  1743. ),
  1744. );
  1745. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1746. '#type' => 'select',
  1747. '#title' => 'Record to refer to',
  1748. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1749. '#options' => $fk_options,
  1750. '#ahah' => array(
  1751. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1752. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1753. 'effect' => 'fade'
  1754. ),
  1755. '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
  1756. );
  1757. $form['edit_fields']['foreign_key']['foreign_field'] = array(
  1758. '#type' => 'select',
  1759. '#title' => 'Field to refer to',
  1760. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1761. '#options' => $ref_chado_fields,
  1762. '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign field'],
  1763. );
  1764. $form['edit_fields']['additional'] = array(
  1765. '#type' => 'fieldset',
  1766. '#title' => 'Additional Options',
  1767. );
  1768. $form['edit_fields']['additional']['required'] = array(
  1769. '#type' => 'checkbox',
  1770. '#title' => 'Make this field required',
  1771. '#default_value' => $template_field['required'],
  1772. );
  1773. $form['edit_fields']['additional']['regex_transform'] = array(
  1774. '#type' => 'fieldset',
  1775. '#title' => 'Transform Data File Value Rules',
  1776. '#collapsible' => TRUE,
  1777. '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
  1778. );
  1779. $transformation_msg = '<p>A transformation rule allows you to transform the original value '
  1780. .'(usually from a user submitted data file) into the form you would like it stored '
  1781. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1782. .'which determines which replacement patterns are applied and captures regions of the '
  1783. .'original value) and a replacement pattern (a string which may contain capture references '
  1784. .'that describes what the new value should be). Each rule is applied to the result of the '
  1785. .'previous rule.<p>';
  1786. $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
  1787. '#type' => 'item',
  1788. '#value' => $transformation_msg,
  1789. );
  1790. $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
  1791. '#tree' => TRUE,
  1792. );
  1793. foreach ($template_field['regex']['pattern'] as $index => $pattern) {
  1794. $data_element = array(
  1795. 'pattern' => array(
  1796. '#type' => 'item',
  1797. '#value' => $pattern,
  1798. ),
  1799. 'replace' => array(
  1800. '#type' => 'item',
  1801. '#value' => $template_field['regex']['replace'][$index],
  1802. ),
  1803. 'old_index' => array(
  1804. '#type' => 'hidden',
  1805. '#value' => $index,
  1806. ),
  1807. 'new_index' => array(
  1808. '#type' => 'select',
  1809. '#options' => range(0, sizeof($template_field['regex']['pattern'])-1),
  1810. '#default_value' => $index,
  1811. ),
  1812. 'id' => array(
  1813. '#type' => 'hidden',
  1814. '#value' => $index,
  1815. ),
  1816. 'submit-delete' => array(
  1817. '#type' => 'submit',
  1818. '#value' => 'Delete Transformation',
  1819. '#name' => $index,
  1820. ),
  1821. );
  1822. $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1823. }
  1824. $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1825. '#type' => 'submit',
  1826. '#value' => 'Save Transformation Rule Order'
  1827. );
  1828. $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
  1829. '#type' => 'fieldset',
  1830. '#title' => 'Add a new Transformation Rule',
  1831. );
  1832. $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1833. '#type' => 'textfield',
  1834. '#title' => 'Match Pattern',
  1835. '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
  1836. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1837. .'pattern get applied to the value. To capture a section of your value for use in the '
  1838. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1839. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1840. .' To match and capture any value use <i>.*</i>',
  1841. );
  1842. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1843. '#type' => 'textfield',
  1844. '#title' => 'Replacement Pattern',
  1845. '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
  1846. .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
  1847. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1848. .'first set of round brackets.</p>',
  1849. );
  1850. if ($field_type == 'table field') {
  1851. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1852. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1853. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1854. .'This allows you to substitute other data file values into the current field. For example, '
  1855. .'if you had the following line:<br />'
  1856. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1857. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1858. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1859. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1860. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1861. }
  1862. $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1863. '#type' => 'submit',
  1864. '#value' => 'Add Transformation',
  1865. );
  1866. $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
  1867. '#type' => 'fieldset',
  1868. '#title' => 'Test Transformation Rules',
  1869. );
  1870. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1871. '#type' => 'textfield',
  1872. '#title' => 'Test Value',
  1873. '#description' => 'This should be a value that you expect the above transformation rules '
  1874. .'to be applied to.',
  1875. '#default_value' => $form_state['storage']['test_regex_test'],
  1876. );
  1877. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1878. '#type' => 'textfield',
  1879. '#title' => 'Test Result',
  1880. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1881. .'riles were applied to the Test Value.',
  1882. '#default_value' => $form_state['storage']['test_regex_result'],
  1883. );
  1884. $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1885. '#type' => 'submit',
  1886. '#value' => 'Test Transformation Rules'
  1887. );
  1888. $form['edit_fields']['submit-edit_field'] = array(
  1889. '#type' => 'submit',
  1890. '#value' => 'Save Changes'
  1891. );
  1892. $form['edit_fields']['submit-cancel'] = array(
  1893. '#type' => 'submit',
  1894. '#value' => 'Cancel'
  1895. );
  1896. return $form;
  1897. }
  1898. function tripal_bulk_loader_edit_template_field_form_validate($form, $form_state) {
  1899. // Don't worry about validation when Cancel button is clicked
  1900. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1901. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1902. $form_state['values']['record_name'],
  1903. $form_state['values']['template_id'],
  1904. $form_state['storage']['template_array']
  1905. );
  1906. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1907. if ((!$is_unique) AND $new_record) {
  1908. form_set_error('record_name', "New Record Name must be unique. '".$form_state['values']['record_name']."' is not unique.");
  1909. }
  1910. }
  1911. }
  1912. /**
  1913. * Edit Field Form Submit
  1914. *
  1915. * @param $form
  1916. * The form that was submitted
  1917. * @param $form_state
  1918. * The values and storage for the form
  1919. */
  1920. function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state) {
  1921. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1922. //dpm($op, 'Operation Submitted');
  1923. //Clear Test
  1924. $form_state['storage']['test_regex_result'] = NULL;
  1925. $form_state['storage']['test_regex_test'] = NULL;
  1926. if (!$form_state['ahah_submission']) {
  1927. if ($op == 'Save Changes') {
  1928. // If new record
  1929. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1930. // add new record
  1931. $record_name = $form_state['values']['record_name'];
  1932. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1933. $old_priority = $form_state['storage']['original_field']['priority'];
  1934. $field_index = $form_state['storage']['original_field']['field_index'];
  1935. $form_state['storage']['record2priority'][$record_name] = $priority;
  1936. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1937. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1938. }
  1939. else {
  1940. $priority = $form_state['values']['field_group'];
  1941. $old_priority = $form_state['storage']['original_field']['priority'];
  1942. $field_index = $form_state['storage']['original_field']['field_index'];
  1943. $record_name = $form_state['storage']['record2priority'][$priority];
  1944. }
  1945. $field = $form_state['storage']['original_field'];
  1946. if ($form_state['values']['field_type'] == 'table field') {
  1947. $field['type'] = 'table field';
  1948. $field['title'] = $form_state['values']['field_title'];
  1949. $field['field'] = $form_state['values']['chado_field'];
  1950. $field['required'] = $form_state['values']['required'];
  1951. //$field['allowed values'] = empty by default;
  1952. $field['spreadsheet column'] = $form_state['values']['column_number'];
  1953. $field['exposed'] = $form_state['values']['column_exposed'];
  1954. $field['exposed_description'] = $form_state['values']['column_exposed_desc'];
  1955. }
  1956. elseif ($form_state['values']['field_type'] == 'constant') {
  1957. $field['type'] = 'constant';
  1958. $field['title'] = $form_state['values']['field_title'];
  1959. $field['field'] = $form_state['values']['chado_field'];
  1960. $field['required'] = $form_state['values']['required'];
  1961. //$field['allowed values'] = empty by default;
  1962. $field['constant value'] = $form_state['values']['constant_value'];
  1963. $field['exposed_validate'] = $form_state['values']['constant_validate'];
  1964. $field['exposed'] = $form_state['values']['constant_exposed'];
  1965. }
  1966. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1967. $field['type'] = 'foreign key';
  1968. $field['title'] = $form_state['values']['field_title'];
  1969. $field['field'] = $form_state['values']['chado_field'];
  1970. $field['show_all_records'] = $form_state['values']['show_all_records'];
  1971. $field['foreign key'] = $form_state['values']['foreign_record'];
  1972. $field['foreign field'] = $form_state['values']['foreign_field'];
  1973. $field['required'] = $form_state['values']['required'];
  1974. }
  1975. // Deal with any additional options
  1976. // if the record has changed...
  1977. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1978. if ($old_priority != $priority) {
  1979. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1980. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1981. // if there are no fields left delete the old record
  1982. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1983. unset($form_state['storage']['template_array'][$old_priority]);
  1984. }
  1985. }
  1986. else {
  1987. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1988. }
  1989. // Save Template
  1990. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1991. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1992. if ($success) {
  1993. drupal_set_message(t('Successfully Updated Field'));
  1994. drupal_set_message(t('Template Saved.'));
  1995. $path = explode('?', $form_state['storage']['referring URL']);
  1996. parse_str($path[1], $query);
  1997. $query['template_id'] = $form_state['storage']['template']->template_id;
  1998. drupal_goto($path[0], $query);
  1999. }
  2000. else {
  2001. drupal_set_message(t('Unable to Save Template!'), 'error');
  2002. watchdog('T_bulk_loader',
  2003. 'Unable to save bulk loader template: %template',
  2004. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2005. WATCHDOG_ERROR
  2006. );
  2007. }
  2008. }
  2009. elseif ($op == 'Cancel') {
  2010. $path = explode('?', $form_state['storage']['referring URL']);
  2011. parse_str($path[1], $query);
  2012. $query['template_id'] = $form_state['storage']['template']->template_id;
  2013. drupal_goto($path[0], $query);
  2014. }
  2015. elseif ($op == 'Add Transformation') {
  2016. // Add transformation rule to original field
  2017. $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  2018. $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
  2019. // Add original field back into template
  2020. $priority = $form_state['storage']['original_field']['priority'];
  2021. $field_index = $form_state['storage']['original_field']['field_index'];
  2022. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  2023. // Save Template
  2024. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  2025. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2026. if ($success) {
  2027. drupal_set_message(t('Successfully Added Transformation Rule'));
  2028. drupal_set_message(t('Template Saved.'));
  2029. }
  2030. else {
  2031. drupal_set_message(t('Unable to Save Template!'), 'error');
  2032. watchdog('T_bulk_loader',
  2033. 'Unable to save bulk loader template: %template',
  2034. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2035. WATCHDOG_ERROR
  2036. );
  2037. }
  2038. }
  2039. elseif ($op == 'Save Transformation Rule Order') {
  2040. // Generate new regex array
  2041. $new_regex = array();
  2042. $old_regex = $form_state['storage']['original_field']['regex'];
  2043. foreach ($form_state['values']['regex-data'] as $key => $element) {
  2044. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  2045. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  2046. }
  2047. // sort new regex arrays
  2048. asort($new_regex['pattern']);
  2049. asort($new_regex['replace']);
  2050. // Add back to original field
  2051. $form_state['storage']['original_field']['regex'] = $new_regex;
  2052. $priority = $form_state['storage']['original_field']['priority'];
  2053. $field_index = $form_state['storage']['original_field']['field_index'];
  2054. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  2055. // Save Template
  2056. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  2057. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2058. if ($success) {
  2059. drupal_set_message(t('Successfully Reordered Transformation Rules'));
  2060. drupal_set_message(t('Template Saved.'));
  2061. }
  2062. else {
  2063. drupal_set_message(t('Unable to Save Template!'), 'error');
  2064. watchdog('T_bulk_loader',
  2065. 'Unable to save bulk loader template: %template',
  2066. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2067. WATCHDOG_ERROR
  2068. );
  2069. }
  2070. }
  2071. elseif ($op == 'Delete Transformation') {
  2072. // Unset regex rule
  2073. $index = $form_state['clicked_button']['#name'];
  2074. unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
  2075. unset($form_state['storage']['original_field']['regex']['replace'][$index]);
  2076. $priority = $form_state['storage']['original_field']['priority'];
  2077. $field_index = $form_state['storage']['original_field']['field_index'];
  2078. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  2079. // Save Template
  2080. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  2081. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2082. if ($success) {
  2083. drupal_set_message(t('Successfully Reordered Transformation Rules'));
  2084. drupal_set_message(t('Template Saved.'));
  2085. }
  2086. else {
  2087. drupal_set_message(t('Unable to Save Template!'), 'error');
  2088. watchdog('T_bulk_loader',
  2089. 'Unable to save bulk loader template: %template',
  2090. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2091. WATCHDOG_ERROR
  2092. );
  2093. }
  2094. }
  2095. elseif ($op == 'Test Transformation Rules') {
  2096. $patterns = $form_state['storage']['original_field']['regex']['pattern'];
  2097. $replaces = $form_state['storage']['original_field']['regex']['replace'];
  2098. $test_string = $form_state['values']['test_string'];
  2099. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  2100. $form_state['storage']['test_regex_test'] = $test_string;
  2101. }
  2102. }
  2103. }
  2104. /**
  2105. * @section
  2106. * Helper Functions
  2107. */
  2108. /**
  2109. * Meant to be called from a form_validate function to ensure a newly added bulk loader record
  2110. * name is unique and not empty.
  2111. *
  2112. * @param $new_record_name
  2113. * The record name to check for uniqueness
  2114. * @param $template_id
  2115. * The template_id of the template to add the record to
  2116. * @param $template_array
  2117. * The array describing the template. Optional -will be loaded using template_id if not provided
  2118. *
  2119. * @return
  2120. * TRUE if the record name is not empty and not already in the template_array; FALSE otherwise
  2121. */
  2122. function tripal_bulk_loader_is_record_name_unique($new_record_name, $template_id, $template_array = NULL) {
  2123. // get the template array if it's not supplied
  2124. if (empty($template_array)) {
  2125. $template = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d",$template_id));
  2126. $template_array = unserialize($template->template_array);
  2127. if (!is_array($template_array)) {
  2128. watchdog(
  2129. 'tripal_bulk_loader',
  2130. 'Unable to retrieve template array from database where template_id=%template_id',
  2131. array('%template_id' => $template_id),
  2132. WATCHDOG_WARNING
  2133. );
  2134. return FALSE;
  2135. }
  2136. }
  2137. // Check that the new record name is not empty
  2138. if (empty($new_record_name)) {
  2139. return FALSE;
  2140. }
  2141. // Check the new record name is unique
  2142. foreach ($template_array as $t) {
  2143. if (strcmp($t['record_id'], $new_record_name) == 0) {
  2144. return FALSE;
  2145. }
  2146. }
  2147. return TRUE;
  2148. }
  2149. /**
  2150. * @section
  2151. * AHAH Callbacks
  2152. */
  2153. /**
  2154. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  2155. *
  2156. * @return
  2157. * JSON Data printed to the screen
  2158. */
  2159. function tripal_bulk_loader_add_field_ahah() {
  2160. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  2161. $form_build_id = filter_xss($_POST['form_build_id']);
  2162. $form = form_get_cache($form_build_id, $form_state);
  2163. $args = $form['#parameters'];
  2164. $form_id = array_shift($args);
  2165. $form_state['post'] = $form['#post'] = $_POST;
  2166. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  2167. $form_state['ahah_submission'] = TRUE;
  2168. $form['#programmed'] = $form['#redirect'] = FALSE;
  2169. drupal_process_form($form_id, $form, $form_state);
  2170. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  2171. $form_element = $form['add_fields'];
  2172. // Remove the wrapper so we don't double it up.
  2173. //unset($form_element['#prefix'], $form_element['#suffix']);
  2174. $output = theme('status_messages');
  2175. $output .= drupal_render($form_element);
  2176. // Final rendering callback.
  2177. print drupal_json(array('status' => TRUE, 'data' => $output));
  2178. exit();
  2179. }
  2180. /**
  2181. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  2182. *
  2183. * @return
  2184. * JSON Data printed to the screen
  2185. */
  2186. function tripal_bulk_loader_edit_field_ahah() {
  2187. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  2188. $form_build_id = filter_xss($_POST['form_build_id']);
  2189. $form = form_get_cache($form_build_id, $form_state);
  2190. $args = $form['#parameters'];
  2191. $form_id = array_shift($args);
  2192. $form_state['post'] = $form['#post'] = $_POST;
  2193. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  2194. $form_state['ahah_submission'] = TRUE;
  2195. $form['#programmed'] = $form['#redirect'] = FALSE;
  2196. drupal_process_form($form_id, $form, $form_state);
  2197. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  2198. $form_element = $form['edit_fields'];
  2199. // Remove the wrapper so we don't double it up.
  2200. unset($form_element['#prefix'], $form_element['#suffix']);
  2201. $output = theme('status_messages');
  2202. $output .= drupal_render($form_element);
  2203. // Final rendering callback.
  2204. print drupal_json(array('status' => TRUE, 'data' => $output));
  2205. exit();
  2206. }