1

Editing a SMS

Code

Create the file filtersms.php in the folder library with the following content:

  1. function filtersms($s) {
  2.     $charset7bit = "@£\$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
  3.     $charset7bitext = "\f^{}\\[~]|€";
  4.  
  5.     $s = preg_replace('/\R/', "\r\n", trimsms($s));
  6.  
  7.     if (preg_match('/[^' . preg_quote($charset7bit . $charset7bitext, '/') . ']/', $s)) {
  8.         $coding='16bit';
  9.         $maxlen=70;
  10.         $extlen=67;
  11.     }
  12.     else {
  13.         $coding='7bit';
  14.         $maxlen=160;
  15.         $extlen=153;
  16.  
  17.         $s = preg_replace('/([' . preg_quote($charset7bitext, '/') . '])/u', "\e\\1", $s);
  18.     }
  19.  
  20.     $msglen = mb_strlen($s, 'UTF-8');
  21.  
  22.     if ($msglen > $maxlen) {
  23.         $count = 1 + floor($msglen / $extlen);
  24.         $len = $count * $extlen;
  25.     }
  26.     else {
  27.         $count = 1;
  28.         $len = $maxlen;
  29.     }
  30.  
  31.     return array($s, $coding, $msglen, $count, $len);
  32. }
  33.  
  34. function trimsms($s) {
  35.     return preg_replace('/ *(\R)+/', '\1', preg_replace('/^ /m', '', preg_replace('/[ \t]+/', ' ', trim($s))));
  36. }

Edit the file views/nodecontent.phtml:

  1.         case 'sms':
  2.             $text = $c['text'];
  3.             if ($text) {
  4.                 require_once 'filtersms.php';
  5.  
  6.                 list(, $coding, $msglen, $count, $len)=filtersms($text);
  7.  
  8.                 echo '<div class="sms">', PHP_EOL;
  9.                 echo '<p id="sms_params">', PHP_EOL;
  10.                 echo "$msglen / $len &nbsp;-&nbsp $count SMS $coding", PHP_EOL;
  11.                 echo '</p>', PHP_EOL;
  12.                 echo '<p>', nl2br(htmlspecialchars($text, ENT_COMPAT, 'UTF-8')), '</p>', PHP_EOL;
  13.                 echo '</div>', PHP_EOL;
  14.             }
  15.             break;

Modify the view smseditor in English and in French in the files views/en/smseditor.phtml and views/fr/smseditor.phtml:

  1. <p class="inlabel<?php if ($missing_text): ?> inerror<?php endif; ?>">Message:</p>
  2. <textarea id="sms_text" name="sms_text" cols="80" rows="5" title="SMS" placeholder="" onkeyup="return filtersms()"><?php echo htmlspecialchars($sms_text, ENT_COMPAT, 'UTF-8'); ?></textarea>
  3. <br/><span class="grippie btn_edit btn_grippie">===</span>
  4. <p id="sms_params">
  5. <span id="sms_msglen"><?php echo $sms_msglen; ?></span>
  6. /
  7. <span id="sms_len"><?php echo $sms_len; ?></span>
  8. &nbsp;-&nbsp;
  9. <span id="sms_count"><?php echo $sms_count; ?></span>
  10. SMS
  11. <span id="sms_coding"><?php echo $sms_coding; ?></span>
  12. </p>
  1. <script type="text/javascript">
  2. //<![CDATA[
  3. function filtersms() {
  4.     var charset7bit = "@£\$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
  5.     var charset7bitext = "\f^{}\\[~]|€";
  6.  
  7.     input = document.getElementById('sms_text');
  8.     if (input == null)
  9.         return;
  10.  
  11.     s = input.value.trim();
  12.  
  13.     s = s.replace(/[ \t]+/gm, ' ').replace(/^ /gm, '').replace(/ *\n+/gm, "\r\n");
  14.  
  15.     msglen = s.length;
  16.  
  17.     if (s.match(new RegExp('[^' + preg_quote(charset7bit + charset7bitext, '/') + ']'))) {
  18.         coding='16bit';
  19.         maxlen=70;
  20.         extlen=67;
  21.     }
  22.     else {
  23.         coding='7bit';
  24.         maxlen=160;
  25.         extlen=153;
  26.  
  27.         if (r = s.match(new RegExp('[' + preg_quote(charset7bitext, '/') + ']', 'g'))) {
  28.             msglen += r.length;
  29.         }
  30.     }
  31.  
  32.     if (msglen > maxlen) {
  33.         count = 1 + Math.floor(msglen / extlen);
  34.         len = count * extlen;
  35.     }
  36.     else {
  37.         count = 1;
  38.         len = maxlen;
  39.     }
  40.  
  41.     document.getElementById('sms_msglen').innerHTML=msglen;
  42.     document.getElementById('sms_len').innerHTML=len;
  43.     document.getElementById('sms_count').innerHTML=count;
  44.     document.getElementById('sms_coding').innerHTML=coding;
  45. }
  46.  
  47. function preg_quote (s, delim) {
  48.     return s.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + delim + '-]', 'g'), '\\$&');
  49. }
  50. //]]>
  51. </script>

