Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function nodeform_form_alter(&$form, &$form_state, $form_id) {
- if (($form_id == 'article_node_form') || ($form_id == 'avatar_selection_upload_form') || ($form_id == 'avatar_selection_edit_form')) {
- $subjids = array();
- if (!empty($form_state['values']['subject'])) {
- $subjids = array_filter($form_state['values']['subject']);
- }
- $gradids = array();
- if (!empty($form_state['values']['grade'])) {
- $gradids = array_filter($form_state['values']['grade']);
- }
- $strandids = array();
- if (!empty($form_state['values']['strand'])) {
- $strandids = array_filter($form_state['values']['strand']);
- }
- $topicids = array();
- if (!empty($form_state['values']['topics'])) {
- $topicids = array_filter($form_state['values']['topics']);
- }
- if ( !empty($form['aid']) ) {
- $aid = $form['aid']['#value'];
- }
- drupal_set_message('<pre>' . print_r($form, TRUE) . '</pre>');
- $form['#subvalues'] = $subjids;
- $form['#grdvalues'] = $gradids;
- $form['#strvalues'] = $strandids;
- $form['#topvalues'] = $topicids;
- if ( !empty($form['aid']) ) {
- $form['#aid'] = $aid;
- }
- $form['subjectblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Subjects',
- );
- $form['subjectblock']['subject'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_subjects(),
- '#prefix' => '<div style="background:#FF7F55;">',
- '#suffix' => '</div>',
- '#ajax' => array(
- 'callback' => 'make_topic_chex_callback',
- 'progress' => array('type' => 'none'),
- ),
- );
- $form['gradeblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Grades',
- );
- $form['gradeblock']['grade'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_grades(),
- '#prefix' => '<div style="background:#557FFF;">',
- '#suffix' => '</div>',
- '#ajax' => array(
- 'callback' => 'make_topic_chex_callback',
- 'progress' => array('type' => 'none'),
- ),
- );
- $form['blanx'] = array(
- '#type' => 'markup',
- '#prefix' => '<div>',
- '#suffix' => '</div>',
- );
- $form['strandwrap'] = array(
- '#type' => 'markup',
- '#prefix' => '<div id="pow">',
- '#suffix' => '</div>',
- );
- if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
- $form['strandwrap']['strandblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Strands',
- );
- }
- if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
- $form['strandwrap']['strandblock']['strand'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_strands($subjids, $gradids),
- '#prefix' => '<div style="background:#FFFF2A;">',
- '#suffix' => '</div>',
- '#ajax' => array(
- 'callback' => 'make_topic_chex_callback',
- 'progress' => array('type' => 'none'),
- ),
- );
- }
- $form['topwrap'] = array(
- '#type' => 'markup',
- '#prefix' => '<div id="gunge">',
- '#suffix' => '</div>',
- );
- if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
- $form['topwrap']['topblock'] = array(
- '#type' => 'fieldset',
- '#title' => 'Topics',
- );
- }
- if (!empty($form_state['values']['subject']) && !empty($form_state['values']['grade'])) {
- $form['topwrap']['topblock']['topics'] = array(
- '#type' => 'checkboxes',
- '#options' => grab_topics($subjids, $gradids, $strandids),
- '#prefix' => '<div style="background:#FFAA00;">',
- '#suffix' => '</div>',
- );
- }
- }
- if ($form_id == 'article_node_form') {
- $form['#prefix'] = '<div style="background:#AAFF55;">';
- $form['#suffix'] = '</div>';
- $form['actions']['submit']['#value'] = t('Hammering');
- $form['title']['#default_value'] = t('Dance');
- $form['title']['#required'] = FALSE;
- }
- if ($form_id == 'avatar_selection_upload_form') {
- $form['#submit'][] = 'tagg_adder_submit';
- }
- }
- function make_topic_chex_callback(&$form, $form_state) {
- $commands = array();
- $subz = array_filter($form_state['values']['subject']);
- $grdz = array_filter($form_state['values']['grade']);
- $isempty_subz = $subz ? 'TRUE' : 'FALSE';
- $isempty_grdz = $grdz ? 'TRUE' : 'FALSE';
- if ($isempty_subz == 'TRUE' && $isempty_grdz == 'TRUE' ) {
- $commands[] = ajax_command_replace("#gunge", render($form['topwrap']));
- }
- else {
- $commands[] = ajax_command_replace("#gunge", "<div id='gunge'>");
- }
- if ($isempty_subz == 'TRUE' && (in_array(15, $grdz) || in_array(16, $grdz)) ) {
- $commands[] = ajax_command_replace("#pow", render($form['strandwrap']));
- }
- else {
- $commands[] = ajax_command_replace("#pow", "<div id='pow'>");
- }
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- function grab_grades() {
- $gquery = db_select('taxonomy_term_data', 't');
- $gquery
- ->fields('t', array('vid', 'name', 'tid', 'weight'))
- ->condition('t.vid', 3, '=')
- ->orderBy('t.weight', 'ASC');
- $gresult = $gquery->execute();
- $goptions = array();
- foreach ($gresult as $grow) {
- $goptions[$grow->tid] = $grow->name;
- };
- return $goptions;
- }
- function grab_subjects() {
- $squery = db_select('taxonomy_term_data', 't');
- $squery
- ->fields('t', array('vid', 'name', 'tid', 'weight'))
- ->condition('t.vid', 2, '=')
- ->orderBy('t.weight', 'ASC');
- $sresult = $squery->execute();
- $soptions = array();
- foreach ($sresult as $srow) {
- $soptions[$srow->tid] = $srow->name;
- };
- return $soptions;
- }
- function grab_strands($subjids, $gradids) {
- $strquery = db_select('taxonomy_term_data', 'str');
- $strquery->join('field_data_field_strand_subject', 's', 'str.tid = s.entity_id');
- $strquery->join(' field_data_field_strand_grade', 'g', 'str.tid = g.entity_id');
- $strquery->fields('str', array('vid', 'name', 'tid'));
- $strquery->fields('s', array('field_strand_subject_tid'));
- $strquery->fields('g', array('field_strand_grade_tid'));
- $db_or = db_or();
- foreach ($subjids as $subjid) {
- $db_or->condition('s.field_strand_subject_tid', $subjid, '=');
- $strquery->condition($db_or);
- }
- $db_more = db_or();
- foreach ($gradids as $gradid) {
- $db_more->condition('g.field_strand_grade_tid', $gradid, '=');
- $strquery->condition($db_more);
- }
- $strresult = $strquery->execute();
- $stroptions = array();
- foreach ($strresult as $strrow) {
- $stroptions[$strrow->tid] = $strrow->name;
- };
- return $stroptions;
- }
- function grab_topics($subjids, $gradids, $strandids) {
- $topquery = db_select('taxonomy_term_data', 't');
- $topquery->join('field_data_field_topic_subject', 's', 't.tid = s.entity_id');
- $topquery->join('field_data_field_topic_grade', 'g', 't.tid = g.entity_id');
- if (!empty($strandids) && (in_array(15, $gradids) || in_array(16, $gradids))) {
- $topquery->join('field_data_field_topic_strand', 'str', 't.tid = str.entity_id');
- }
- $topquery->fields('t', array('vid', 'name', 'tid'));
- $topquery->fields('s', array('field_topic_subject_tid'));
- $topquery->fields('g', array('field_topic_grade_tid'));
- if (!empty($strandids) && (in_array(15, $gradids) || in_array(16, $gradids))) {
- $topquery->fields('str', array('field_topic_strand_tid'));
- }
- $topquery->condition('t.vid', 5, '=');
- $db_or = db_or();
- foreach ($subjids as $subjid) {
- $db_or->condition('s.field_topic_subject_tid', $subjid, '=');
- $topquery->condition($db_or);
- }
- $db_more = db_or();
- foreach ($gradids as $gradid) {
- $db_more->condition('g.field_topic_grade_tid', $gradid, '=');
- $topquery->condition($db_more);
- }
- $topresult = $topquery->execute();
- $topoptions = array();
- $strandids[] = 539;
- foreach ($topresult as $toprow) {
- if (!(isset($toprow->field_topic_strand_tid))) {
- $topoptions[$toprow->tid] = $toprow->name;
- }
- if ((isset($toprow->field_topic_strand_tid)) && (in_array($toprow->field_topic_strand_tid, $strandids))) {
- $topoptions[$toprow->tid] = $toprow->name;
- }
- };
- return $topoptions;
- }
- // This function hooks into the original form and populates the database with the checked grades,
- // subjects, strands, and topics with an additional submit hook
- // if the record is not yet in the avatar selectiont table, then it adds the record below and
- // then adds records in the complementary join tables
- // if the record is already in the avatar selection table, then it updates the record and deletes
- // all records in the complementary tables. Then it readds records in the complementary tables.
- // That way, it doesn't have to do some funky loop in the complementary tables when it updates the records
- // stemming from complementary checkboxes being checked
- function tagg_adder_submit($form, &$form_state) {
- if ( !empty($form['aid']) ) {
- $aid = $form['#aid'];
- }
- $subjids = $form['#subvalues'];
- $gradids = $form['#grdvalues'];
- $strandids = $form['#strvalues'];
- $topicids = $form['#topvalues'];
- // Add or update avatar_selection table.
- if (isset($aid)) {
- $result = db_delete('avatar_selection_subjects')
- ->condition('aid', $aid)
- ->execute();
- $result = db_delete('avatar_selection_grades')
- ->condition('aid', $aid)
- ->execute();
- $result = db_delete('avatar_selection_strands')
- ->condition('aid', $aid)
- ->execute();
- $result = db_delete('avatar_selection_topics')
- ->condition('aid', $aid)
- ->execute();
- }
- else {
- $aid = 2;
- }
- // Add access settings.
- if (is_array($subjids) && count($subjids)) {
- foreach ($subjids as $subjid) {
- $id = db_insert('avatar_selection_subjects')
- ->fields(array(
- 'aid' => $aid,
- 'subject_id' => $subjid,
- ))
- ->execute();
- }
- }
- if (is_array($gradids) && count($gradids)) {
- foreach ($gradids as $gradid) {
- $id = db_insert('avatar_selection_grades')
- ->fields(array(
- 'aid' => $aid,
- 'grade_id' => $gradid,
- ))
- ->execute();
- }
- }
- if (is_array($strandids) && count($strandids)) {
- foreach ($strandids as $strandid) {
- $id = db_insert('avatar_selection_strands')
- ->fields(array(
- 'aid' => $aid,
- 'strand_id' => $strandid,
- ))
- ->execute();
- }
- }
- if (is_array($topicids) && count($topicids)) {
- foreach ($topicids as $topicid) {
- $id = db_insert('avatar_selection_topics')
- ->fields(array(
- 'aid' => $aid,
- 'topic_id' => $topicid,
- ))
- ->execute();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement