Example #1
0
 function ProcessData()
 {
     global $_JAM;
     // Validate data; this fills $this->postData
     $this->ValidateData();
     // Display error and abort if there is invalid or missing data or a file upload error
     if ($this->invalidData || $this->missingData || $this->fileUploadError) {
         return false;
     }
     // Clear cache entirely; very brutal but will do for now
     $_JAM->cache->Clear();
     // Run custom action method if available
     if ($action = $_POST['action']) {
         $actionMethod = $action . 'Action';
         if (method_exists($this, $actionMethod)) {
             $this->{$actionMethod}();
             return true;
         } elseif ($this->parentModule->name == 'admin') {
             // We're in admin mode; look for action in admin module
             if (method_exists($this->parentModule, $actionMethod)) {
                 $this->parentModule->{$actionMethod}($this);
                 return true;
             }
         }
     }
     // Determine what we need to insert from what was submitted
     foreach ($this->schema as $name => $info) {
         // Omit fields which we can't edit
         if ($info['canEdit'] && !$_JAM->user->HasPrivilege($info['canEdit'])) {
             continue;
         }
         // Make sure data exists, and exclude 'multi' fields; we handle them later
         if (isset($this->postData[$name]) && $info['type'] != 'multi') {
             if ($info['localizable']) {
                 $localizedData[$name] = $this->postData[$name];
             } else {
                 $insertData[$name] = $this->postData[$name];
             }
         }
     }
     if (!$_GET['item']) {
         // FIXME: More kludge! Translations again.
         if (!$this->config['useCustomTable']) {
             // This is a standard table with special fields
             // If user is logged in, insert user ID
             if ($_JAM->user->id) {
                 $insertData['user'] = $_JAM->user->id;
             }
         }
         if (!$this->config['keepVersions']) {
             // Standard table; simple update
             if ($_POST['master']) {
                 // Update mode
                 $where = 'id = ' . $_POST['master'];
                 if (!$this->UpdateItems($insertData, $where)) {
                     // Update failed
                     trigger_error("Couldn't update module", E_USER_ERROR);
                     return false;
                 }
                 $insertID = $_POST['master'];
             } else {
                 // Post mode
                 if (!$this->config['useCustomTable']) {
                     $insertData['created'] = $_JAM->databaseTime;
                 }
                 if (!Database::Insert($this->name, $insertData)) {
                     trigger_error("Couldn't insert into module " . $this->name, E_USER_ERROR);
                     return false;
                 }
                 // Keep ID of inserted item for path
                 $insertID = Database::GetLastInsertID();
             }
         } else {
             // Special update for tables with multiple versions support
             // Set item as current
             $insertData['current'] = true;
             // If we already have a creation date and one wasn't specified, use that
             if (!$insertData['created'] && $this->item['created']) {
                 $insertData['created'] = $this->item['created'];
             }
             if (!Database::Insert($this->name, $insertData)) {
                 trigger_error("Couldn't insert into module " . $this->name, E_USER_ERROR);
             } else {
                 // Keep ID of inserted item for path
                 $insertID = Database::GetLastInsertID();
                 // $this->postData now represents actual data
                 $this->LoadData($this->postData);
                 // Disable all other items with the same master
                 if ($insertData['master']) {
                     $updateParams['current'] = false;
                     $whereArray = array(array('master = ' . $insertData['master'], 'id = ' . $insertData['master']), 'id != ' . $insertID);
                     $where = Database::GetWhereString($whereArray);
                     if (!Database::Update($this->name, $updateParams, $where)) {
                         trigger_error("Couldn't update module " . $this->name, E_USER_ERROR);
                         return false;
                     }
                 }
             }
         }
     } else {
         // FIXME: Kuldgy. Added to make translations work.
         $insertID = $_GET['item'];
     }
     // Insert localized data
     if ($localizedData) {
         $tableName = $this->name . '_localized';
         $localizedData['item'] = $insertID;
         $localizedData['language'] = $this->postData['language'];
         $where = array('item = ' . $insertID, "language = '" . $localizedData['language'] . "'");
         if (Database::Update($tableName, $localizedData, $where)) {
             // Insert if no rows were affected
             if (Database::GetModifiedRows() == 0) {
                 if (Database::Insert($tableName, $localizedData)) {
                     $success = true;
                 } else {
                     trigger_error("Couldn't insert localized data for module " . $this->name, E_USER_ERROR);
                 }
             } else {
                 $success = true;
             }
             // Put data into module object to reflect changes in the database
             if ($success) {
                 $this->LoadData($localizedData);
             }
         } else {
             trigger_error("Couldn't update localized data for module " . $this->name, E_USER_ERROR);
             return false;
         }
     }
     if ($insertID) {
         // Update path
         $this->UpdatePath($insertID);
         // Get ID for this item
         $id = $_POST['master'] ? $_POST['master'] : $insertID;
         // Delete previous many-to-many relationships
         $where = array('frommodule = ' . $this->moduleID, 'fromid = ' . $insertID);
         if (!Database::DeleteFrom('_relationships', $where)) {
             trigger_error("Couldn't delete previous many-to-many relationships for module " . $this->name, E_USER_ERROR);
         }
         foreach ($this->schema as $name => $info) {
             switch ($info['type']) {
                 case 'multi':
                     // Insert many-to-many relationships
                     foreach ($this->postData[$name] as $targetID) {
                         // Insert each item into _relationships table
                         $targetModuleName = $info['relatedModule'];
                         $targetModuleID = array_search($targetModuleName, $_JAM->installedModules);
                         $params = array('frommodule' => $this->moduleID, 'fromid' => $insertID, 'tomodule' => $targetModuleID, 'toid' => $targetID);
                         if (!Database::Insert('_relationships', $params)) {
                             trigger_error("Couldn't insert many-to-many relationship for module " . $this->name, E_USER_ERROR);
                         }
                     }
                     break;
             }
         }
     }
     if (method_exists($this, 'PostProcessData')) {
         $this->PostProcessData($insertID);
     }
     // Check whether we need to redirect to a specific anchor
     $anchor = $this->config['redirectToAnchor'][$this->parentModule->name];
     // Reload page
     if ($_JAM->rootModuleName == 'admin' || !$this->config['postSubmitRedirect']) {
         HTTP::ReloadCurrentURL('?m=updated' . ($anchor ? '#' . $anchor : ''));
     } else {
         HTTP::RedirectLocal($this->config['postSubmitRedirect']);
     }
 }