tripal_bulk_loader.admin.templates.inc 94 KB

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