Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: Article Settings  (Read 1128 times)

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Article Settings
« on: August 22, 2010, 09:46:54 AM »

This mod is my interpretation of WordPress' Custom Fields function.


http://www.mediafire.com/?df3nat7j49zwdcm

Basic usage:
This works almost exactly like settings, except the article writer can define the setting and it's value, but the template writer places it in the temple, with the exception of placing it in the article itself (using the function tag).  This 'setting' works on the articles types as defined by sNews - id articles, pages, and extras.

get_metadata('string', $_ID, TRUE) - exact call as above outlining the parameter:
   parameter 1: The string or variable.
   parameter 2: The ID of the content this applies to - currently only for the articles table types - DEFAULT $_ID;
   parameter 3: Return the value as a string (TRUE) or as an array (FALSE) - DEFAULT TRUE

get_metadata('var'); Single call, single value returned
get_metadata('authors', $_ID, FALSE); - Calls for an array to be returned
get_metadata('image', $r['id'], TRUE); - Calls for an image setting, single value (TRUE), and in this example, in the extra while DB loop

For the admin:
When starting out, on the admin side, you just get the blank name/value and an add Field button.  The Add Field button will not work unless you have data in both fields in the last name/value combo.  

If you have names already present in the DB, then the select drop down will show, allowing you to select a pre-existing name (no, not it's value) which will automatically populate the name field.

If you submit the article and either the name or value is left blank, either adding or editing, the name/value combo gets DELETED!


Onto the code:

SQL:
Code: [Select]
CREATE TABLE `meta` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`article_id` INT( 11 ) NOT NULL ,
`name` VARCHAR( 255 ) NOT NULL ,
`value` LONGTEXT NOT NULL ,
PRIMARY KEY ( `id` ) ,
INDEX ( `article_id` , `name` )
);

Main code:

Before the closing ?> in snews.php
Code: (WITHOUT THE PHP OPENING & CLOSING TAGS) [Select]
<?php

function get_metadata$var$id ''$single TRUE ) {
$value '';
if (!empty($id)) {
$id $id;
} else {
global $_ID;
$id $_ID;
}

$key get_metadata_byId$id );

if ($single === TRUE) {
$value $key[$var][0];
} else {
$value $key[$var];
}
return $value;
}

function 
get_metadata_byId$id ) {
static $_id$array;
if ($_id !== $id) {
$_id $id;
$array = array(); 

$sql 'SELECT name, value FROM '._PRE.'meta WHERE article_id = '.$id;
$result mysql_query($sql);
if (!$result) return;
while ($r mysql_fetch_assoc($result)) {
$array[$r['name']][] = $r['value'];
}
}
return $array;
}

function 
get_all_metadata_byId$id ) {
static $array;
if (!$array) {
$sql 'SELECT id, name, value FROM meta WHERE article_id = '.$id.' ORDER BY id';
$result mysql_query($sql);
if (!$result) return;
while ($r mysql_fetch_assoc($result)) {
$array[$r['name']][] = array(
'id' => $r['id'], 
'value' => $r['value']
);
}
}
return $array;
}

function 
get_all_meta_names() {
static $array;
if (!$array) {
$sql 'SELECT name FROM '._PRE.'meta GROUP BY name';
$result mysql_query($sql);
if (!$result) return;
while ($r mysql_fetch_assoc($result)) {
$array[] = $r['name'];
}
}
return $array;
}

