1
1

Specializing the editor

Code

Edit the file actions/campaigneditpage.php:

  1.     global $with_toolbar;
  1.     $node_editor = build('smseditor', $lang, $clang, $node_id);

Create the file smseditor.php in the folder blocks with the following content:

  1. require_once 'readarg.php';
  2. require_once 'strtofname.php';
  3. require_once 'models/sms.inc';
  4.  
  5. function smseditor($lang, $clang, $node_id) {
  6.     $action='init';
  7.     if (isset($_POST['sms_configure'])) {
  8.         $action='configure';
  9.     }
  10.  
  11.     $sms_modified=$sms_title=$sms_name=$sms_cloud=$sms_subject=$sms_text=false;
  12.  
  13.     switch($action) {
  14.         case 'init':
  15.             $r = sms_get_campaign($clang, $node_id);
  16.             if ($r) {
  17.                 extract($r);    /* sms_modified sms_title sms_name sms_cloud sms_subject sms_text */
  18.             }
  19.             break;
  20.  
  21.         case 'configure':
  22.             if (isset($_POST['sms_modified'])) {
  23.                 $sms_modified=filter_var($_POST['sms_modified'], FILTER_VALIDATE_INT, array('options' => array('min_range' => 0)));
  24.             }
  25.             if (isset($_POST['sms_title'])) {
  26.                 $sms_title=readarg($_POST['sms_title']);
  27.                 $sms_name = strtofname($sms_title);
  28.             }
  29.             if (isset($_POST['sms_cloud'])) {
  30.                 $sms_cloud=readarg($_POST['sms_cloud'], true, false);   // trim but DON'T strip!
  31.                 preg_match_all('/(\S+)/', $sms_cloud, $r);
  32.                 $sms_cloud=implode(' ', array_unique($r[0]));
  33.             }
  34.             if (isset($_POST['sms_subject'])) {
  35.                 $sms_subject=readarg($_POST['sms_subject']);
  36.             }
  37.             if (isset($_POST['sms_text'])) {
  38.                 $sms_text=readarg($_POST['sms_text']);
  39.             }
  40.             break;
  41.         default:
  42.             break;
  43.     }
  44.  
  45.     $missing_title=false;
  46.     $bad_title=false;
  47.  
  48.     $missing_subject=false;
  49.  
  50.     $missing_text=false;
  51.  
  52.     switch($action) {
  53.         case 'configure':
  54.             if (!$sms_title or !$sms_name) {
  55.                 $missing_title = true;
  56.             }
  57.             else if (!preg_match('#^\w+(-\w+)*$#', $sms_name)) {
  58.                 $bad_title = true;
  59.             }
  60.  
  61.             if (!$sms_subject) {
  62.                 $missing_subject = true;
  63.             }
  64.  
  65.             if (!$sms_text) {
  66.                 $missing_text = true;
  67.             }
  68.  
  69.             break;
  70.  
  71.         default:
  72.             if (!$sms_text) {
  73.                 $missing_text = true;
  74.             }
  75.  
  76.             break;
  77.     }
  78.  
  79.     switch($action) {
  80.         case 'configure':
  81.             if ($missing_title or $bad_title or $missing_text) {
  82.                 break;
  83.             }
  84.  
  85.             $r = sms_set_campaign($clang, $node_id, $sms_title, $sms_name, $sms_cloud, $sms_subject, $sms_text);
  86.  
  87.             if (!$r) {
  88.                 $internal_error=true;
  89.                 break;
  90.             }
  91.  
  92.             $sms_modified=time();
  93.  
  94.             break;
  95.  
  96.         default:
  97.             break;
  98.     }
  99.  
  100.     $inlanguages=view('inlanguages', false, compact('clang'));
  101.  
  102.     $errors = compact('internal_error', 'missing_title', 'bad_title', 'missing_subject', 'missing_text');
  103.  
  104.     $output = view('smseditor', $lang, compact('clang', 'inlanguages', 'sms_modified', 'sms_title', 'sms_cloud', 'sms_subject', 'sms_text', 'errors'));
  105.  
  106.     return $output;
  107. }

