Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / modules / admin / what-list_cats.php
index 048e8605d381f1f620cbfd03c5b398b70a85f324..919e203a054c10c25657ff9fad3febd72a3374ac 100644 (file)
@@ -1,25 +1,24 @@
 <?php
 /************************************************************************
- * Mailer v0.2.1-FINAL                                Start: 01/27/2004 *
- * ================                             Last change: 07/13/2004 *
+ * Mailer v0.2.1-FINAL                                Start: 10/10/2003 *
+ * ===================                          Last change: 07/04/2004 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : what-list_cats.php                               *
  * -------------------------------------------------------------------- *
- * Short description : List selected categories from users              *
+ * Short description : Add new categories and edit / delete existing    *
  * -------------------------------------------------------------------- *
- * Kurzbeschreibung  : Ausgewaehlte Kategorien eines Mitgliedes         *
- *                     auflisten                                        *
+ * Kurzbeschreibung  : Neue Kategorien hinzufuegen und bestehende       *
+ *                     editieren / loeschen                             *
  * -------------------------------------------------------------------- *
  * $Revision::                                                        $ *
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
@@ -43,64 +42,148 @@ if ((!defined('__SECURITY')) || (!isAdmin())) {
 } // END - if
 
 // Add description as navigation point
-addMenuDescription('admin', __FILE__);
-
-if (isGetRequestElementSet('userid')) {
-       // Check if the user already exists
-       if (fetchUserData(getRequestElement('userid'))) {
-               // Loads surname, family's name and the email address
-               $content = getUserDataArray();
-
-               // Prepare constants
-               // @TODO No longer needed? define('__CATS_BASE'   , "<a href=\"{?URL?}/modules.php?module=admin&amp;userid=");
-               $content['userid']       = bigintval(getRequestElement('userid'));
-               $content['member_links'] = generateMemberAdminActionLinks($content['userid']);
-               $content['email']        = "<a href=\"".generateEmailLink($content['email'], 'user_data')."\">".$content['surname']." ".$content['family']."</a>";
-
-               // Ok, list categories of this user
-               $result_cats = SQL_QUERY("SELECT `id`, `cat` FROM `{?_MYSQL_PREFIX?}_cats` ORDER BY `sort` ASC", __FILE__, __LINE__);
-
-               if (SQL_NUMROWS($result_cats) > 0) {
-                       // List categories
-                       $cnt = 1; $OUT = ''; $SW = 2;
-                       while ($content = merge_array($content, SQL_FETCHARRAY($result_cats))) {
-                               // Check user's selection
-                               $result_user = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `userid`=%s AND `cat_id`=%s LIMIT 1",
-                                       array(bigintval(getRequestElement('userid')), bigintval($content['id'])), __FILE__, __LINE__);
-
-                               // Set selection
-                               $selection = "<font color=\"#ee0000\">{--NO--}</div>";
-                               if (SQL_NUMROWS($result_user) == 1) $selection = "<font color=\"#00ee00\">{--YES--}</div>";
-
-                               // Add more elements
-                               $content['sw']  = $SW;
-                               $content['cnt'] = $cnt;
-                               $content['sel'] = $selection;
-
-                               // Load row template and switch colors + count up
-                               $OUT .= loadTemplate('admin_list_cats_row', true, $content);
-                               $SW = 3 - $SW; $cnt++;
-                       } // END - while
-
-                       // Free memory
-                       SQL_FREERESULT($result);
-
-                       // Remember all rows in the array
-                       $content['rows'] = $OUT;
-
-                       // Load main template
-                       loadTemplate('admin_list_cats', false, $content);
+addYouAreHereLink('admin', __FILE__);
+
+// Init variable to avoid a notice
+$CATS = '';
+
+if (isFormSent('add')) {
+       // Default message
+       $content = '<span class="bad">{--CATEGORY_ALREADY_EXISTS--}</span>';
+
+       // Add a new category
+       $result = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_cats` WHERE `cat`='%s' LIMIT 1",
+               array(postRequestElement('catname')), __FILE__, __LINE__);
+
+       if (ifSqlHasZeroNumRows($result)) {
+               // Category does not exists, we simply add it...
+               sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_cats` (`cat`, `visible`, `sort`) VALUES ('%s','%s','%s')",
+                       array(
+                               postRequestElement('catname'),
+                               postRequestElement('visible'),
+                               bigintval(postRequestElement('parent') + 1)
+                       ), __FILE__, __LINE__);
+
+               // Category has been added
+               $content = '{--CATEGORY_ADDED--}';
+       } // END - if
+
+       // Free memory
+       sqlFreeResult($result);
+
+       // Display message
+       displayMessage($content);
+} elseif ((isFormSent()) && (ifPostContainsSelections('id'))) {
+       // Change or delete categories...
+       $TEXT = '';
+       foreach (postRequestElement('id') as $id => $cat) {
+               // Secure id
+               $id = bigintval($id);
+
+               // Is the entry set?
+               if (!empty($cat)) {
+                       switch (getRequestElement('do')) {
+                               case 'edit': // Change categories
+                                       sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_cats` SET `cat`='%s',`visible`='%s',`sort`=%s WHERE `id`=%s LIMIT 1",
+                                               array(
+                                                       $cat,
+                                                       postRequestElement('visible', $id),
+                                                       postRequestElement('sort', $id),
+                                                       $id
+                                               ), __FILE__, __LINE__);
+                                       $TEXT = '{--ADMIN_CATEGORIES_SAVED--}';
+                                       break;
+
+                               case 'delete': // Delete categories
+                                       sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_cats` WHERE `id`=%s LIMIT 1",
+                                               array($id), __FILE__, __LINE__);
+                                       sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_cats` WHERE `cat_id`=%s",
+                                               array($id), __FILE__, __LINE__);
+                                       $TEXT = '{--ADMIN_CATEGORIES_DELETED--}';
+                                       break;
+                       } // END - switch
                } else {
-                       // No categories selected! :-(
-                       loadTemplate('admin_list_cats_404', false, $content);
+                       // Entry not saved
+                       $TEXT .= '{%message,ADMIN_CATEGORY_NOT_SAVED=' . $id . '%}';
                }
-       } else {
-               // User not found
-               loadTemplate('admin_settings_saved', false, sprintf(getMessage('ADMIN_MEMBER_404'), $content['userid']));
-       }
+       } // END - foreach
+
+       if (isset($TEXT)) {
+               // Display message
+               displayMessage($TEXT);
+       } // END - if
+} elseif ((isFormSent('delete')) && (ifPostContainsSelections())) {
+       // Delete categories
+       $OUT = '';
+       foreach (postRequestElement('sel') as $id => $value) {
+               // Load row template and switch colors
+               $OUT .= loadTemplate('admin_delete_cats_row', TRUE, $id);
+       } // END - foreach
+
+       // Load main template
+       loadTemplate('admin_delete_cats', FALSE, $OUT);
+} elseif ((isFormSent('edit')) && (ifPostContainsSelections())) {
+       // Edit categories
+       $OUT = '';
+       foreach (postRequestElement('sel') as $id => $value) {
+               // Load data from the category
+               $result = sqlQueryEscaped("SELECT `id`, `visible`, `sort` FROM `{?_MYSQL_PREFIX?}_cats` WHERE `id`=%s LIMIT 1",
+                       array(bigintval($id)), __FILE__, __LINE__);
+               $content = sqlFetchArray($result);
+
+               // Free result
+               sqlFreeResult($result);
+
+               // Prepare data for the row template
+               $content['visible_selection'] = addSelectionBox('yn', $content['visible'], 'visible', $content['id']);
+
+               // Load row template and switch colors
+               $OUT .= loadTemplate('admin_edit_cats_row', TRUE, $content);
+       } // END - foreach
+
+       // Load main template
+       loadTemplate('admin_edit_cats', FALSE, $OUT);
 } else {
-       // Output selection form with all confirmed user accounts listed
-       addMemberSelectionBox();
+       // Init variable here
+       $CATS = '';
+
+       // Load all categories
+       $result = sqlQuery("SELECT
+       `id`,
+       `cat`,
+       `visible`,
+       `sort`
+FROM
+       `{?_MYSQL_PREFIX?}_cats`
+ORDER BY
+       `sort` ASC", __FILE__, __LINE__);
+
+       if (!ifSqlHasZeroNumRows($result)) {
+               // Init variables
+               $OUT = '';
+
+               // List already existing categories for editing
+               while ($content = sqlFetchArray($result)) {
+                       // Put cat descriptions into variable for the selection box
+                       if (strlen($content['cat']) > 40) $content['cat'] = substr($content['cat'], 0, 37) . '...';
+                       $CATS .= '<option value="' . $content['sort'] . '">' . $content['cat'] . '</option>';
+
+                       // Load row template and switch color
+                       $OUT .= loadTemplate('admin_list_cats_row', TRUE, $content);
+               } // END - while
+
+               // Free memory
+               sqlFreeResult($result);
+
+               // Load main template
+               loadTemplate('admin_list_cats', FALSE, $OUT);
+       } // END - if
+
+       // Remember in array
+       $content['cats'] = $CATS;
+
+       // Form to add a new category
+       loadTemplate('admin_add_cat', FALSE, $content);
 }
 
 // [EOF]