The version in French is nearly identical except for the label and the title of the text area. The code in JavaScript is the same:

Edit the file blocks/smseditor.php:

  1. require_once 'filtersms.php';
  1.             if (isset($_POST['sms_text'])) {
  2.                 $sms_text=trimsms(readarg($_POST['sms_text'], false, false));   // don't trim and DON'T strip!
  3.             }
  1.     list(, $sms_coding, $sms_msglen, $sms_count, $sms_len)=filtersms($sms_text);
  1.     $output = view('smseditor', $lang, compact('clang', 'inlanguages', 'sms_modified', 'sms_title', 'sms_cloud', 'sms_subject', 'sms_text', 'sms_msg', 'sms_coding', 'sms_msglen', 'sms_count', 'sms_len', 'errors'));

Add the CSS for the display of the SMS properties at the end of the file css/theme.css:

#sms_params {font-size:small;}
Test

Connect with the identifier barfoo. Display the campaign The web engine.

Message

97 / 160  -  1 SMS 7bit

Create your multimedia website in a few minutes with iZend - The web engine: http://www.izend.org

Edit the campaign.

Message:


===

97 / 160  -  1 SMS 7bit

Click in the input area of the SMS at the end of the line. The counter is at 97. Press Enter twice to add a blank line. The counter doesn't change.

Type Make $. As soon as you have typed the M, the counter jumped to 100, 97 + 2 x the 2 characters CR (CARRIAGE RETURN) and LF (LINEFEED) for the two end of lines + 1 for the M. The counter is at 103.

Type a space. The counter doesn't change.

Enter a $ (DOLLAR). The counter jumps to 105, 1 for the space + 1 for the $.

Replace the $ with a € (EURO). The counter jumps to 106. 1 € takes 2 characters.

Add spaces between words, at the beginning or at the end of lines. The counter doesn't change.

Add the line русский язык. The SMS switches to 16bit encoding. Sending the message will take 2 SMS of 67 characters maximum each.

Press Configure to check that the code in PHP does the same calculations.

Display the campaign.

Git
  1. /izendsms.com
    1. blocks
      1. smseditor.php
    2. css
      1. theme.css
    3. library
      1. filtersms.php
    4. views
      1. nodecontent.phtml
      2. en
        1. smseditor.phtml
      3. fr
        1. smseditor.phtml

Commit this version:

$ git status
$ git add blocks/smseditor.php css/theme.css library/filtersms.php views/en/smseditor.phtml views/fr/smseditor.phtml views/nodecontent.phtml
$ git commit -m'Shows message properties when editing or displaying a SMS'

IMPORTANT: Edit the DB connector in the file includes/db.inc.

Comments

To add a comment, click here.