function engagements_form_alter

6.x engagements.module engagements_form_alter(&$form, $form_id)

Implements hook_form_alter

We want to make various modifications to our form, based on what we are trying to do to it. We may also want to add in some custom javascript as well.

File

modules/engagements/engagements.module, line 2763
This is the primary module file for the engagements module.

Code

function engagements_form_alter(&$form, $form_id) 
 {

  global $user;

  if ($form_id == 'content_edit_content_form') {
    if (@$form ['type']['value'] == 'engagement') {

      $db = get_global_database_handler();

      fp_add_js(fp_get_module_path("engagements") . "/js/engagements.js");
      fp_add_css(fp_get_module_path("engagements") . "/css/style.css");

      // If this is a NEW form, then check for values in the URL to auto-fill.      
      if ($form ['cid']['value'] === 'new') {

        if (!isset($_GET ['engagement_type']) || $_GET ['engagement_type'] == '' || $_GET ['engagement_type'] == 'new') {
          // This means we are logging a NEW engagement, and not trying to send a txt or email.  We need to
          // set our manual_entry value to something meaningful.
          $form ['manual_entry']['value'] = 'Y';
        }

        if (isset($_GET ['engagement_type']) && $_GET ['engagement_type'] != '' && $_GET ['engagement_type'] != 'new') {

          $form ['engagement_type']['value'] = $_GET ['engagement_type'];


          // Since we are setting the type, we do not want to display it as an option.
          $form ['engagement_type']['attributes'] = array('class' => 'hidden');
          // There are other fields we do not wish to display as well:
          $form ['direction']['attributes'] = array('class' => 'hidden');
          $form ['activity_datetime']['attributes'] = array('class' => 'hidden');
        }

        $initials = variable_get_for_school("school_initials", "DEMO", $user->school_id);

        if (isset($_GET ['faculty_id'])) {
          $form ['faculty_id']['value'] = $_GET ['faculty_id'];

          $form ['faculty_id']['attributes'] = array('class' => 'hidden');
          $form ['mark_from'] = array(
            'type' => 'markup',
            'value' => "<div class='engagement-field-mark engagement-from'><label>" . t("From") . ":</label>
                                " . $db->get_faculty_name($form ['faculty_id']['value']) . "
                        </div>",
            'weight' => $form ['faculty_id']['weight'],
          );
        }


        if (isset($_GET ['student_id'])) {
          $form ['student_id']['value'] = $_GET ['student_id'];


          $initials = variable_get_for_school("school_initials", "DEMO", db_get_school_id_for_student_id($_GET ['student_id']));


          $form ['student_id']['attributes'] = array('class' => 'hidden');
          $extra_mark = "";

          if ($form ['engagement_type']['value'] == 'email') {
            $extra_mark = fp_get_student_email($form ['student_id']['value']);

            // TODO:  The EMAIL header may need to be a setting with replacement values
            $form ['engagement_msg']['prefix'] = t("Your message will automatically begin with <strong>(@initials) from @name:</strong>", array("@initials" => $initials, "@name" => $db->get_faculty_name($form ['faculty_id']['value'])));
          }


          $student_user_id = db_get_user_id_from_cwid($_GET ['student_id'], 'student');
          $student_account = fp_load_user($student_user_id);
          if ($form ['engagement_type']['value'] == 'txt_msg') {


            // Since this is a txt message, the textare has a max num of characters (1600)
            $form ['engagement_msg']['maxlength'] = 1600;


            // Add in select list of the phone numbers we are allowed to select from.
            $from_options = engagements_get_from_phones_for_fapi(FALSE, $user);
            $form ['from_sms_phone']['options'] = $from_options;
            if (count($from_options) == 0) {
              $form ['from_sms_phone']['suffix'] .= "<h3>" . t("Note: You do
                        not have permission to send txt messages from any number.
                        See your system administrator for access.") . "</h3>";

              $form ['from_sms_phone']['attributes']['readonly'] = "readonly";
              $form ['from_sms_phone']['attributes']['class'] .= " readonly";
              $form ['from_sms_phone']['attributes']['disabled'] .= "disabled";

              $form ['engagement_msg']['attributes']['readonly'] = "readonly";
              $form ['engagement_msg']['attributes']['class'] .= " readonly";
              $form ['engagement_msg']['attributes']['disabled'] .= "disabled";

              $form ['submit_submit']['attributes']['disabled'] = 'disabled';
              $form ['submit_submit']['attributes']['class'] .= ' button-disabled';
            }
            $form ['from_sms_phone']['required'] = TRUE;

            // get student phone number
            $extra_mark = "(NO MOBILE NUMBER AVAILABLE)";


            $mobile_phone = user_get_attribute($student_user_id, "mobile_phone");
            if ($mobile_phone) {
              $extra_mark = engagements_convert_to_pretty_phone_number($mobile_phone, TRUE);
            }
            else {
              // Mobile phone was never found, so disable submit and message box.

              $form ['engagement_msg']['attributes']['readonly'] = 'readonly';
              $form ['engagement_msg']['attributes']['class'] .= ' readonly';

              $form ['submit_submit']['attributes']['disabled'] = 'disabled';
              $form ['submit_submit']['attributes']['class'] .= ' button-disabled';
              $form ['submit_submit']['suffix'] = "<strong>" . t('NOTE: Cannot send a text message as there is no mobile phone number for this recipient.') . "</strong>";
            }

            if (trim($mobile_phone) != "" && !engagements_can_send_sms_to_number($mobile_phone)) {
              // Meaning, the user has opted-out!  Disable fields and let the user know.
              $form ['engagement_msg']['attributes']['readonly'] = 'readonly';
              $form ['engagement_msg']['attributes']['class'] .= ' readonly';

              $form ['submit_submit']['attributes']['disabled'] = 'disabled';
              $form ['submit_submit']['attributes']['class'] .= ' button-disabled';
              $form ['submit_submit']['suffix'] = "<strong>" . t('NOTE: Cannot send a text message as this recipient has opted-out of receving text messages.  Try email or calling instead.') . "</strong>";


            }


            // The header needs to be a setting with replacement values           
            $sms_header = engagements_sms_replace_patterns(variable_get('sms_header', '(@initials) from @name:'), "", $user);

            $form ['engagement_msg']['prefix'] = t("Your message will automatically begin with <strong>@header</strong>", array("@header" => $sms_header));
            $form ['engagement_msg']['description'] = t("To insert emoji in Windows, press <span style='padding-left: 10px; padding-right: 10px; font-size: 1.3em;'><i class='fa fa-windows'></i> + .</span>  <em>(Windows key + period)</em>");


            $form ['attachment']['attributes'] = array('class' => 'hidden');
          }


          $form ['mark_to'] = array(
            'type' => 'markup',
            'value' => "<div class='engagement-field-mark engagement-to'><label>" . t("To") . ":</label>
                               " . $db->get_student_name($form ['student_id']['value'], TRUE) . "<span class='engagement-student-extra-mark'>$extra_mark</span>
                        </div>",
            'weight' => $form ['student_id']['weight'],
          );
        }




        // Always set the published = TRUE, and hide.        
        $form ['published']['value'] = TRUE;
        $form ['published']['attributes'] = array('class' => 'hidden');




        // Add a validate handler to form, so we can name sure email or mobile phone numbers are valid.
        $form ['#validate_handlers'][] = 'engagements_send_email_or_txt_form_validate';

        // Add a submit handler to form, so we can send email or txt messages
        $form ['#submit_handlers'][] = 'engagements_send_email_or_txt_form_submit';
      } // cid == new




      if ($form ['engagement_type']['value'] != 'txt_msg') {
        // If this is anything OTHER than a txt_msg, we will show the tinymce editor for the Message
        $form ['engagement_msg']['type'] = 'textarea_editor';
        // Also get rid of the sms number field.
        $form ['from_sms_phone'] = array(
          'type' => 'hidden',
          'value' => '',
          'required' => FALSE,
        );
      }

      if ($form ['engagement_type']['value'] != 'txt_msg' && $form ['engagement_type']['value'] != 'email') {
        // Set visibility to 'faculty' by default.
        $form ['visibility']['value'] = 'faculty';
      }
      else {
        $form ['visibility']['value'] = 'public';
        $form ['visibility']['attributes'] = array('class' => 'hidden');
      }
    } // form type == engagement
  } // content_edit_content_form











}