tripal_bulk_loader.admin.templates.inc 93 KB

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