Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [MOD] Links MOD for ML (multiple language) package  (Read 2260 times)

henrich

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 381
  • Passion for quality and excellence!
    • My personal blog and portofolio
[MOD] Links MOD for ML (multiple language) package
« on: January 30, 2008, 07:31:41 am »

Hi community folks  :D,

This MOD is based on the Links MOD (http://snewscms.com/forum/index.php?topic=5105.0) created by bakercad.

Bob's MOD had a weak point under the ML package, every time when i tryed to edit a link, i received an error message that the link id could not be found. So i realised that i need to adapt this MOD to the ML package (created and released by Rui Mendes). All of you who wants to use the Links MOD in the ML package should do the following things (same steps as Bob wrote on 2007-06-22, 04:27:27, i noted with blue the new necesary code):

- BACKUP your files!

Step 1. add links database table
remember to add your prefix_ is you use one in your snews installation.

Quote
CREATE TABLE `prefix_links` (
  `id` int(3) NOT NULL auto_increment,
  `url` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `target` varchar(255) default NULL,
  `description` varchar(255) default NULL,
  `linkorder` int(3) NOT NULL,
  `lang` varchar(6) default 'EN',
  `visible` enum('YES','NO') NOT NULL default 'YES',
  PRIMARY KEY  (`id`)
) ;

* - due to the multiple languages, we need the new lang field.

Step 2. add classes to style.css (or your own CSS file)

Quote
/* LINKS */
.midlength {width: 40%;}
.biglength {width: 75%;}


* - this can be different at you, you can write your own css rules.

STEP 3. snews.php - Language variable changes/additions

Find $l['cat_listSEF'] and add the blue

Quote
$l['cat_listSEF'] = $l['home_sef'].',archive,contact,sitemap,rss-articles,rss-pages,rss-comments,login,administration,
admin_category,admin_article,article_new,extra_new,page_new,categories,articles,extra_contents,
pages,settings,files,logout,admin_links'; //SEF links of the hardcoded categories

at the end of language variables add these new varialbes
Quote
#links
   $l['admin_links'] = 'Links';
   $l['edit_link'] = 'Edit Link';
   $l['new_link'] = 'New Link';
   $l['addnew_link'] = 'Add New Link';
   $l['exist_links'] = 'Existing Links';
   $l['link_edit_noid'] = 'No ID was provided';
   $l['link_edit_wrongid'] = 'ID provided was not found';
   $l['link_url'] = 'URL Address';
   $l['link_name'] = 'Name';
   $l['link_desc'] = 'Description';
   $l['link_target'] = 'Target: (WARNING choosing "New Window" will make your code invalid for "Strict" document type)';
   $l['link_target_blank'] = 'New Window';
   $l['link_target_none'] = 'None';
   $l['link_url_exists'] = 'This URL already exists!';
   $l['link_url_invalid'] = 'Invalid URL, please make sure the URL entered is formatted correctly(example... www.solucija.com, http://solucija.com)!';
   $l['link_info_empty'] = 'URL Address or Name were left empty, please try again.';
   $l['no_links'] = 'No Links at the moment';
   $l['blogroll'] = 'Blogroll';
   
   #general - can be used for other things
   $l['up'] = 'Up';
   $l['down'] = 'Down';
   $l['please_wait'] = 'Please wait while you are redirected';

STEP 4. snews.php - NEW FUNCTIONS

Quote
// LINKS FORM
function form_links($action) {
   if ($action == '' || ($action == 'edit' && is_numeric($_GET['id']) && !is_null($_GET['id']))) {
      $id = $_GET['id'];
      if ($action == 'edit'){$wrong_id = retrieve('id','links','id',$id);$wrong_id = $wrong_id == '' ? '' : 'no';}
      else {$wrong_id = 'no';}
      if ($wrong_id === 'no'){
         if ($action == 'edit') {
            $query = "SELECT * FROM ".db('prefix')."links WHERE id = '$id' AND lang='".$_SESSION[db('website').'lang']."'";
            $result = mysql_query($query);
            $r = mysql_fetch_array($result);
            $selected_none = $r['target'] == 'none' ? ' selected="selected"' : '';
            $selected_blank = $r['target'] == 'blank' ? ' selected="selected"' : '';
         }
         if ($action == '') {
            $selected_none = $_POST['link_target'] == 'none' ? ' selected="selected"' : '';
            $selected_blank = $_POST['link_target'] == 'blank' ? ' selected="selected"' : '';
         }
         $fieldname = $action == 'edit' ? l('edit_link') : l('new_link');
         $url_post = isset($_POST['link_url']) ? $_POST['link_url']: 'http://';
         $url = $action == 'edit' ? $r['url'] : $url_post;
         $name = $action == 'edit' ? $r['name'] : $_POST['link_name'];
         $desc = $action == 'edit' ? $r['description'] : $_POST['link_desc'];
         $addnew = $action == 'edit' ? '<p><a href="admin_links/" title="'.l('addnew_link').'">'.l('addnew_link').'</a></p>' : '';
         $frm_action = $action == 'edit' ? db('website').'index.php?action=process&task=admin_link&id='.$id : db('website').'index.php?action=process&task=admin_link' ;
         $frm_lang = $_SESSION[db('website').'lang'];
         $frm_submit = $action == 'edit' ? 'edit_link' : 'add_link';
         echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', $frm_action);
         echo html_input('fieldset', '', '', '', '', '', '', '', '', '', '', '', '', '', $fieldname);
         echo $addnew;
         echo '<p>'.l('required').'</p>';
         echo html_input('text', 'link_url', 'url', $url, '* '.l('link_url'), 'midlength', '', '', '', '', '', '', '', '', '');
         echo html_input('text', 'link_name', 'name', $name, '* '.l('link_name'), 'biglength', '', '', '', '', '', '', '', '', '');
         echo html_input('text', 'link_desc', 'desc', $desc, l('link_desc'), 'biglength', '', '', '', '', '', '', '', '', '');
         echo '<p><label for="tr">'.l('link_target').'</label>
<select name="link_target" id="tr">';
         echo '<option value="none"'.$selected_none.'>'.l('link_target_none').'</option>';
         echo '<option value="blank"'.$selected_blank.'>'.l('link_target_blank').'</option>';
         echo '</select></p>';
            echo '<p>';
   $langlist = explode(',',s('langlist'));
   $llanglist = '<label for="langbox">'.l('choselang').':</label> <select name="langused" id="langbox">';
   foreach($langlist as $llang) {
      $llanglist .= '<option value="'.$llang.'"';
      if($llang == $frm_lang) $llanglist .= ' selected ';
      $llanglist .= '>'.$llang.'</option>';}
   $llanglist .= '</select>';
   echo '</p>';
   echo $llanglist;
   $_SESSION[db('website').'lang']= $frm_lang;
   echo '<br /><br />';

         echo html_input('hidden', 'id', 'id', $id, '', '', '', '', '', '', '', '', '', '', '');
         echo html_input('submit', $frm_submit, $frm_submit, l('save'), '', 'button', '', '', '', '', '', '', '', '', '');
         echo '</fieldset></form>';
         echo html_input('fieldset', '', '', '', '', '', '', '', '', '', '', '', '', '', l('exist_links'));
         echo '<ul>';
         echo links('900','admin');
         echo '</ul>';
         echo '</fieldset>';
      }
      else { echo notification(1,l('link_edit_wrongid'),'admin_links');}
   }
   else { echo notification(1,l('link_edit_noid'),'admin_links');}
}

// LINKS
function links($limit,$admin) {
   $query = "SELECT * FROM ".db('prefix')."links WHERE visible = 'YES' AND lang='".$_SESSION[db('website').'lang']."' ORDER BY linkorder ASC LIMIT $limit";
   $result = mysql_query($query);
   $num = mysql_num_rows($result);
   if ($num < 1) { echo '<li>'.l('no_links').'</li>';}
   else {
      while ($r = mysql_fetch_array($result)) {
         $up_down = $admin == 'admin' && $num > '1' ? '<a href="'.db('website').'index.php?action=process&task=moveup&item=links&id='.$r['id'].'">'.l('up').'</a> | <a href="'.db('website').'index.php?action=process&task=movedown&item=links&id='.$r['id'].'">'.l('down').'</a> '.l('divider').' ': '' ;
         $admin_items = $admin == 'admin' ? ' ('.$r['url'].') '.l('divider').' <a href="index.php?action=edit_link&id='.$r['id'].'">'.l('edit').'</a> '.l('divider').' <a href="?action=del_link&id='.$r['id'].'" onclick="javascript: return pop()">'.l('delete').'</a> ' : '' ;
         $title = !empty($r['description']) ? $r['description'] : $r['name'] ;
         $target = $r['target'] == 'blank' ? ' target="_blank"' : '';
         echo '<li>'.$up_down.'<a href="'.$r['url'].'" title="'.$title.'"'.$target.'>'.$r['name'].'</a>'.$admin_items.'</li>';
      }
   }
}

//DELETE LINK
function del_link() {
   $id = $_GET['id'];
   $linkorder = retrieve('linkorder','links','id',$id);
    mysql_query("DELETE FROM ".db('prefix')."links WHERE id = $id LIMIT 1;");
   $query = "SELECT id,linkorder FROM ".db('prefix')."links WHERE linkorder > $linkorder";
   $result = mysql_query($query);
   $num = mysql_num_rows($result);
   while ($r = mysql_fetch_array($result)) {
         $new_order = ($r['linkorder'] - 1);
         $update = mysql_query("UPDATE ".db('prefix')."links SET linkorder = '$new_order' WHERE id = '".$r['id']."'");
   }
   echo notification(0,'','admin_links');
}

STEP 5. snews.php - center function

Add the blue red
Quote
case 'files': files(); return; break;

case 'admin_links': form_links(''); return; break;
case 'edit_link': form_links('edit'); return; break;
case 'del_link': del_link(); return; break;


case 'process': processing(); return; break;

STEP 6. snews.php - administration function
FIND

Quote
echo '<p><a href="files/" title="'.l('files').'">'.l('files').'</a></p></fieldset>';
CHANGE TO
Code: [Select]
//echo '<p><a href="files/" title="'.l('files').'">'.l('files').'</a></p></fieldset>';
echo '<p><a href="files/" title="'.l('files').'">'.l('files').'</a></p>';
echo '<p><a href="admin_links/" title="'.l('admin_links').'">'.l('admin_links').'</a></p></fieldset>';

STEP 7. snews.php - processing function
ADD new cases:
Quote
case 'admin_link';
         if (empty($_POST['link_url']) || empty($_POST['link_name'])) { // check that URL or Name are not empty
            echo notification(1,l('link_info_empty')); $form_link = isset($_POST['add_link']) ? form_links('') : form_links('edit');// URL or Name are empty display error message
         }
         else {  // URL or Name not empty
            $url = strpos($_POST['link_url'],'http://'); // checks for http:// in URL
            $url = $url === false ? $url = 'http://'.$_POST['link_url'] : $_POST['link_url'] ; // adds http:// to beginning of URL if left out
            if (preg_match("/^http(s?)+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/\-_]+$/i", $url)) { // checks that URL is valid
               $url_check = retrieve('url','links','url',$url); // see if URL already exists
               if (isset($_POST['add_link']) && $url_check){ //URL Exists!
                  echo notification(1,l('link_url_exists').' (<strong>'.$_POST['link_url'].'</strong>)'); unset($_POST['link_url']);$form_link = isset($_POST['add_link']) ? form_links('') : form_links('edit');  // displays error message
               }
               else { // URL doesn't exist....process it.
                  $query = "SELECT linkorder FROM ".db('prefix')."links ORDER BY linkorder DESC LIMIT 1"; // finds highest linkorder number
                  $result = mysql_query($query) or die(mysql_error() .'
'.$query);
                  $r = mysql_fetch_array($result);
                  $num = mysql_num_rows($result);
                  $order = $num < '1' ? '1' : $r['linkorder'] + 1 ; //if not linkorder found, not links exist, new linkorder number is 1, else, add 1 to the linkorder number
                  switch (true){
                     case (isset($_POST['add_link'])):
                       mysql_query("INSERT INTO ".db('prefix')."links (url,name,target,description,linkorder,lang) VALUES('$url','".$_POST['link_name']."','".$_POST['link_target']."','".$_POST['link_desc']."','$order','$langused')");
                     break;
                     case (isset($_POST['edit_link'])):
                        mysql_query("UPDATE ".db('prefix')."links SET url='$url',name='".$_POST['link_name']."',target='".$_POST['link_target']."',description='".$_POST['link_desc']."',lang='$langused' WHERE id = '$id' ");
                     break;
                  }
                  echo notification(0,'','admin_links'); //display success and link back to link admin panel
               }
            }
            else { // URL Invalid!
               echo notification(1,l('link_url_invalid')); $form_link = isset($_POST['add_link']) ? form_links('') : form_links('edit');// display error message
            }
         }
      break;
      case 'moveup';
            $id_a = $_GET['id'];
         $item = $_GET['item'];
         switch ($item) {
            case 'links': $order = 'linkorder';   $link = 'admin_links'; break;
            case 'pages': $order = 'pageorder'; $link = 'pages';  $item = 'articles'; break; // for compatibility with updated "Change Order of Pages MOD
         }
            $ordernum = retrieve($order,$item,'id',$id_a);
            if ($ordernum > 1) {
            $ordernum_a = $ordernum-1;
                $id = retrieve('id',$item,$order,$ordernum_a);
                $query1 = "UPDATE ".db('prefix')."$item SET $order = '$ordernum' WHERE id = '$id'";
                $result1 = mysql_query($query1) or die (mysql_error()."
Couldn't execute query: $query1");
               
                $query2 = "UPDATE ".db('prefix')."$item SET $order = '$ordernum_a' WHERE id = '$id_a'";
                $result2 = mysql_query($query2) or die (mysql_error()."
Couldn't execute query: $query2");
               
            }
         echo notification(0,l('please_wait'));
         echo '<meta http-equiv="refresh" content="1; url='.db('website').$link.'/">';
        break;
        case 'movedown';
            $id_a = $_GET['id'];
         $item = $_GET['item'];
         switch ($item) {
            case 'links': $order = 'linkorder'; $link = 'admin_links'; break;
            case 'pages': $order = 'pageorder'; $link = 'pages';  $item = 'articles'; break; // for compatibility with updated "Change Order of Pages MOD
         }
            $ordernum = retrieve($order,$item,'id',$id_a);
           
            $query = "SELECT $order FROM ".db('prefix')."$item ORDER BY $order DESC LIMIT 1 ";
                $result = mysql_query($query) or die (mysql_error()."
Couldn't execute query: $query");
                $row = mysql_fetch_array($result);
                $high_ordernum = $row[$order];
               
            if ($ordernum < $high_ordernum) {
           
                $ordernum_a = $ordernum+1;
                $id = retrieve('id',$item,$order,$ordernum_a);
               
                $query1 = "UPDATE ".db('prefix')."$item SET $order = '$ordernum' WHERE id = '$id'";
                $result1 = mysql_query($query1) or die (mysql_error()."
Couldn't execute query: $query1");
               
                $query2 = "UPDATE ".db('prefix')."$item SET $order = '$ordernum_a' WHERE id = '$id_a'";
                $result2 = mysql_query($query2) or die (mysql_error()."
Couldn't execute query: $query2");
               
            }
         echo notification(0,l('please_wait'));
         echo '<meta http-equiv="refresh" content="1; url='.db('website').$link.'/">';
        break;

USAGE index.php - Bob insert them BELOW "New Comments"

Quote
<h3>New comments:</h3>
<ul>
   <?php new_comments(5, 30); ?>
</ul>
<h3><?php echo l('blogroll');?>:</h3>
<ul>
   <?php links(5); ?> //change the number (5) to however many links you'd like to display
</ul>

so, now we have a links MOd for the ML package, if site is under english, you will see only the links for english, if you switch under french, you will see the french links and so on. A snapshoot is available also:



Please test this MOD, if you find errors then maybe i forgot to add some blue data :P, just write here and we gonna solve the problem. Thanks for Bob and thank you for reading my post. Have a nice day  ;D
« Last Edit: January 30, 2008, 11:17:12 pm by henrich »
Logged
By(e) Henrich :)
------------------------------
IT related blog