]> git.mxchange.org Git - mailer.git/blobdiff - inc/extensions-functions.php
Rewrote 'we' word a little, rewrote mail order to use SQL_INSERTID() instead of anoth...
[mailer.git] / inc / extensions-functions.php
index fb7139553670570c1e595c1e5be71bc3bd9d545c..93e1fdf84a88628df20810492439f7cb7656fab7 100644 (file)
@@ -41,7 +41,7 @@ if (!defined('__SECURITY')) {
 } // END - if
 
 // Load the extension and maybe found language and function files.
-function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0.0', $isDryRun = false) {
+function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0.0', $isDryRun = false, $previousExtension = NULL) {
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',ext_mode=' . $ext_mode . ',ext_ver=' . $ext_ver . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
        // Loading an extension in same mode, but not test/update, twice is not
        // good, so is the extension $ext_name already loaded in mode $ext_mode?
@@ -53,6 +53,9 @@ function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0.0', $isDryRun = fa
        // Make sure this situation can only happen once
        $GLOBALS['loaded_extension'][$ext_name][$ext_mode] = true;
 
+       // Remember previous extension here
+       $GLOBALS['previous_extension'][$ext_name] = $previousExtension;
+
        // Set extension mode
        setExtensionMode($ext_mode);
 
@@ -122,7 +125,7 @@ function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0.0', $isDryRun = fa
                loadLanguageFile($ext_name);
        } // END - if
 