Add the functions sms_get_campaign and sms_set_campaign in the file models/sms.inc :

  1. function sms_get_campaign($lang, $node_id) {
  2.     $sqllang=db_sql_arg($lang, false);
  3.  
  4.     $tabnode=db_prefix_table('node');
  5.     $tabnodelocale=db_prefix_table('node_locale');
  6.     $tabnodecontent=db_prefix_table('node_content');
  7.     $tabcontentsms=db_prefix_table('content_sms');
  8.  
  9.     $sql="SELECT nloc.name AS sms_name, nloc.title AS sms_title, nloc.abstract AS sms_subject, nloc.cloud AS sms_cloud, UNIX_TIMESTAMP(n.modified) AS sms_modified, c.text AS sms_text FROM $tabnode n JOIN $tabnodelocale nloc ON nloc.node_id=n.node_id AND nloc.locale=$sqllang JOIN $tabnodecontent nc ON nc.node_id=n.node_id AND nc.content_type='sms' AND nc.ignored=FALSE LEFT JOIN $tabcontentsms c ON c.content_id=nc.content_id AND c.locale=nloc.locale WHERE n.node_id=$node_id";
  10.  
  11.     $r = db_query($sql);
  12.  
  13.     return $r ? $r[0] : false;
  14. }
  1. function sms_set_campaign($lang, $node_id, $sms_title, $sms_name, $sms_cloud, $sms_subject, $sms_text) {
  2.     $tabnode=db_prefix_table('node');
  3.  
  4.     $sql="UPDATE $tabnode SET modified=NOW() WHERE node_id=$node_id";
  5.  
  6.     $r = db_update($sql);
  7.  
  8.     if ($r === false) {
  9.         return false;
  10.     }
  11.  
  12.     $sqllang=db_sql_arg($lang, false);
  13.     $sqlname=db_sql_arg($sms_name, true);
  14.     $sqltitle=db_sql_arg($sms_title, true, true);
  15.     $sqlcloud=db_sql_arg($sms_cloud, true, true);
  16.     $sqlabstract=db_sql_arg($sms_subject, true, true);
  17.  
  18.     $tabnodelocale=db_prefix_table('node_locale');
  19.  
  20.     $sql="INSERT INTO $tabnodelocale (node_id, locale, name, title, abstract, cloud) SELECT * FROM (SELECT $node_id AS node_id, $sqllang AS locale, $sqlname AS name, $sqltitle AS title, $sqlabstract AS abstract, $sqlcloud AS cloud) s WHERE NOT EXISTS (SELECT node_id FROM $tabnodelocale WHERE node_id=$node_id AND locale=$sqllang)";
  21.  
  22.     $r = db_insert($sql);
  23.  
  24.     if ($r === false) {
  25.         return false;
  26.     }
  27.  
  28.     if ($r === 0) {
  29.         $sql="UPDATE $tabnodelocale SET name=$sqlname, title=$sqltitle, abstract=$sqlabstract, cloud=$sqlcloud WHERE node_id=$node_id AND locale=$sqllang";
  30.  
  31.         $r = db_update($sql);
  32.  
  33.         if ($r === false) {
  34.             return false;
  35.         }
  36.     }
  37.  
  38.     $r = cloud_tag_node($lang, $node_id, $sms_cloud);
  39.  
  40.     if (!$r) {
  41.         return false;
  42.     }
  43.  
  44.     $tabnodecontent=db_prefix_table('node_content');
  45.  
  46.     $sql="SELECT nc.content_id FROM $tabnodecontent nc WHERE nc.node_id=$node_id AND nc.content_type='sms'";
  47.  
  48.     $r = db_query($sql);
  49.  
  50.     if (!$r) {
  51.         return false;
  52.     }
  53.  
  54.     $content_id=$r[0]['content_id'];
  55.  
  56.     $sqltext=db_sql_arg($sms_text, true, true);
  57.  
  58.     $tabcontentsms=db_prefix_table('content_sms');
  59.  
  60.     $sql="INSERT INTO $tabcontentsms (content_id, locale, text) SELECT * FROM (SELECT $content_id AS user_id, $sqllang AS locale, $sqltext AS text) s WHERE NOT EXISTS (SELECT $content_id FROM $tabcontentsms WHERE content_id=$content_id AND locale=$sqllang)";
  61.  
  62.     $r = db_insert($sql);
  63.  
  64.     if ($r) {
  65.         return true;
  66.     }
  67.  
  68.     $sql="UPDATE $tabcontentsms SET text=$sqltext WHERE content_id=$content_id AND locale=$sqllang";
  69.  
  70.     $r = db_update($sql);
  71.  
  72.     if ($r === false) {
  73.         return false;
  74.     }
  75.  
  76.     return true;
  77. }

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

  1. <?php if ($sms_modified): ?>
  2. <?php require_once 'dateen.php'; ?>
  3. <p class="info" title="Last modification"><?php echo longdate_en($sms_modified); ?> at <?php echo date('H:i', $sms_modified); ?></p>
  4. <?php endif; ?>
  5. <p class="info">Press the assistance button <span class="btn_edit btn_help" title="Help">help</span> to toggle the display of help messages.</p>
  6. <p class="help helptoggle">Slide the handle <span class="btn_edit btn_grippie">===</span> to extend a text area.</p>
  7. <?php extract($errors); ?>
  8. <form class="compact" action="" method="post">
  9. <input name="clang" type="hidden" value="<?php echo $clang; ?>" />
  10. <?php if ($sms_modified): ?>
  11. <input name="sms_modified" type="hidden" value="<?php echo $sms_modified; ?>" />
  12. <?php endif; ?>
  13. <p>
  14. <?php echo $inlanguages; ?>
  15. <span id="node_hidy" class="hidy">+</span>
  16. </p>
  17. <div id="node">
  18. <p class="inlabel<?php if ($missing_title or $bad_title): ?> inerror<?php endif; ?>">Title:</p>
  19. <p><input id="sms_title" name="sms_title" type="text" size="40" maxlength="100" value="<?php echo htmlspecialchars($sms_title, ENT_COMPAT, 'UTF-8'); ?>" title="Campaign title" onkeypress="return focusonenter(event, 'sms_cloud')"/></p>
  20. <p class="inlabel">Cloud:</p>
  21. <p>
  22. <textarea id="sms_cloud" name="sms_cloud" cols="80" rows="2" title="Series of keywords for the search engine"><?php echo htmlspecialchars($sms_cloud, ENT_COMPAT, 'UTF-8'); ?></textarea>
  23. <br/><span class="grippie btn_edit btn_grippie">===</span>
  24. </p>
  25. <p class="help helptoggle">
  26. Associate keywords to this campaign.
  27. </p>
  28. <p class="inlabel<?php if ($missing_subject): ?> inerror<?php endif; ?>">Subject:</p>
  29. <p>
  30. <textarea id="sms_subject" name="sms_subject" cols="80" rows="2" title="Just a paragraph"><?php echo htmlspecialchars($sms_subject, ENT_COMPAT, 'UTF-8'); ?></textarea>
  31. <br/><span class="grippie btn_edit btn_grippie">===</span>
  32. </p>
  33. <p class="help helptoggle">
  34. Describe the subject of the campaign.
  35. </p>
  36. </div>
  37. <p class="inlabel<?php if ($missing_text): ?> inerror<?php endif; ?>">Message:</p>
  38. <p>
  39. <textarea id="sms_text" name="sms_text" cols="80" rows="4" title="SMS" placeholder="Type in the text of the SMS..."><?php echo htmlspecialchars($sms_text, ENT_COMPAT, 'UTF-8'); ?></textarea>
  40. <br/><span class="grippie btn_edit btn_grippie">===</span>
  41. </p>
  42. <p class="help helptoggle">
  43. </p>
  44. <p><input type="submit" class="submit submit_configure<?php if ($missing_title or $bad_title): ?> inerror<?php endif; ?>" name="sms_configure" id="sms_configure" value="Configure" title="Configure the campaign" /></p>
  45. </form>
  46. <?php if (!$missing_title): ?>
  47. <?php head('javascript', 'jquery.cookie'); ?>
  48. <script type="text/javascript">
  49. if ($.cookie('hidenode') == 1) {
  50.     $('#node').hide();
  51. }
  52. $('#node_hidy').click(function() {
  53.     $('#node').toggle();
  54.     $.cookie('hidenode', $('#node').is(':hidden') ? 1 : 0, { path: '/' });
  55. });
  56. </script>
  57. <?php endif; ?>
  58. <?php
  59. $focus=false;
  60. if ($missing_title or $bad_title) {
  61.     $focus='#sms_title';
  62. }
  63. else if ($missing_text) {
  64.     $focus='#sms_text';
  65. }
  66. ?>
  67. <?php if ($focus): ?>
  68. <script type="text/javascript">
  69. $(document).ready(function() {
  70.     $('<?php echo $focus; ?>').focus();
  71. });
  72. </script>
  73. <?php endif; ?>
  74. <?php head('javascript', 'jquery.cookie'); ?>
  75. <script type="text/javascript">
  76. function togglehelp() {
  77.     $('.helptoggle').each(function() {
  78.         $(this).toggle();
  79.     });
  80.     $.cookie('nommshelp', $.cookie('nommshelp') == 1 ? '0' : '1');
  81. }
  82.  
  83. if ($.cookie('nommshelp') == 1) {
  84.     $('.helptoggle').each(function() {
  85.         $(this).hide();
  86.     });
  87. }
  88.  
  89. $('.btn_help').each(function() {
  90.     $(this).click(function() {togglehelp();});
  91. });
  92. </script>
  93. <script type="text/javascript">
  94. $(document).ready(function() {
  95.     $('.grippie').each(function() {
  96.         var grippie = $(this);
  97.         var textarea = grippie.prev().prev();
  98.         var offset;
  99.  
  100.         grippie.mousedown(startDrag);
  101.  
  102.         function startDrag(e) {
  103.           offset = textarea.height() - e.pageY;
  104.           textarea.css('opacity', 0.25);
  105.           $(document).mousemove(performDrag).mouseup(endDrag);
  106.           return false;
  107.         }
  108.  
  109.         function performDrag(e) {
  110.           textarea.height(Math.max(32, offset + e.pageY) + 'px');
  111.           return false;
  112.         }
  113.  
  114.         function endDrag(e) {
  115.           $(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
  116.           textarea.css('opacity', 1);
  117.         }
  118.     });
  119. });
  120. </script>
  1. <?php if ($sms_modified): ?>
  2. <?php require_once 'datefr.php'; ?>
  3. <p class="info" title="Dernière modification"><?php echo longdate_fr($sms_modified); ?> à <?php echo date('H:i', $sms_modified); ?></p>
  4. <?php endif; ?>
  5. <p class="info">Appuyez sur le bouton d'assistance <span class="btn_edit btn_help" title="Aide">aide</span> pour basculer l'affichage des messages d'aide.</p>
  6. <p class="help helptoggle">Glissez la poignée <span class="btn_edit btn_grippie">===</span> pour étendre une zone de saisie.</p>
  7. <?php extract($errors); ?>
  8. <form class="compact" action="" method="post">
  9. <input name="clang" type="hidden" value="<?php echo $clang; ?>" />
  10. <?php if ($sms_modified): ?>
  11. <input name="sms_modified" type="hidden" value="<?php echo $sms_modified; ?>" />
  12. <?php endif; ?>
  13. <p>
  14. <?php echo $inlanguages; ?>
  15. <span id="node_hidy" class="hidy">+</span>
  16. </p>
  17. <div id="node">
  18. <p class="inlabel<?php if ($missing_title or $bad_title): ?> inerror<?php endif; ?>">Titre&nbsp;:</p>
  19. <p><input id="sms_title" name="sms_title" type="text" size="40" maxlength="100" value="<?php echo htmlspecialchars($sms_title, ENT_COMPAT, 'UTF-8'); ?>" title="Titre de la campagne" onkeypress="return focusonenter(event, 'sms_cloud')"/></p>
  20. <p class="inlabel">Nuage&nbsp;:</p>
  21. <p>
  22. <textarea id="sms_cloud" name="sms_cloud" cols="80" rows="2" title="Série de mots clés pour le moteur de recherche"><?php echo htmlspecialchars($sms_cloud, ENT_COMPAT, 'UTF-8'); ?></textarea>
  23. <br/><span class="grippie btn_edit btn_grippie">===</span>
  24. </p>
  25. <p class="help helptoggle">
  26. Associez des mots clés à cette campagne.
  27. </p>
  28. <p class="inlabel<?php if ($missing_subject): ?> inerror<?php endif; ?>">Sujet&nbsp;:</p>
  29. <p>
  30. <textarea id="sms_subject" name="sms_subject" cols="80" rows="2" title="Juste un paragraphe"><?php echo htmlspecialchars($sms_subject, ENT_COMPAT, 'UTF-8'); ?></textarea>
  31. <br/><span class="grippie btn_edit btn_grippie">===</span>
  32. </p>
  33. <p class="help helptoggle">
  34. Décrivez le sujet de la campagne.
  35. </p>
  36. </div>
  37. <p class="inlabel<?php if ($missing_text): ?> inerror<?php endif; ?>">Message&nbsp;:</p>
  38. <p>
  39. <textarea id="sms_text" name="sms_text" cols="80" rows="4" title="SMS" placeholder="Tapez le texte du SMS..."><?php echo htmlspecialchars($sms_text, ENT_COMPAT, 'UTF-8'); ?></textarea>
  40. <br/><span class="grippie btn_edit btn_grippie">===</span>
  41. </p>
  42. <p class="help helptoggle">
  43. </p>
  44. <p><input type="submit" class="submit submit_configure<?php if ($missing_title or $bad_title): ?> inerror<?php endif; ?>" name="sms_configure" id="sms_configure" value="Configurer" title="Configurez la campagne" /></p>
  45. </form>
  46. <?php if (!$missing_subject): ?>
  47. <?php head('javascript', 'jquery.cookie'); ?>
  48. <script type="text/javascript">
  49. if ($.cookie('hidenode') == 1) {
  50.     $('#node').hide();
  51. }
  52. $('#node_hidy').click(function() {
  53.     $('#node').toggle();
  54.     $.cookie('hidenode', $('#node').is(':hidden') ? 1 : 0, { path: '/' });
  55. });
  56. </script>
  57. <?php endif; ?>
  58. <?php
  59. $focus=false;
  60. if ($missing_title or $bad_title) {
  61.     $focus='#sms_title';
  62. }
  63. else if ($missing_text) {
  64.     $focus='#sms_text';
  65. }
  66. ?>
  67. <?php if ($focus): ?>
  68. <script type="text/javascript">
  69. $(document).ready(function() {
  70.     $('<?php echo $focus; ?>').focus();
  71. });
  72. </script>
  73. <?php endif; ?>
  74. <?php head('javascript', 'jquery.cookie'); ?>
  75. <script type="text/javascript">
  76. function togglehelp() {
  77.     $('.helptoggle').each(function() {
  78.         $(this).toggle();
  79.     });
  80.     $.cookie('nosmshelp', $.cookie('nosmshelp') == 1 ? '0' : '1');
  81. }
  82.  
  83. if ($.cookie('nosmshelp') == 1) {
  84.     $('.helptoggle').each(function() {
  85.         $(this).hide();
  86.     });
  87. }
  88.  
  89. $('.btn_help').each(function() {
  90.     $(this).click(function() {togglehelp();});
  91. });
  92. </script>
  93. <script type="text/javascript">
  94. $(document).ready(function() {
  95.     $('.grippie').each(function() {
  96.         var grippie = $(this);
  97.         var textarea = grippie.prev().prev();
  98.         var offset;
  99.  
  100.         grippie.mousedown(startDrag);
  101.  
  102.         function startDrag(e) {
  103.           offset = textarea.height() - e.pageY;
  104.           textarea.css('opacity', 0.25);
  105.           $(document).mousemove(performDrag).mouseup(endDrag);
  106.           return false;
  107.         }
  108.  
  109.         function performDrag(e) {
  110.           textarea.height(Math.max(32, offset + e.pageY) + 'px');
  111.           return false;
  112.         }
  113.  
  114.         function endDrag(e) {
  115.           $(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
  116.           textarea.css('opacity', 1);
  117.         }
  118.     });
  119. });
  120. </script>

Add the CSS for the help messages at the end of the file css/theme.css:

.help {color:#666666;font-size:x-small;font-style:normal;margin-top:0.5em;line-height:16px;}
.help span.btn_grippie {margin:0;cursor:default;}
Test

Connect as a client and go to the campaigns.

Edit the campaign The web engine.

Modify the subject and the message of the campaign and press Configure.

If the title or the message is empty, an error is reported and the modification is rejected.

Message:


===

If the subject is empty, an error is reported but the modification is accepted.

Press on the flag to display the French version. Press on the flag to display the English version. IMPORTANT: The content of the editor is lost after switching to another language. Press Configure to validate your modifications.

Click on the triangle facing the language selector to show just the message. Click again on the triangle to display all the fields. NOTE: This option is preserved by the editor. If you don't see all the fields of a campaign, remember to click on the triangle to display them all.

Git
  1. /izendsms.com
    1. actions
      1. campaigneditpage.php
    2. blocks
      1. smseditor.php
    3. css
      1. theme.css
    4. models
      1. sms.inc
    5. views
      1. en
        1. smseditor.phtml
      2. fr
        1. smseditor.phtml

Commit this version:

$ git status
$ git add actions/campaigneditpage.php blocks/smseditor.php css/theme.css models/sms.inc views/en/smseditor.phtml views/fr/smseditor.phtml
$ git commit -m'Adds a dedicated campaign editor'

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

Comments

To add a comment, click here.