tripal_bulk_loader.admin.templates.inc 94 KB

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