# --- BEGIN PLUGIN HELP ---
For display information visit:
http://homeplatewp.com/TextCommerce/article/67/tc_product_display-001
# --- END PLUGIN HELP ---
';
echo '';
switch ($sort)
{
case 'id':
$sort_sql = 'ID '.$dir;
break;
case 'posted':
$sort_sql = 'Posted '.$dir;
break;
case 'title':
$sort_sql = 'Title '.$dir.', Posted desc';
break;
case 'section':
$sort_sql = 'Section '.$dir.', Posted desc';
break;
case 'category1':
$sort_sql = 'Category1 '.$dir.', Posted desc';
break;
case 'category2':
$sort_sql = 'Category2 '.$dir.', Posted desc';
break;
case 'status':
$sort_sql = 'Status '.$dir.', Posted desc';
break;
case 'author':
$sort_sql = 'AuthorID '.$dir.', Posted desc';
break;
case 'comments':
$sort_sql = 'comments_count '.$dir.', Posted desc';
break;
default:
$dir = 'desc';
$sort_sql = 'Posted '.$dir;
break;
}
$switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
$criteria = "section = 'store'";
if ($search_method and $crit)
{
$crit_escaped = doSlash($crit);
$critsql = array(
'id' => "ID = '$crit_escaped'",
'title_body' => "Title rlike '$crit_escaped' or Body rlike '$crit_escaped'",
'section' => "Section rlike '$crit_escaped'",
'categories' => "Category1 rlike '$crit_escaped' or Category2 rlike '$crit_escaped'",
'status' => "Status = '".(@$sesutats[gTxt($crit_escaped)])."'",
'author' => "AuthorID rlike '$crit_escaped'",
);
if (array_key_exists($search_method, $critsql))
{
$criteria = $critsql[$search_method];
$limit = 500;
}
else
{
$search_method = '';
$crit = '';
}
}
else
{
$search_method = '';
$crit = '';
}
$total = safe_count('textpattern', "$criteria");
if ($total < 1)
{
if ($criteria != 1)
{
echo n.list_search_form_products($crit, $search_method).
n.graf("No products found", ' style="text-align: center;"');
}
else
{
echo graf("No products found", ' style="text-align: center;"');
}
return;
}
$limit = max(@$article_list_pageby, 15);
list($page, $offset, $numPages) = pager($total, $limit, $page);
echo n.list_search_form_products($crit, $search_method);
$rs = safe_rows_start('*, unix_timestamp(Posted) as posted', 'textpattern',
"$criteria order by $sort_sql limit $offset, $limit"
);
if ($rs)
{
$total_comments = array();
// fetch true comment count, not the public comment count
// maybe we should have another row in the db?
$rs2 = safe_rows_start('parentid, count(*) as num', 'txp_discuss', "1 group by parentid order by parentid");
if ($rs2)
{
while ($a = nextRow($rs2))
{
$pid = $a['parentid'];
$num = $a['num'];
$total_comments[$pid] = $num;
}
}
echo n.n.'
'.
n.'Import Products';
//n.
$instructions = tag(tag('
Using FTP, upload your product images to /txp_site_root/images/_import/
Upload a correctly formatted CSV file using the form below. (CSV must be in UTF-8 character encoding with DOS or UNIX line breaks.)Sit back and watch the magic',"ol"), "div", ' id="instructions" style="display:none; width:380px; text-align:left; margin:0 auto;"');
echo tag(''.$instructions.upload_form("Browse for CSV:", '', 'product_import', 'product'), 'div', ' id="uploadCSV" style="display:none;"');
echo n.nav_form('list', $page, $numPages, $sort, $dir, $crit, $search_method).
n.pageby_form('list', $article_list_pageby);
}
}//---- end products_list()
function product_import(){
global $txp_user, $textile;
define("TEMP_IMPATH",'../images/_import/');
if(isset($_FILES["thefile"])){
$thefile = $_FILES["thefile"]["tmp_name"];
move_uploaded_file($thefile, "../files/import.csv");
chmod("../files/import.csv", 0666);
$data = parse_csv("../files/import.csv", true);
foreach($data as $row){
$title = implode(",", $row);
$title = explode(",", $title);
$title = $title[0];
extract($row);
if($row['STATUS'] == 'Live'){
$status = 4;
}else if($row['STATUS'] == 'Hidden'){
$status = 2;
}else{
$status = 3;
}
if(!empty($row['VENDOR'])){
include_once txpath.'/lib/classTextile.php';
$textile = new Textile();
$vendor = dumbDown($textile->TextileThis(trim(doSlash($row['VENDOR'])),1));
$vendor = preg_replace("/[^[:alnum:]\-_]/", "", str_replace(" ","-",$row['VENDOR']));
}
if(!empty($CATEGORY_1)){
$CATEGORY_1 = preg_replace("/[^[:alnum:]\-_]/", "", str_replace(" ","-",$CATEGORY_1));
}
if(!empty($CATEGORY_2)){
$CATEGORY_2 = preg_replace("/[^[:alnum:]\-_]/", "", str_replace(" ","-",$CATEGORY_2));
}
safe_insert(
"textpattern",
"Title = '$title',
Body = '$DESCRIPTION',
Status = $status,
Posted = now(),
LastMod = now(),
AuthorID = '$txp_user',
Section = 'store',
Category1 = '$CATEGORY_1',
Category2 = '$CATEGORY_2',
custom_1 = '$PRICE',
custom_2 = '$WEIGHT',
custom_3 = '$SKU',
custom_4 = '$ITEMS_IN_STOCK',
custom_5 = '$vendor',
uid = '".md5(uniqid(rand(),true))."',
feed_time = now()"
);
//echo mysql_error();
$ID = mysql_insert_id();
//echo $ID; print_r($product); die();
if(!empty($row['PRODUCT_IMAGE_1'])){
$img = TEMP_IMPATH.$row['PRODUCT_IMAGE_1'];
upload_image($img, '1', $ID);
}
if(!empty($row['PRODUCT_IMAGE_2'])){
$img = TEMP_IMPATH.$row['PRODUCT_IMAGE_2'];
upload_image($img, '2', $ID);
}
if(!empty($row['PRODUCT_IMAGE_3'])){
$img = TEMP_IMPATH.$row['PRODUCT_IMAGE_3'];
upload_image($img, '3', $ID);
}
if(!empty($row['PRODUCT_IMAGE_4'])){
$img = TEMP_IMPATH.$row['PRODUCT_IMAGE_4'];
upload_image($img, '4', $ID);
}
$customFields = '';
if(!empty($row['CUSTOM_FIELD_LABEL_1'])){
$field = array(
'label' => $row['CUSTOM_FIELD_LABEL_1'],
'value' => $row['CUSTOM_FIELD_VALUE_1']);
$customFields[] = $field;
}
if(!empty($row['CUSTOM_FIELD_LABEL_2'])){
$field = array(
'label' => $row['CUSTOM_FIELD_LABEL_2'],
'value' => $row['CUSTOM_FIELD_VALUE_2']);
$customFields[] = $field;
}
if(!empty($row['CUSTOM_FIELD_LABEL_3'])){
$field = array(
'label' => $row['CUSTOM_FIELD_LABEL_3'],
'value' => $row['CUSTOM_FIELD_VALUE_3']);
$customFields[] = $field;
}
if(!empty($row['CUSTOM_FIELD_LABEL_4'])){
$field = array(
'label' => $row['CUSTOM_FIELD_LABEL_4'],
'value' => $row['CUSTOM_FIELD_VALUE_4']);
$customFields[] = $field;
}
if(count($customFields) > 0){
save_custom_fields($customFields, $ID);
}
}
products_list('', '', "Products Imported");
}else{
products_list('','','Error: Couldn\'t Find Uploaded File!');
}
}
function product_edit($event, $step, $message='') {
global $vars, $txp_user, $comments_disabled_after, $txpcfg, $prefs, $general_settings;
extract($prefs);
define("IMPATH",$path_to_site.'/'.$img_dir.'/');
extract(gpsa(array('view','from_view','step')));
if(!empty($GLOBALS['ID'])) { // newly-saved article
$ID = $GLOBALS['ID'];
$step = 'edit';
} else {
$ID = gps('ID');
}
//GET ARTICLE DATA FOR 'EDIT' & 'SAVE' STEP
//==================================
if ($step == "edit" || $step=="save"
&& !empty($ID)) {
$pull = true; //-- it's an existing article - off we go to the db
$ID = assert_int($ID);
$rs = safe_row(
"*, unix_timestamp(Posted) as sPosted,
unix_timestamp(LastMod) as sLastMod",
"textpattern",
"ID=$ID"
);
extract($rs);
if ($AnnotateInvite!= $comments_default_invite) {
$AnnotateInvite = $AnnotateInvite;
} else {
$AnnotateInvite = $comments_default_invite;
}
$custom_fields = safe_rows("*", "product_custom_fields", "articleID = $ID ORDER BY id DESC");
$step = "save";
}else if(empty($step)){
$step = "create";
}
$textile = new Textile();
$textile_body = $use_textile;
$textile_excerpt = $use_textile;
$page_title = "Add a new product";
$message = "";
pagetop($page_title, $message);
if(count($custom_fields) > 0){
$startFieldNum = ($custom_fields[0]['id']+1);
}else{
$startFieldNum = 0;
}
echo "";
//JS INCLUDES
//==================================
//print_r($prefs);
echo '';
echo '';
//CSS FOR PRODUCT DISPLAY
//==================================
echo n.'';
doJS();
echo n.n.'';
}
function product_image_display($imageURL, $size){
$str = str_replace("medium", $size, $imageURL);
return hu."images/".$str;
}
function product_save(){
global $txp_user, $vars, $txpcfg, $prefs;
extract($prefs);
$incoming = psa($vars);
define("IMPATH",$path_to_site.'/'.$img_dir.'/');
$oldArticle = safe_row('Status, url_title, Title, Image, custom_6, custom_7, custom_8','textpattern','ID = '.(int)$incoming['ID']);
if (! ( ($oldArticle['Status'] >= 4 and has_privs('article.edit.published'))
or ($oldArticle['Status'] >= 4 and $incoming['AuthorID']==$txp_user and has_privs('article.edit.own.published'))
or ($oldArticle['Status'] < 4 and has_privs('article.edit'))
or ($oldArticle['Status'] < 4 and $incoming['AuthorID']==$txp_user and has_privs('article.edit.own'))))
{
// Not allowed, you silly rabbit, you shouldn't even be here.
// Show default editing screen.
product_edit();
return;
}
$wrapper = new TXP_Wrapper();
$incoming = $wrapper->textile_main_fields($incoming, $use_textile);
//dmp($incoming);
extract(doSlash($incoming));
extract(array_map('assert_int', psa(array('ID', 'Status', 'textile_body', 'textile_excerpt'))));
$Annotate = ( ps( 'Annotate')) ? assert_int( ps( 'Annotate')) : 0;
if (!has_privs('article.publish') && $Status>=4) $Status = 3;
if($reset_time) {
$whenposted = "Posted=now()";
} else {
$when = strtotime($year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$second)-tz_offset();
$when = "from_unixtime($when)";
$whenposted = "Posted=$when";
}
if (isset($new_category_name) && !empty($new_category_name)){
$Category1 = $new_category_name;
category_create($Category1,"Products");
}else if (isset($category)){
$Category1 = $category;
}
if (isset($new_vendor_name) && !empty($new_vendor_name)){
$Category2 = $new_vendor_name;
category_create($Category2,"Vendors");
}else if (isset($vendor)){
$Category2 = $vendor;
}
//Auto-Update custom-titles according to Title, as long as unpublished and NOT customized
if ( empty($url_title)
|| ( ($oldArticle['Status'] < 4)
&& ($oldArticle['url_title'] == $url_title )
&& ($oldArticle['url_title'] == stripSpace($oldArticle['Title'],1))
&& ($oldArticle['Title'] != $Title)
)
)
{
$url_title = stripSpace($Title_plain, 1);
}
if (!$Annotate) $Annotate = 0;
//IMAGE DELETE/UPDATE FUNCTIONALITY
//=======================================
if(strtolower($Image) == "delete"){
$delPath = IMPATH.$oldArticle['Image'];
unlink($delPath);
unlink(str_replace("medium", "small", $delPath));
unlink(str_replace("gif", "jpg", str_replace("medium", "large", $delPath)));
$Image = "";
}
if(strtolower($custom_6) == "delete"){
$delPath = IMPATH.$oldArticle['Image'];
unlink($delPath);
unlink(str_replace("medium", "small", $delPath));
unlink(str_replace("gif", "jpg", str_replace("medium", "large", $delPath)));
$custom_6 = "";
}
if(strtolower($custom_7) == "delete"){
$delPath = IMPATH.$oldArticle['Image'];
unlink($delPath);
unlink(str_replace("medium", "small", $delPath));
unlink(str_replace("gif", "jpg", str_replace("medium", "large", $delPath)));
$custom_7 = "";
}
if(strtolower($custom_8) == "delete"){
$delPath = IMPATH.$oldArticle['Image'];
unlink($delPath) or die("can't delete file: ".$delPath);
unlink(str_replace("medium", "small", $delPath));
unlink(str_replace("gif", "jpg", str_replace("medium", "large", $delPath)));
$custom_8 = "";
}
//UPDATE ARTICLE
//=======================================
safe_update("textpattern",
"Title = '$Title',
Body = '$Body',
Body_html = '$Body_html',
Excerpt = '$Excerpt',
Excerpt_html = '$Excerpt_html',
Keywords = '$Keywords',
Image = '$Image',
Status = $Status,
LastMod = now(),
LastModID = '$txp_user',
Section = '$Section',
Category1 = '$Category1',
Category2 = '$Category2',
Annotate = $Annotate,
textile_body = $textile_body,
textile_excerpt = $textile_excerpt,
override_form = '$override_form',
url_title = '$url_title',
AnnotateInvite = '$AnnotateInvite',
custom_1 = '$custom_1',
custom_2 = '$custom_2',
custom_3 = '$custom_3',
custom_4 = '$custom_4',
custom_5 = '$custom_5',
custom_6 = '$custom_6',
custom_7 = '$custom_7',
custom_8 = '$custom_8',
custom_9 = '$custom_9',
custom_10 = '$custom_10',
$whenposted",
"ID = $ID"
);
save_custom_fields($_REQUEST['custom_fields'], $ID);
//IMAGE UPLOAD
//=======================
if($_FILES['uploadFile']['error'] == "0"){
$file = $_FILES['uploadFile'];
}else if($_FILES['uploadFile1']['error'] == "0"){
$file = $_FILES['uploadFile1'];
}else if($_FILES['uploadFile2']['error'] == "0"){
$file = $_FILES['uploadFile2'];
}else if($_FILES['uploadFile3']['error'] == "0"){
$file = $_FILES['uploadFile3'];
}else if($_FILES['uploadFile4']['error'] == "0"){
$file = $_FILES['uploadFile4'];
}
if ($file["type"] == "image/gif" || $file["type"] == "image/jpeg" || $file["type"] == "image/png"){
// prepare the image for insertion
//we need to check what images have been uploaded already
$article_updated = safe_row("Image, custom_6, custom_7, custom_8", "textpattern", "ID = $ID");
extract($article_updated);
if(empty($Image)){
$image_num = "";
}else if(empty($custom_6)){
$image_num = "2";
}else if(empty($custom_7)){
$image_num = "3";
}else if(empty($custom_8)){
$image_num = "4";
}
echo "Uploading image: ".$file['name'];
$img = $file['tmp_name'];
upload_image($img, $image_num, $ID);
//echo "here"; die();
}
//END IMAGE UPLOAD
//=======================
if($Status >= 4) {
if ($oldArticle['Status'] < 4) {
if(!function_exists("do_pings")){
require_once(txpath.'/include/txp_article.php');
do_pings();
}
}
update_lastmod();
}
product_edit("","","Product Saved");
}
function product_post(){
global $txp_user, $vars, $txpcfg, $prefs;
extract($prefs);
define("IMPATH",$path_to_site.'/'.$img_dir.'/');
$incoming = psa($vars);
$import = false;
$message='';
$wrapper = new TXP_Wrapper();
$incoming = $wrapper->textile_main_fields($incoming, $use_textile);
extract(doSlash($incoming));
extract(array_map('assert_int', psa(array( 'Status', 'textile_body', 'textile_excerpt'))));
$Annotate = ( ps( 'Annotate')) ? assert_int( ps( 'Annotate')) : 0;
if($import){
$Status = $product['Status'];
}
$when = 'now()';
if ($Title or $Body or $Excerpt) {
if (!has_privs('article.publish') && $Status>=4) $Status = 3;
if (empty($url_title)) $url_title = stripSpace($Title_plain, 1);
if (!$Annotate) $Annotate = 0;
if (isset($new_category_name) && !empty($new_category_name)){
$Category1 = $new_category_name;
category_create($Category1,"Products");
}else if (isset($category)){
$Category1 = $category;
}
if (isset($new_vendor_name) && !empty($new_vendor_name)){
$Category2 = $new_vendor_name;
category_create($Category2,"Vendors");
}else if (isset($vendor)){
$Category2 = $vendor;
}
safe_insert(
"textpattern",
"Title = '$Title',
Body = '$Body',
Body_html = '$Body_html',
Excerpt = '$Excerpt',
Excerpt_html = '$Excerpt_html',
Image = '$Image',
Keywords = '$Keywords',
Status = $Status,
Posted = $when,
LastMod = now(),
AuthorID = '$txp_user',
Section = '$Section',
Category1 = '$Category1',
Category2 = '$Category2',
textile_body = $textile_body,
textile_excerpt = $textile_excerpt,
Annotate = $Annotate,
override_form = '$override_form',
url_title = '$url_title',
AnnotateInvite = '$AnnotateInvite',
custom_1 = '$custom_1',
custom_2 = '$custom_2',
custom_3 = '$custom_3',
custom_4 = '$custom_4',
custom_5 = '$custom_5',
custom_6 = '$custom_6',
custom_7 = '$custom_7',
custom_8 = '$custom_8',
custom_9 = '$custom_9',
custom_10 = '$custom_10',
uid = '".md5(uniqid(rand(),true))."',
feed_time = now()"
);
$GLOBALS['ID'] = mysql_insert_id();
$ID = $GLOBALS['ID'];
//print_r($_FILES);
//CUSTOM FIELDS
save_custom_fields($_REQUEST['custom_fields'], $ID);
//IMAGE UPLOAD
//=======================
if ($_FILES["uploadFile"]["type"] == "image/gif" || $_FILES["uploadFile"]["type"] == "image/jpeg" || $_FILES["uploadFile"]["type"] == "image/png"){
// prepare the image for insertion
$img = $_FILES['uploadFile']['tmp_name'];
upload_image($img, 1, $ID);
}
//END IMAGE UPLOAD
//=======================
if ($Status>=4) {
if(!function_exists("do_pings")){
require_once(txpath.'/include/txp_article.php');
do_pings();
}
update_lastmod();
}
product_edit("","","Product Saved");
} else product_edit();
}
function product_category_option_list($Category){
//$rows = product_category_list();
$options = "";
$rows = getTree('Products','article');
foreach($rows as $cat){
if($Category == $cat['name']){
$selected = " selected=\"selected\"";
}else{
$selected = "";
}
$options .= "".n;
}
return $options;
}
function product_category_list(){
$rows = safe_rows("*", "txp_category", "type = 'Article' and name != 'root' parent='Products'");
return $rows;
}
function product_multiedit_form($page, $sort, $dir, $crit, $search_method){
$methods = array(
'delete' => gTxt('delete')
);
return event_multiedit_form('product', $methods, $page, $sort, $dir, $crit, $search_method);
}
function product_multi_edit() {
global $txp_user;
$selected = ps('selected');
if (!$selected)
{
return products_list();
}
$method = ps('edit_method');
$changed = false;
$ids = array();
if ($method == 'delete')
{
if (!has_privs('article.delete'))
{
$allowed = array();
if (has_privs('article.delete.own'))
{
foreach ($selected as $id)
{
$id = assert_int($id);
$author = safe_field('AuthorID', 'textpattern', "ID = $id");
if ($author == $txp_user)
{
$allowed[] = $id;
}
}
}
$selected = $allowed;
}
foreach ($selected as $id)
{
$id = assert_int($id);
if (safe_delete('textpattern', "ID = $id"))
{
$ids[] = $id;
}
}
$changed = join(', ', $ids);
}
if ($changed)
{
return products_list(
messenger('Product', $changed, (($method == 'delete') ? 'deleted' : 'modified' ))
);
}
return products_list();
}
?>