Advertisement
benjamin_mcf

Untitled

Dec 5th, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.41 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. function nodeform_form_alter(&$form, &$form_state, $form_id) {
  5.     if (($form_id == 'article_node_form') || ($form_id == 'avatar_selection_upload_form') || ($form_id == 'avatar_selection_edit_form')) {    
  6.      
  7.       $subjids = array();  
  8.       if (!empty($form_state['values']['subject'])) {
  9.         $subjids = array_filter($form_state['values']['subject']);
  10.       }
  11.       $gradids = array();
  12.       if (!empty($form_state['values']['grade'])) {
  13.         $gradids = array_filter($form_state['values']['grade']);
  14.       }
  15.       $strandids = array();
  16.       if (!empty($form_state['values']['strand'])) {
  17.         $strandids = array_filter($form_state['values']['strand']);
  18.       }
  19.       $topicids = array();
  20.       if (!empty($form_state['values']['topics'])) {
  21.         $topicids = array_filter($form_state['values']['topics']);
  22.       }
  23.       if ( !empty($form['aid']) ) {
  24.         $aid = $form['aid']['#value'];
  25.       }
  26.       drupal_set_message('<pre>' . print_r($form, TRUE) . '</pre>');
  27.      
  28.      
  29.       $form['#subvalues'] = $subjids;
  30.       $form['#grdvalues'] = $gradids;
  31.       $form['#strvalues'] = $strandids;
  32.       $form['#topvalues'] = $topicids;
  33.       if ( !empty($form['aid']) ) {
  34.           $form['#aid'] =  $aid;
  35.       }
  36.      
  37.       $form['subjectblock'] = array(
  38.             '#type'     => 'fieldset',
  39.             '#title'     => 'Subjects',
  40.       );  
  41.       $form['subjectblock']['subject'] = array(
  42.             '#type'     => 'checkboxes',
  43.             '#options' => grab_subjects(),
  44.             '#prefix' => '<div style="background:#FF7F55;">',
  45.             '#suffix' => '</div>',
  46.             '#ajax' => array(
  47.                 'callback' => 'make_topic_chex_callback',
  48.                 'progress' => array('type' => 'none'),
  49.             ),
  50.       );
  51.       $form['gradeblock'] = array(
  52.             '#type'     => 'fieldset',
  53.             '#title'     => 'Grades',
  54.       );
  55.       $form['gradeblock']['grade'] = array(
  56.             '#type'     => 'checkboxes',
  57.             '#options' => grab_grades(),
  58.             '#prefix' => '<div style="background:#557FFF;">',
  59.             '#suffix' => '</div>',
  60.             '#ajax' => array(
  61.                 'callback' => 'make_topic_chex_callback',
  62.                 'progress' => array('type' => 'none'),
  63.             ),
  64.       );
  65.       $form['blanx'] = array(
  66.             '#type'     => 'markup',
  67.             '#prefix' => '<div>',
  68.             '#suffix' => '</div>',
  69.       );  
  70.       $form['strandwrap'] = array(
  71.             '#type'     => 'markup',
  72.             '#prefix' => '<div id="pow">',
  73.             '#suffix' => '</div>',
  74.       );  
  75.       if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  76.           $form['strandwrap']['strandblock'] = array(
  77.                 '#type'     => 'fieldset',
  78.                 '#title'     => 'Strands',
  79.           );
  80.       }
  81.       if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  82.           $form['strandwrap']['strandblock']['strand'] = array(
  83.                 '#type'     => 'checkboxes',
  84.                 '#options' => grab_strands($subjids, $gradids),
  85.                 '#prefix' => '<div style="background:#FFFF2A;">',
  86.                 '#suffix' => '</div>',
  87.                 '#ajax' => array(
  88.                     'callback' => 'make_topic_chex_callback',
  89.                     'progress' => array('type' => 'none'),
  90.                 ),
  91.           );
  92.       }
  93.       $form['topwrap'] = array(
  94.             '#type'     => 'markup',
  95.             '#prefix' => '<div id="gunge">',
  96.             '#suffix' => '</div>',
  97.       );  
  98.       if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  99.           $form['topwrap']['topblock'] = array(
  100.                     '#type'   => 'fieldset',
  101.                     '#title'  => 'Topics',
  102.           );
  103.       }
  104.       if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
  105.           $form['topwrap']['topblock']['topics'] = array(
  106.                 '#type'     => 'checkboxes',
  107.                 '#options' => grab_topics($subjids, $gradids, $strandids),
  108.                 '#prefix' => '<div style="background:#FFAA00;">',
  109.                 '#suffix' => '</div>',
  110.           );
  111.       }
  112.    }
  113.    if ($form_id == 'article_node_form') {
  114.       $form['#prefix'] = '<div style="background:#AAFF55;">';
  115.       $form['#suffix'] = '</div>';
  116.       $form['actions']['submit']['#value'] = t('Hammering');
  117.       $form['title']['#default_value'] = t('Dance');
  118.       $form['title']['#required'] = FALSE;
  119.    }
  120.    if ($form_id == 'avatar_selection_upload_form') {
  121.       $form['#submit'][] = 'tagg_adder_submit';
  122.    }
  123. }
  124.  
  125.  
  126.  
  127. function make_topic_chex_callback(&$form, $form_state) {
  128.     $commands = array();
  129.     $subz = array_filter($form_state['values']['subject']);
  130.     $grdz = array_filter($form_state['values']['grade']);
  131.     $isempty_subz = $subz ? 'TRUE' : 'FALSE';
  132.     $isempty_grdz = $grdz ? 'TRUE' : 'FALSE';
  133.     if ($isempty_subz == 'TRUE' && $isempty_grdz == 'TRUE' ) {
  134.         $commands[] = ajax_command_replace("#gunge", render($form['topwrap']));
  135.     }
  136.     else {
  137.         $commands[] = ajax_command_replace("#gunge", "<div id='gunge'>");
  138.     }
  139.     if ($isempty_subz == 'TRUE' && (in_array(15, $grdz) || in_array(16, $grdz)) ) {
  140.         $commands[] = ajax_command_replace("#pow", render($form['strandwrap']));
  141.     }
  142.     else {
  143.         $commands[] = ajax_command_replace("#pow", "<div id='pow'>");
  144.     }
  145.     return array('#type' => 'ajax', '#commands' => $commands);
  146. }
  147.  
  148. function grab_grades() {
  149.   $gquery = db_select('taxonomy_term_data', 't');
  150.   $gquery
  151.         ->fields('t', array('vid', 'name', 'tid', 'weight'))
  152.         ->condition('t.vid', 3, '=')
  153.         ->orderBy('t.weight', 'ASC');
  154.   $gresult = $gquery->execute();
  155.   $goptions = array();
  156.   foreach ($gresult as $grow) {
  157.         $goptions[$grow->tid] = $grow->name;
  158.   };
  159.   return $goptions;
  160. }
  161.  
  162. function grab_subjects() {
  163.   $squery = db_select('taxonomy_term_data', 't');
  164.   $squery
  165.         ->fields('t', array('vid', 'name', 'tid', 'weight'))
  166.         ->condition('t.vid', 2, '=')
  167.         ->orderBy('t.weight', 'ASC');
  168.   $sresult = $squery->execute();
  169.   $soptions = array();
  170.   foreach ($sresult as $srow) {
  171.         $soptions[$srow->tid] = $srow->name;
  172.   };
  173.   return $soptions;
  174. }
  175.  
  176. function grab_strands($subjids, $gradids) {    
  177.      
  178.   $strquery = db_select('taxonomy_term_data', 'str');
  179.   $strquery->join('field_data_field_strand_subject', 's', 'str.tid = s.entity_id');
  180.   $strquery->join(' field_data_field_strand_grade', 'g', 'str.tid = g.entity_id');
  181.   $strquery->fields('str', array('vid', 'name', 'tid'));
  182.   $strquery->fields('s', array('field_strand_subject_tid'));
  183.   $strquery->fields('g', array('field_strand_grade_tid'));
  184.   $db_or = db_or();
  185.   foreach ($subjids as $subjid) {
  186.       $db_or->condition('s.field_strand_subject_tid', $subjid, '=');    
  187.       $strquery->condition($db_or);
  188.   }
  189.   $db_more = db_or();
  190.   foreach ($gradids as $gradid) {
  191.       $db_more->condition('g.field_strand_grade_tid', $gradid, '=');    
  192.       $strquery->condition($db_more);
  193.   }
  194.   $strresult = $strquery->execute();
  195.   $stroptions = array();
  196.   foreach ($strresult as $strrow) {
  197.         $stroptions[$strrow->tid] = $strrow->name;
  198.   };
  199.   return $stroptions;
  200. }
  201.  
  202. function grab_topics($subjids, $gradids, $strandids) {
  203.      
  204.      
  205.   $topquery = db_select('taxonomy_term_data', 't');
  206.   $topquery->join('field_data_field_topic_subject', 's', 't.tid = s.entity_id');
  207.   $topquery->join('field_data_field_topic_grade', 'g', 't.tid = g.entity_id');
  208.   if (!empty($strandids) && (in_array(15, $gradids) || in_array(16, $gradids))) {
  209.       $topquery->join('field_data_field_topic_strand', 'str', 't.tid = str.entity_id');
  210.   }
  211.   $topquery->fields('t', array('vid', 'name', 'tid'));
  212.   $topquery->fields('s', array('field_topic_subject_tid'));
  213.   $topquery->fields('g', array('field_topic_grade_tid'));
  214.   if (!empty($strandids) && (in_array(15, $gradids) || in_array(16, $gradids))) {
  215.       $topquery->fields('str', array('field_topic_strand_tid'));
  216.   }
  217.   $topquery->condition('t.vid', 5, '=');
  218.   $db_or = db_or();
  219.   foreach ($subjids as $subjid) {
  220.       $db_or->condition('s.field_topic_subject_tid', $subjid, '=');    
  221.       $topquery->condition($db_or);
  222.   }
  223.   $db_more = db_or();
  224.   foreach ($gradids as $gradid) {
  225.       $db_more->condition('g.field_topic_grade_tid', $gradid, '=');    
  226.       $topquery->condition($db_more);
  227.   }
  228.   $topresult = $topquery->execute();
  229.   $topoptions = array();
  230.   $strandids[] = 539;
  231.   foreach ($topresult as $toprow) {
  232.       if (!(isset($toprow->field_topic_strand_tid))) {
  233.         $topoptions[$toprow->tid] = $toprow->name;
  234.       }
  235.       if ((isset($toprow->field_topic_strand_tid)) && (in_array($toprow->field_topic_strand_tid, $strandids))) {
  236.         $topoptions[$toprow->tid] = $toprow->name;
  237.       }
  238.   };
  239.   return $topoptions;
  240. }
  241.  
  242.  
  243.  
  244. // This function hooks into the original form and populates the database with the checked grades,
  245. // subjects, strands, and topics with an additional submit hook
  246.  
  247. // if the record is not yet in the avatar selectiont table, then it adds the record below and
  248. // then adds records in the complementary join tables
  249.  
  250. // if the record is already in the avatar selection table, then it updates the record and deletes
  251. // all records in the complementary tables. Then it readds records in the complementary tables.
  252. // That way, it doesn't have to do some funky loop in the complementary tables when it updates the records
  253. // stemming from complementary checkboxes being checked
  254.  
  255. function tagg_adder_submit($form, &$form_state) {
  256.  
  257.   if ( !empty($form['aid']) ) {    
  258.     $aid = $form['#aid'];  
  259.   }
  260.   $subjids = $form['#subvalues'];
  261.   $gradids = $form['#grdvalues'];
  262.   $strandids = $form['#strvalues'];
  263.   $topicids = $form['#topvalues'];
  264.  
  265.  
  266.   // Add or update avatar_selection table.
  267.   if (isset($aid)) {
  268.     $result = db_delete('avatar_selection_subjects')
  269.       ->condition('aid', $aid)
  270.       ->execute();
  271.     $result = db_delete('avatar_selection_grades')
  272.       ->condition('aid', $aid)
  273.       ->execute();
  274.     $result = db_delete('avatar_selection_strands')
  275.       ->condition('aid', $aid)
  276.       ->execute();
  277.     $result = db_delete('avatar_selection_topics')
  278.       ->condition('aid', $aid)
  279.       ->execute();
  280.   }
  281.   else {
  282.     $aid = 2;
  283.   }
  284.      
  285.   // Add access settings.
  286.   if (is_array($subjids) && count($subjids)) {
  287.     foreach ($subjids as $subjid) {
  288.       $id = db_insert('avatar_selection_subjects')
  289.         ->fields(array(
  290.           'aid' => $aid,
  291.           'subject_id' => $subjid,
  292.         ))
  293.         ->execute();
  294.     }
  295.   }
  296.   if (is_array($gradids) && count($gradids)) {
  297.     foreach ($gradids as $gradid) {
  298.       $id = db_insert('avatar_selection_grades')
  299.         ->fields(array(
  300.           'aid' => $aid,
  301.           'grade_id' => $gradid,
  302.         ))
  303.         ->execute();
  304.     }
  305.   }
  306.   if (is_array($strandids) && count($strandids)) {
  307.     foreach ($strandids as $strandid) {
  308.       $id = db_insert('avatar_selection_strands')
  309.         ->fields(array(
  310.           'aid' => $aid,
  311.           'strand_id' => $strandid,
  312.         ))
  313.         ->execute();
  314.     }
  315.   }
  316.   if (is_array($topicids) && count($topicids)) {
  317.     foreach ($topicids as $topicid) {
  318.       $id = db_insert('avatar_selection_topics')
  319.         ->fields(array(
  320.           'aid' => $aid,
  321.           'topic_id' => $topicid,
  322.         ))
  323.         ->execute();
  324.     }
  325.   }
  326. }
  327.  
  328.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement