]> git.mjollnir.org Git - s9y.git/commitdiff
Added helper API class by Falk Döring
authorgarvinhicking <garvinhicking>
Thu, 11 May 2006 08:14:39 +0000 (08:14 +0000)
committergarvinhicking <garvinhicking>
Thu, 11 May 2006 08:14:39 +0000 (08:14 +0000)
docs/NEWS
include/plugin_api_extension.inc.php [new file with mode: 0644]

index d98543667adf159aa866f11e14e97f49b97d9941..6239db0e889dab1ecbb9265428a707397116a6cd 100644 (file)
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -3,6 +3,10 @@
 Version 1.1-alpha5()
 ------------------------------------------------------------------------
 
+   * Added "plugin_api_extension" class, which contains some helper
+     methods for future use, like re-ordering DB items or checking
+     valid emails. (Falk Döring)
+
    * Fix multiple occurences of the string "--" in the entry title to
      not mess up HTML display because of weird Firefox interpretation
      (Bug #1474290) (garvinhicking)
diff --git a/include/plugin_api_extension.inc.php b/include/plugin_api_extension.inc.php
new file mode 100644 (file)
index 0000000..f728277
--- /dev/null
@@ -0,0 +1,127 @@
+<?php # $Id: plugin_api.inc.php 1168 2006-04-29 13:06:11Z garvinhicking $
+# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
+# All rights reserved.  See LICENSE file for licensing details
+
+if (IN_serendipity !== true) {
+    die ('Don\'t hack!');
+}
+
+if (defined('S9Y_FRAMEWORK_PLUGIN_API_EXTENSION')) {
+    return;
+}
+@define('S9Y_FRAMEWORK_PLUGIN_API_EXTENSION', true);
+
+class serendipity_plugin_api_extension extends serendipity_plugin_api
+{
+    /**
+     * Prepare a given one dimension array for reordering
+     *
+     * @access public
+     * @author Falk Doering
+     * @param  array  the array
+     * @param  string the key of the parent id
+     * @return array  the final array with two new keys: 'up' and 'down'
+     */
+
+    function prepareReorder($array, $parent_id = 'parent_id')
+    {
+        if (is_array($array)) {
+            for ($i = 0, $ii = count($array); $i < $ii; $i++) {
+                $array[$i]['down'] = (isset($array[$i]['down']) ? $array[$i]['down'] : false);
+                $array[$i]['up']   = (isset($array[$i]['up']) ? $array[$i]['up'] : false);
+                for ($j = ($i + 1); $j < $ii; $j++) {
+                    if ($array[$j][$parent_id] == $array[$i][$parent_id]) {
+                        $array[$i]['down'] = true;
+                        $array[$j]['up'] = true;
+                    }
+                }
+            }
+            return $array;
+        }
+        return $array;
+    }
+
+    /**
+     * Update table for re-ordering
+     *
+     * @access public
+     * @author Falk Doering
+     * @param  string  Name of the table
+     * @param  string  The direction ('up' or 'down')
+     * @param  array   The update array
+     * @param  array   The array containing the where clause
+     * @return boolean
+     */
+
+    function doReorder($table, $moveto, $update_array, $where_array)
+    {
+        global $serendipity;
+
+        if (is_array($update_array) && is_array($where_array)) {
+            $where = '';
+            foreach ($where_array as $key => $value) {
+                if (strlen($where)) {
+                    $where .= ' AND ';
+                }
+                $where .= $key . ' = ' . $value;
+            }
+            $q = 'SELECT '.implode(", ", array_keys($update_array)).'
+                    FROM '. $serendipity['dbPrefix'] . $table .'
+                   WHERE '.$where;
+            $old = serendipity_db_query($q, true, 'assoc');
+
+            if (is_array($old)) {
+                $where = array();
+                $update = array();
+                switch ($moveto) {
+                    case 'up':
+                        foreach ($update_array as $key => $value) {
+                            if ($value) {
+                                $where[$key] = ($old[$key] - 1);
+                                $update[$key] = $old[$key];
+                                $update_1[$key] = ($old[$key] - 1);
+                            } else {
+                                $where[$key] = $old[$key];
+                            }
+                        }
+                        break;
+                    case 'down':
+                        foreach ($update_array as $key => $value) {
+                            if ($value) {
+                                $where[$key] = ($old[$key] + 1);
+                                $update[$key] = $old[$key];
+                                $update_1[$key] = ($old[$key] + 1);
+                            } else {
+                                $where[$key] = $old[$key];
+                            }
+                        }
+                        break;
+                    default:
+                        return false;
+                }
+                serendipity_db_update($table, $where, $update);
+                serendipity_db_update($table, $where_array, $update_1);
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     *
+     * Check if a string is a valid email
+     *
+     * @access public
+     * @author Falk Doering
+     * @param  string   The email string
+     * @return bool     is valid email true, else false
+     *
+     */
+
+    function isEmail($email)
+    {
+        $preg = '^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@(([_a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}|museum)|localhost)$';
+        return (preg_match($preg, $email) != 0);
+    }
+
+}