tripal_bulk_loader.admin.templates.inc 92 KB

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