-       // Do we have cache?
+       // Is there cache?
        if (isExtensionFunctionFileReadable($ext_name)) {
                // Not yet loaded?
                if ((($GLOBALS['cache_array']['extension']['ext_func'][$ext_name] == 'Y') || (!isset($GLOBALS['cache_array']['extension']['ext_func'][$ext_name]))) && (!isExtensionLibraryLoaded($ext_name))) {
@@ -177,7 +180,7 @@ function loadExtension ($ext_name, $ext_mode, $ext_ver = '0.0.0', $isDryRun = fa
 }
 
 // Registers an extension and possible update dependencies
-function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdates = false) {
+function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdates = false, $previousExtension = NULL) {
        // Set current extension name
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId=' . intval($taskId) . ',isDryRun=' . intval($isDryRun) . ',ignoreUpdates=' . intval($ignoreUpdates) . ' - ENTERED!');
        setCurrentExtensionName($ext_name);
@@ -225,7 +228,7 @@ function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdate
                        // Is it still zero?
                        if ((is_null($taskId)) || ($taskId == '0')) {
                                // Then request a bug report
-                               reportBug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId(%s)",
+                               reportBug(__FUNCTION__, __LINE__, sprintf("%s: task_id is still zero after determineExtensionTaskId/createNewExtensionTask(%s)",
                                        __FUNCTION__,
                                        getCurrentExtensionName()
                                ));
@@ -247,7 +250,7 @@ function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdate
 
        // Does this extension exists?
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ' - CALLING loadExtension() ...');
-       if (loadExtension(getCurrentExtensionName(), 'register', '0.0.0', isExtensionDryRun())) {
+       if (loadExtension(getCurrentExtensionName(), 'register', '0.0.0', isExtensionDryRun(), $previousExtension)) {
                // Set current extension name again
                setCurrentExtensionName($ext_name);
 
@@ -257,7 +260,7 @@ function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdate
                foreach ($history as $ext_ver) {
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ', ext_ver=' . $ext_ver);
                        // Load extension in update mode
-                       loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun());
+                       loadExtension(getCurrentExtensionName(), 'update', $ext_ver, isExtensionDryRun(), $previousExtension);
 
                        // Add update notes to our output
                        addExtensionNotes($ext_ver);
@@ -273,7 +276,7 @@ function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdate
 
                        // Check if extension is not installed and not already in registration procedure and if loading it wents finally fine...
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',isExtensionRegistrationRunning(' . $ext_update . ')=' . intval(isExtensionRegistrationRunning($ext_update)));
-                       if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun()))) {
+                       if ((!isExtensionInstalled($ext_update)) && (!isExtensionRegistrationRunning($ext_update)) && (loadExtension($ext_update, 'test', '', isExtensionDryRun(), $previousExtension))) {
                                // Set current extension name again
                                setCurrentExtensionName($ext_name);
 
@@ -295,7 +298,7 @@ function registerExtension ($ext_name, $taskId, $isDryRun = false, $ignoreUpdate
                                        if (($updateTaskId > 0) || (isInstallationPhase())) {
                                                // Try to register the extension
                                                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currName=' . getCurrentExtensionName() . ',ext_update=' . $ext_update . ',updateTaskId=' . $updateTaskId . ',isExtensionDryRun()=' . intval(isExtensionDryRun()));
-                                               $processResult = registerExtension($ext_update, $updateTaskId, isExtensionDryRun(), true);
+                                               $processResult = registerExtension($ext_update, $updateTaskId, isExtensionDryRun(), true, $ext_name);
 
                                                // Reset extension name
                                                setCurrentExtensionName($ext_name);
@@ -471,7 +474,7 @@ function doExtensionSqls ($ext_id, $load_mode) {
        // Debug message
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SQLs::count=' . countSqls());
 
-       // Do we have entries?
+       // Are there entries?
        if (isSqlsValid()) {
                // Run SQL commands...
                runFilterChain('run_sqls');
@@ -526,7 +529,7 @@ function isExtensionInstalled ($ext_name) {
                // Look in database
                $ext_id = getExtensionId($ext_name);
 
-               // Do we have a record?
+               // Is there a record?
                $isInstalled = ($ext_id > 0);
 
                // Log debug message
@@ -790,9 +793,9 @@ function addExtensionVerboseSqlTable ($title = '{--ADMIN_SQLS_EXECUTED_ON_REMOVA
        // Init variables
        $OUT = '';
 
-       // Do we have queries?
+       // Is there queries?
        if (isVerboseSqlEnabled()) {
-               // Do we have entries?
+               // Are there entries?
                if (countExtensionSqls() > 0) {
                        // Init counter
                        $idx = 0;
@@ -880,7 +883,7 @@ function getExtensionId ($ext_name) {
        // Init id number
        $data['ext_id'] = '0';
 
-       // Do we have cache?
+       // Is there cache?
        if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) {
                // Load from cache
                $data['ext_id'] = $GLOBALS['cache_array']['extension']['ext_id'][$ext_name];
@@ -911,7 +914,7 @@ function getExtensionId ($ext_name) {
 
 // Determines whether the given extension name is valid
 function isExtensionNameValid ($ext_name) {
-       // Do we have cache?
+       // Is there cache?
        if (!isset($GLOBALS['ext_name_valid'][$ext_name])) {
                // Generate include file name
                $INC = sprintf("inc/extensions/ext-%s.php", $ext_name);
@@ -1039,7 +1042,8 @@ function createNewExtensionTask ($ext_name) {
        $taskId = determineTaskIdBySubject($subject);
 
        // Not installed and do we have created a task for the admin?
-       if (($taskId == '0') && (!isExtensionInstalled($ext_name))) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId[' . gettype($taskId) . ']=' . $taskId);
+       if (((is_null($taskId)) || ($taskId == '0')) && (!isExtensionInstalled($ext_name))) {
                // Set default message if ext-foo is missing
                $message = '{%message,ADMIN_EXTENSION_TEXT_FILE_MISSING=' . $ext_name . '%}';
 
@@ -1076,7 +1080,8 @@ function createExtensionDeactivationTask ($ext_name) {
        $taskId = determineTaskIdBySubject($subject);
 
        // Not installed and do we have created a task for the admin?
-       if (($taskId == '0') && (isExtensionInstalled($ext_name))) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',taskId[' . gettype($taskId) . ']=' . $taskId);
+       if (((is_null($taskId)) || ($taskId == '0')) && (isExtensionInstalled($ext_name))) {
                // Task not created so add it
                $taskId = createNewTask($subject, SQL_ESCAPE(loadTemplate('task_EXTENSION_deactivated', true, $ext_name)), 'EXTENSION_DEACTIVATION');
        } // END - if
@@ -1557,6 +1562,16 @@ function addExtensionSql ($sql) {
                $GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()] = array();
        } // END - if
 
+       // Is the SQL statement empty?
+       if (empty($sql)) {
+               /*
+                * Abort here as this may happen if getExtensionMode() is 'activate' or
+                * 'deactivate'. This means that for 'mode' are no SQL statements
+                * specified.
+                */
+               return;
+       } // END - if
+
        // Add it
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . getCurrentExtensionName() . ',ext_version=' . getCurrentExtensionVersion() . ',sql=' . $sql);
        array_push($GLOBALS['ext_sqls'][getCurrentExtensionName()][getCurrentExtensionVersion()], $sql);
@@ -1640,7 +1655,7 @@ function FILTER_INIT_EXTENSIONS () {
        // Init notification pool
        initIncludePool('notify');
 
-       // Do we have some entries?
+       // Are there some entries?
        if (isset($GLOBALS['cache_array']['extension']['ext_name'])) {
                // Load all found extensions if found
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'CACHE - START!');
@@ -1731,7 +1746,7 @@ function removeExtensionFromArray () {
 
 // "Getter" for 'extension has a CSS file' (with same name, of course)
 function getExtensionHasCss () {
-       // Do we have cache?
+       // Is there cache?
        if (!isset($GLOBALS[__FUNCTION__][getCurrentExtensionName()][getCurrentTheme()])) {
                // Construct FQFN for check
                $FQFN = sprintf("%stheme/%s/css/%s.css",
@@ -1750,7 +1765,7 @@ function getExtensionHasCss () {
 
 // Checks whether the given extension has a language file
 function ifExtensionHasLanguageFile ($ext_name) {
-       // Do we have cache?
+       // Is there cache?
        if (isset($GLOBALS['cache_array']['extension']['ext_lang'][$ext_name])) {
                // Count cache hits
                incrementStatsEntry('cache_hits');
@@ -1853,6 +1868,19 @@ function addDropTableSql ($tableName) {
        } // END - if
 }
 
+// Adds a RENAME TABLE stament if 'from' table exist and 'to' table not
+function addRenameTableSql ($fromTable, $toTable) {
+       // Is renaming required?
+       if ((ifSqlTableExists($fromTable)) && (!ifSqlTableExists($toTable))) {
+               // Add it
+               addExtensionSql('RENAME TABLE `{?_MYSQL_PREFIX?}_' . $fromTable . '` TO `{?_MYSQL_PREFIX?}_' . $toTable . '`');
+
+               // Mark both tables
+               $GLOBALS['ifSqlTableExists'][$fromTable] = false;
+               $GLOBALS['ifSqlTableExists'][$toTable]   = true;
+       } // END - if
+}
+
 // Adds an admin menu to the SQL queue of the menu entry is not found
 function addAdminMenuSql ($action, $what, $title, $descr, $sort) {
        // Now check if this menu is there
@@ -2007,6 +2035,62 @@ function addConfigChangeSql ($oldColumnName, $newColumnName, $columnSql) {
        addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_config` CHANGE `' . $oldColumnName . '` `' . $newColumnName . '` ' . $columnSql);
 }
 
+/**
+ * Checks if given subject is found and if not, adds an SQL query to the
+ * extension registration queue.
+ */
+function registerExtensionPointsData ($subject, $columnName, $lockedMode, $paymentMethod) {
+       // Default is old extension version
+       $add = '';
+
+       // Is the extension equal or newer 0.8.9?
+       if (((isInstallationPhase()) && ((getExtensionMode() == 'register') || (getExtensionMode() == 'update'))) || (isExtensionInstalledAndNewer('sql_patches', '0.8.9'))) {
+               // Then add provider
+               $add = " AND `account_provider`='EXTENSION'";
+       } // END - if
+
+       // Is the 'subject' there?
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ifSqlTableExists(points_data)=' . ifSqlTableExists('points_data') . ',getExtensionMode()=' . getExtensionMode() . ',add=' . $add);
+       if (((!ifSqlTableExists('points_data')) && ((getExtensionMode() == 'register') || (getExtensionMode() == 'update'))) || (countSumTotalData($subject, 'points_data', 'id', 'subject', true, $add) == 0)) {
+               // Not found so:
+               if ((isset($GLOBALS['previous_extension'][getCurrentExtensionName()])) && (!ifSqlTableExists('points_data'))) {
+                       $dummy = $GLOBALS['previous_extension'][getCurrentExtensionName()];
+                       reportBug(__FUNCTION__, __LINE__, 'previous_extension[' . gettype($dummy) . ']=' . $dummy . ',getCurrentExtensionName()=' . getCurrentExtensionName() . ' - Under development, please report this!');
+               } // END - if
+
+               // ... add an SQL query
+               addExtensionSql(sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`,`column_name`,`locked_mode`,`payment_method`) VALUES ('%s','%s','%s','%s')",
+                       $subject,
+                       $columnName,
+                       $lockedMode,
+                       $paymentMethod
+               ));
+       } // END - if
+}
+
+/**
+ * Checks if given subject is found and if so, adds an SQL query to the
+ * extension unregistration queue.
+ */
+function unregisterExtensionPointsData ($subject) {
+       // Default is old extension version
+       $add = '';
+
+       // Is the extension equal or newer 0.8.9?
+       if (isExtensionInstalledAndNewer('sql_patches', '0.8.9')) {
+               // Then add provider
+               $add = " AND `account_provider`='EXTENSION'";
+       } // END - if
+
+       // Is the 'subject' there?
+       if (countSumTotalData($subject, 'points_data', 'id', 'subject', true, $add) == 1) {
+               // Found one or more, so add an SQL query
+               addExtensionSql(sprintf("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s'" . $add . " LIMIT 1",
+                       $subject
+               ));
+       } // END - if
+}
+
 // Enables/disables productive mode for current extension (used only while
 // registration).
 // @TODO This should be rewrittten to allow, more development states, e.g. 'planing','alpha','beta','beta2','stable'
@@ -2028,7 +2112,7 @@ function isExtensionProductive ($ext_name = '') {
        } // END - if
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
 
-       // Do we have cache?
+       // Is there cache?
        if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
                // Load extension only if not yet loaded
                if (!isset($GLOBALS['ext_productive'][$ext_name])) {