Takes a set of form data ($Form->_PostValues), validates them, and
inserts or updates them to the datatabase.
public save ( array $FormPostValues, array $Settings = false ) : unknown | ||
$FormPostValues | array | An associative array of $Field => $Value pairs that represent data posted from the form in the $_POST or $_GET collection. |
$Settings | array | If a custom model needs special settings in order to perform a save, they would be passed in using this variable as an associative array. |
Результат | unknown |
/** * Create and display a thumbnail of an uploaded file. */ public function utilityController_mediaThumbnail_create($sender, $media_id) { // When it makes it into core, it will be available in // functions.general.php require 'generate_thumbnail.php'; $model = new Gdn_Model('Media'); $media = $model->getID($media_id, DATASET_TYPE_ARRAY); if (!$media) { throw notFoundException('File'); } // Get actual path to the file. $local_path = Gdn_Upload::copyLocal($media['Path']); if (!file_exists($local_path)) { throw notFoundException('File'); } $file_extension = pathinfo($local_path, PATHINFO_EXTENSION); // Generate new path for thumbnail $thumb_path = $this->getBaseUploadDestinationDir() . '/' . 'thumb'; // Grab full path with filename, and validate it. $thumb_destination_path = $this->getAbsoluteDestinationFilePath($local_path, $file_extension, $thumb_path); // Create thumbnail, and grab debug data from whole process. $thumb_payload = generate_thumbnail($local_path, $thumb_destination_path, array('height' => c('Plugins.FileUpload.ThumbnailHeight', 128))); if ($thumb_payload['success'] === true) { // Thumbnail dimensions $thumb_height = round($thumb_payload['result_height']); $thumb_width = round($thumb_payload['result_width']); // Move the thumbnail to its proper location. Calling SaveAs with // cloudfiles enabled will trigger the move to cloudfiles, so use // same path for each arg in SaveAs. The file will be removed from the local filesystem. $parsed = Gdn_Upload::parse($thumb_destination_path); $target = $thumb_destination_path; // $parsed['Name']; $Upload = new Gdn_Upload(); $filepath_parsed = $Upload->saveAs($thumb_destination_path, $target, array('source' => 'content')); // Save thumbnail information to DB. $model->save(array('MediaID' => $media_id, 'StorageMethod' => $filepath_parsed['Type'], 'ThumbWidth' => $thumb_width, 'ThumbHeight' => $thumb_height, 'ThumbPath' => $filepath_parsed['SaveName'])); // Remove cf scratch copy, typically in cftemp, if there was actually a file pulled in from CF. if (strpos($local_path, 'cftemp') !== false) { if (!unlink($local_path)) { // Maybe add logging for local cf copies not deleted. } } $url = $filepath_parsed['Url']; } else { // Fix the thumbnail information so this isn't requested again and again. $model->save(array('MediaID' => $media_id, 'ImageWidth' => 0, 'ImageHeight' => 0, 'ThumbPath' => '')); $url = asset('/plugins/FileUpload/images/file.png'); } redirect($url, 301); }
/** * Save a message. * * @param array $FormPostValues Message data. * @param bool $Settings * @return int The MessageID. */ public function save($FormPostValues, $Settings = false) { // The "location" is packed into a single input with a / delimiter. Need to explode it into three different fields for saving $Location = val('Location', $FormPostValues, ''); if ($Location != '') { $Location = explode('/', $Location); $Application = val(0, $Location, ''); if (in_array($Application, $this->_SpecialLocations)) { $FormPostValues['Application'] = null; $FormPostValues['Controller'] = $Application; $FormPostValues['Method'] = null; } else { $FormPostValues['Application'] = $Application; $FormPostValues['Controller'] = val(1, $Location, ''); $FormPostValues['Method'] = val(2, $Location, ''); } } Gdn::cache()->remove('Messages'); return parent::save($FormPostValues, $Settings); }
/** * Start merging user accounts. * * @param int $OldUserID The ID of the old user. * @param int $NewUserID The ID of the new user. * @return int|null Returns the merge table ID of the merge. * @throws Gdn_UserException Throws an exception of there is a data validation error. */ private function mergeStart($OldUserID, $NewUserID) { $Model = new Gdn_Model('UserMerge'); // Grab the users. $OldUser = $this->getID($OldUserID, DATASET_TYPE_ARRAY); $NewUser = $this->getID($NewUserID, DATASET_TYPE_ARRAY); // First see if there is a record with the same merge. $Row = $Model->getWhere(['OldUserID' => $OldUserID, 'NewUserID' => $NewUserID])->firstRow(DATASET_TYPE_ARRAY); if ($Row) { $MergeID = $Row['MergeID']; // Save this merge in the log. if ($Row['Attributes']) { $Attributes = dbdecode($Row['Attributes']); } else { $Attributes = []; } $Attributes['Log'][] = ['UserID' => Gdn::session()->UserID, 'Date' => Gdn_Format::toDateTime()]; $Row = ['MergeID' => $MergeID, 'Attributes' => $Attributes]; } else { $Row = ['OldUserID' => $OldUserID, 'NewUserID' => $NewUserID]; } $UserSet = []; $OldUserSet = []; if (dateCompare($OldUser['DateFirstVisit'], $NewUser['DateFirstVisit']) < 0) { $UserSet['DateFirstVisit'] = $OldUser['DateFirstVisit']; } if (!isset($Row['Attributes']['User']['CountVisits'])) { $UserSet['CountVisits'] = $OldUser['CountVisits'] + $NewUser['CountVisits']; $OldUserSet['CountVisits'] = 0; } if (!empty($UserSet)) { // Save the user information on the merge record. foreach ($UserSet as $Key => $Value) { // Only save changed values that aren't already there from a previous merge. if ($NewUser[$Key] != $Value && !isset($Row['Attributes']['User'][$Key])) { $Row['Attributes']['User'][$Key] = $NewUser[$Key]; } } } $MergeID = $Model->save($Row); if (val('MergeID', $Row)) { $MergeID = $Row['MergeID']; } if (!$MergeID) { throw new Gdn_UserException($Model->Validation->resultsText()); } // Update the user with the new user-level data. $this->setField($NewUserID, $UserSet); if (!empty($OldUserSet)) { $this->setField($OldUserID, $OldUserSet); } return $MergeID; }
/** * Save the addon data. * * @param array $Stub * @param bool|array $Settings Not used; for signature compatibility. * @return bool|Gdn_DataSet|mixed|object|string */ public function save($Stub, $Settings = false) { trace('AddonModel->Save()'); $Session = Gdn::session(); $this->defineSchema(); // Most of the values come from the file itself. if (isset($Stub['Path'])) { $Path = $Stub['Path']; } elseif (val('Checked', $Stub)) { $Addon = $Stub; } elseif (isset($Stub['File'])) { $Path = combinePaths(array(PATH_UPLOADS, $Stub['File'])); } else { if (!$Session->checkPermission('Addons.Addon.Manage') && isset($Stub['Filename'])) { // Only admins can modify plugin attributes without the file. $this->Validation->addValidationResult('Filename', 'ValidateRequired'); return false; } } // Analyze and fix the file. if (!isset($Addon)) { if (isset($Path)) { try { $Addon = UpdateModel::analyzeAddon($Path, false); } catch (Exception $Ex) { $Addon = false; $this->Validation->addValidationResult('File', '@' . $Ex->getMessage()); } if (!is_array($Addon)) { $this->Validation->addValidationResult('File', 'Could not analyze the addon file.'); return false; } $Addon = array_merge($Stub, $Addon); } else { $Addon = $Stub; if (isset($Path)) { $Addon['MD5'] = md5_file($Path); $Addon['FileSize'] = filesize($Path); } } } // Get an existing addon. if (isset($Addon['AddonID'])) { $CurrentAddon = $this->getID($Addon['AddonID'], false, ['GetVersions' => true]); } elseif (isset($Addon['AddonKey']) && isset($Addon['AddonTypeID'])) { $CurrentAddon = $this->getID(array($Addon['AddonKey'], $Addon['AddonTypeID']), false, ['GetVersions' => true]); } else { $CurrentAddon = false; } trace($CurrentAddon, 'CurrentAddon'); $Insert = !$CurrentAddon; if ($Insert) { $this->addInsertFields($Addon); } $this->addUpdateFields($Addon); // always add update fields if (!$this->validate($Addon, $Insert)) { trace('Addon did not validate'); return false; } // Search for the current version. $MaxVersion = false; $CurrentVersion = false; if ($CurrentAddon && isset($Addon['Version'])) { // Search for a current version. foreach ($CurrentAddon['Versions'] as $Index => $Version) { if (isset($Addon['AddonVersionID'])) { if ($Addon['AddonVersionID'] == $Version['AddonVersionID']) { $CurrentVersion = $Version; } } elseif (version_compare($Addon['Version'], $Version['Version']) == 0) { $CurrentVersion = $Version; } // Only check for a current version if the version has been checked. if (!$Version['Checked']) { continue; } if (!$MaxVersion || version_compare($MaxVersion['Version'], $Version['Version'], '<')) { $MaxVersion = $Version; } } } // Save the addon. $Fields = $this->filterSchema($Addon); if ($Insert) { $AddonID = $this->SQL->insert($this->Name, $Fields); // Add the activity. $ActivityModel = new ActivityModel(); $Activity = array('ActivityType' => 'Addon', 'ActivityUserID' => $Fields['InsertUserID'], 'NotifyUserID' => ActivityModel::NOTIFY_PUBLIC, 'HeadlineFormat' => '{ActivityUserID,user} added the <a href="{Url,html}">{Data.Name}</a> addon.', 'Story' => Gdn_Format::html($Fields['Description']), 'Route' => '/addon/' . rawurlencode(self::slug($Fields, false)), 'Data' => array('Name' => $Fields['Name'])); $ActivityModel->save($Activity); } else { $AddonID = val('AddonID', $CurrentAddon); // Only save the addon if it is the current version. if (!$MaxVersion || version_compare($Addon['Version'], $MaxVersion['Version'], '>=')) { trace('Uploaded version is the most recent version.'); $this->SQL->put($this->Name, $Fields, array('AddonID' => $AddonID)); } else { $this->SQL->reset(); } } // Save the version. if ($AddonID && isset($Path) || isset($Addon['File'])) { trace('Saving addon version'); $Addon['AddonID'] = $AddonID; if (isset($Path)) { if (!stringBeginsWith($Path, PATH_UPLOADS . '/addons/')) { // The addon must be copied into the uploads folder. $NewPath = PATH_UPLOADS . '/addons/' . basename($Path); //rename($Path, $NewPath); $Path = $NewPath; $this->_AddonCache = array(); } $File = substr($Path, strlen(PATH_UPLOADS . '/')); $Addon['File'] = $File; } if ($CurrentVersion) { $Addon['AddonVersionID'] = val('AddonVersionID', $CurrentVersion); } // Insert or update the version. $VersionModel = new Gdn_Model('AddonVersion'); $AddonVersionID = $VersionModel->save($Addon); $this->Validation->addValidationResult($VersionModel->validationResults()); if (!$AddonVersionID) { return false; } // Update the current version in the addon. if (!$MaxVersion || version_compare($CurrentAddon['Version'], $Addon['Version'], '<')) { $this->SQL->put($this->Name, array('CurrentAddonVersionID' => $AddonVersionID), array('AddonID' => $AddonID)); } } $this->_AddonCache = array(); return $AddonID; }
/** * * * @param array $FormPostValues * @param bool $Settings * @return bool|unknown * @throws Exception */ public function save($FormPostValues, $Settings = false) { // Get the ID of an existing tag with the same name. $ExistingTag = $this->getWhere(array('Name' => $FormPostValues['Name'], 'TagID <>' => val('TagID', $FormPostValues)))->firstRow(DATASET_TYPE_ARRAY); if ($ExistingTag) { if (!val('TagID', $FormPostValues)) { return $ExistingTag['TagID']; } // This tag will be merged with the existing one. $Px = $this->Database->DatabasePrefix; $FromID = $FormPostValues['TagID']; $ToID = $ExistingTag['TagID']; try { $this->Database->beginTransaction(); // Delete all of the overlapping tags. $Sql = "delete tg.*\n from {$Px}TagDiscussion tg\n join {$Px}TagDiscussion tg2\n on tg.DiscussionID = tg2.DiscussionID\n and tg.TagID = :FromID and tg2.TagID = :ToID"; $this->Database->query($Sql, array(':FromID' => $FromID, ':ToID' => $ToID)); // Update the tagged discussions. $Sql = "update {$Px}TagDiscussion\n set TagID = :ToID\n where TagID = :FromID"; $this->Database->query($Sql, array(':FromID' => $FromID, ':ToID' => $ToID)); // Update the counts $this->updateTagCountDiscussions($ToID); // Delete the old tag. $Sql = "delete from {$Px}Tag where TagID = :FromID"; $this->Database->query($Sql, array(':FromID' => $FromID)); $this->Database->commitTransaction(); } catch (Exception $Ex) { $this->Database->rollbackTransaction(); throw $Ex; } return $ToID; } else { if (Gdn::session()->checkPermission('Plugins.Tagging.Add')) { return parent::save($FormPostValues, $Settings); } else { return false; } } }
/** * * * @param $Name * @param $Value */ public static function touch($Name, $Value) { $Model = new Gdn_Model('Pocket'); $Pockets = $Model->getWhere(array('Name' => $Name))->resultArray(); if (empty($Pockets)) { $Pocket = array('Name' => $Name, 'Location' => 'Content', 'Sort' => 0, 'Repeat' => Pocket::REPEAT_BEFORE, 'Body' => $Value, 'Format' => 'Raw', 'Disabled' => Pocket::DISABLED, 'MobileOnly' => 0, 'MobileNever' => 0, 'EmbeddedNever' => 0, 'ShowInDashboard' => 0, 'Type' => 'default'); $Model->save($Pocket); } }