function 
meta_name_html_option($selected '') {
$list '';
$name_list get_all_meta_names();
$selectAttrib ' selected="selected"';

if (!empty($name_list)) {
$select = empty($selected
$selectAttrib 
'';
$list '<option value="" '.$select.'>Select</option>';

foreach ($name_list as $k => $v) {
$select = !empty($selected) && $selected == $v 
$selectAttrib 
'';
$list .= '<option value="'.$v.'" '.$select.'>'.$v.'</option>';
}
}

return $list;
}

function 
currSettingList($id '') {
$num 1;
$html '';

$array get_all_metadata_byId($id);
if (empty($array)) {
$html .= nameValueHTML($num);
} else {
foreach ($array as $k => $arr) {
foreach($arr as $key => $value) {
$html .= nameValueHTML($num
array(
'id' => $value['id'], 
'name' => $k
'value' => $value['value']
)
);
$num++;
}
}
}
return $html;
}

function 
nameValueHTML($num$array '') {
$name_list get_all_meta_names();
$html '<tr><td>
<input type="hidden" name="setting_id[]" value="'
.$array['id'].'" />
<input type="text" name="setting_name[]" id="setting_name_'
.$num.'" value="'.$array['name'].'" />';

if (!empty($name_list)) {
$html .= '<br /><select id="select_'.$num.'">';
$html .= meta_name_html_option($array['name']);
$html .= '</select>';
}

$html .='</td><td>
<textarea name="setting_value[]" id="setting_value_'
.$num.'" style="height: 100%" rows="2" cols="30">'.$array['value'].'</textarea>
</td></tr>'
;

return $html;
}

// Ajax request
if ( isset($_POST['nameValueHTML']) ) { 
echo nameValueHTML( (int) $_POST['nameValueHTML'] ); 
 
die();
}

function 
admin_article_settings($id) {
?>

<fieldset>
<legend class="toggle">Article Settings</legend>
<div style="display: none">
<table>
<thead>
<tr>
<td>Name</td>
<td>Value</td>
</tr>
</thead>
<tfoot id="new_settings"></tfoot>
<tbody id="current_settings">
<?php echo currSettingList($id); ?>
</tbody>
</table>
<button id="add_fields" type="button">Add new field</button>
</div>
</fieldset>
<?php
}

function 
post_setting_array() {
for ($n=0$n<count($_POST['setting_id']); $n++) {
$array[] = array(
'id' => $_POST['setting_id'][$n],
'name' => $_POST['setting_name'][$n],
'value' => $_POST['setting_value'][$n]
);
}
array_walk_recursive($array'trim');
array_walk_recursive($array'clean');
return $array;
}

function 
article_setting_js_header() {
if (_ADMIN) {
?>

<style type="text/css">
.toggle { color: #F74600; cursor: pointer; }
.toggle:hover { color:gray; cursor: pointer; }
td { vertical-align: top; }
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<?php
}
}

function 
article_setting_js_body_end() {
if (_ADMIN) {
?>

<script type="text/javascript">
$(document).ready(function() {

// Toggle div
$('.toggle').click(function() {
$('.toggle+div').toggle();
});

// Add Field Ajax Requests
$('#add_fields').click(function() {

var ids = [];
$('input[name="setting_name[]"]').each(function() {
var ID = $(this).attr("id");
var number = ID.substr(13,1);
ids.push(number);

});

if (
$('#setting_name_' + ids.length).val() != '' &&
$('#setting_value_' + ids.length).val() != ''
) {
number = ids.length + 1;


$.post(
"snews.php",
{ nameValueHTML : number },
function(data) {
$('#new_settings').append(data);
},
'text'
);

}
});

$('select').live('change', function() {
var selectID = $(this).attr("id");
var number = selectID.substr(7,1);
$("#setting_name_" + number).attr('value', $(this).val());
});

});
</script>
<?php
}
}

function 
article_settings_insert($array$seftitle) {
foreach( $array as $a) {
$a_id retrieve('id''articles''seftitle'$seftitle); 
$sql 'INSERT INTO '._PRE.'meta (article_id, name, value) VALUES ("'.$a_id.'", "'.$a['name'].'", "'.$a['value'].'")';
mysql_query($sql);
}
}

function 
article_settings_update($array$id) {
foreach( $array as $a) {
if ( empty($a['name']) || empty($a['value']) ) {
$sql 'DELETE FROM '._PRE.'meta WHERE id = "'.$a['id'].'"';
mysql_query($sql);

} elseif ( !empty($a['id']) ) {
$sql 'UPDATE '._PRE.'meta SET id = "'.$a['id'].'", article_id = "'.$id.'", name = "'.$a['name'].'", value = "'.$a['value'].'" WHERE id = "'.$a['id'].'"';
mysql_query($sql);

} else {
$sql 'INSERT INTO '._PRE.'meta (article_id, name, value) VALUES("'.$id.'", "'.$a['name'].'", "'.$a['value'].'")';
mysql_query($sql);
}
}
}

function 
article_settings_delete($id) {
$sql 'DELETE FROM '._PRE.'meta WHERE article_id = '.$id;
mysql_query($sql);
}


?>



In the index add to the header:
Code: [Select]
<?php article_setting_js_header(); ?>** Note - if you are already using jQuery, then you can remove the call in this function

In the index again, add before the closing </html>
Code: [Select]
<?php article_setting_js_body_end(); ?>
In function form_articles, add the first line before the existing lines
Code: (WITHOUT THE PHP OPENING & CLOSING TAGS) [Select]
<?php

admin_article_settings($id);

if ($contents == 'article_new' || $edit_option == 1) {
echo '<div class="adminpanel">';
echo html_input('fieldset''''''''''''''''''''''''''''<a title="'.l('publish_date').
'" onclick="toggle(\'admin_publish_date\')" style="cursor: pointer;">'.l('publish_date').'</a>');

?>

In function process, add the bottom line:
Code: (WITHOUT THE PHP OPENING & CLOSING TAGS) [Select]
<?php
case 'admin_article':
$array post_setting_array();

?>

Later down add where it notes // EQ Add until // EQ End
Code: (WITHOUT THE PHP OPENING & CLOSING TAGS) [Select]
<?php

switch (true) {
case (isset($_POST['add_article'])):
mysql_query("INSERT INTO "._PRE.'articles'."(
title, seftitle, text, date, category,
position, extraid, page_extra, displaytitle,
displayinfo, commentable, published, description_meta,
keywords_meta, show_on_home, show_in_subcats, artorder)
VALUES('
$title', '$seftitle', '$text', '$date', '$category',
'
$position', '$def_extra', '$page', '$display_title',
'
$display_info', '$commentable', '$publish_article',
'
$description_meta', '$keywords_meta', '$show_on_home',
'
$show_in_subcats', '$artorder')");

// EQ Add
article_settings_insert($array$seftitle);
// EQ End


break;
case (isset($_POST['edit_article'])):
$category $position == $category;
$old_pos retrieve('position','articles','id',$id);
// Only do this if page is changed to art/extra
if ($position != $old_pos && $old_pos == 3) {
$chk_extra_query "SELECT id FROM "._PRE.'articles'."
WHERE position = 2 AND category = -3 AND  page_extra = 
$id";
$chk_extra_sql mysql_query($chk_extra_query) or die(mysql_error('oops'));
if ($chk_extra_sql) {
while ($xtra mysql_fetch_array($chk_extra_sql)) {
$xtra_id $xtra['id'];
mysql_query("UPDATE "._PRE.'articles'." SET
category = '0', page_extra = ''
WHERE id = 
$xtra_id");
}
}
}
if ($fpost_enabled == true) {
$future =  "date = '$date',";
//allows backdating of article
$publish_article strtotime($date) < time() ? $publish_article;
}
mysql_query("UPDATE "._PRE.'articles'." SET
title='
$title',
seftitle = '
$seftitle',
text = '
$text',
"
.$future."
category = 
$category,
position = 
$position,
extraid = '
$def_extra',
page_extra = '
$page',
displaytitle = '
$display_title',
displayinfo = '
$display_info',
commentable = '
$commentable',
published = 
$publish_article,
description_meta = '
$description_meta',
keywords_meta = '
$keywords_meta',
show_on_home='
$show_on_home',
show_in_subcats='
$show_in_subcats',
artorder = '
$artorder'
WHERE id = 
$id LIMIT 1") or die(mysql_error());



// EQ Add
article_settings_update($array$id);
// EQ End


break;
case(isset($_POST['delete_article'])):
if ($position == 3) {
$chk_extra_query "SELECT id FROM "._PRE.'articles'."
WHERE position = 2 AND category = -3 AND  page_extra = 
$id";
$chk_extra_sql mysql_query($chk_extra_query) or die(mysql_error());
if ($chk_extra_sql) {
while ($xtra mysql_fetch_array($chk_extra_sql)) {
$xtra_id $xtra['id'];
mysql_query("UPDATE "._PRE.'articles'." SET category = '0',page_extra = '' WHERE id = $xtra_id");
}
}
}
mysql_query("DELETE FROM "._PRE.'articles'." WHERE id = $id");
mysql_query("DELETE FROM "._PRE.'comments'." WHERE articleid = $id");
if ($id == s('display_page')) {
mysql_query("UPDATE "._PRE.'settings'." SET
VALUE = 0 WHERE name = 'display_page'"
);
}


// EQ Add
article_settings_delete($id);
// EQ End

break;
}


?>
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Article Settings
« Reply #1 on: March 17, 2012, 01:24:45 PM »

Hello
I am try to track some notices errors,
In this mod I have

in admin_article_settings($id);
Undefined variable: id
 
and "Uninitialized string offset: 0" in the following 4 places

Quote
<input type="hidden" name="setting_id[]" value="'.$array['id'].'" />

<input type="text" name="setting_name[]" id="setting_name_'.$num.'" value="'.$array['name'].'" />';

$html .= meta_name_html_option($array['name']);

<textarea name="setting_value[]" id="setting_value_'.$num.'" style="height: 100%" rows="10" cols="40">'.$array['value'].'</textarea>

can anyone help about this Uninitialized string offset: 0 ?
Logged

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
Logged
Of all the things I have lost, it is my mind that I miss the most.

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: Article Settings
« Reply #3 on: March 17, 2012, 04:43:03 PM »

hmmm OK thanks, I fix it, but is not possible to do that with this mod, because you don't know  what strings you can add.
I change
Quote
function nameValueHTML($num, $array = '')

to this (I add space)
Quote
function nameValueHTML($num, $array = ' ')

and I have rid of the Uninitialized string offset: 0
Logged