19

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

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:

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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].