Index a result array.
public static index ( array | Traversable $Data, string | array $Columns, array $Options = [] ) : array | ||
$Data | array | Traversable | The array to index. It is formatted similar to the array returned by Gdn_DataSet::Result(). |
$Columns | string | array | The name of the column to index on or an array of columns to index on. |
$Options | array | An array of options for the method. - Sep: The string to seperate index columns by. Default '|'. - Unique: Whether or not the results are unique. - true (default): The index is unique. - false: The index is not unique and each indexed row will be an array or arrays. |
return | array |
/** * Get and store list of current bans. * * @since 2.0.18 * @access public */ public static function &allBans() { if (!self::$_AllBans) { self::$_AllBans = Gdn::sql()->get('Ban')->resultArray(); self::$_AllBans = Gdn_DataSet::index(self::$_AllBans, array('BanID')); } // $AllBans =& self::$_AllBans; return self::$_AllBans; }
/** * * * @return array|null|type */ public function getProviders() { $this->SQL->select('uap.*')->from('UserAuthenticationProvider uap'); if (Gdn::session()->isValid()) { $UserID = Gdn::session()->UserID; $this->SQL->select('ua.ForeignUserKey', '', 'UniqueID')->join('UserAuthentication ua', "uap.AuthenticationKey = ua.ProviderKey and ua.UserID = {$UserID}", 'left'); } $Data = $this->SQL->get()->resultArray(); $Data = Gdn_DataSet::index($Data, array('AuthenticationKey')); foreach ($Data as &$Row) { self::calculate($Row); } return $Data; }
/** * Get all messages or one message. * * @param int|bool $ID ID of message to get. * @return array|null */ public static function messages($ID = false) { if ($ID === null) { Gdn::cache()->remove('Messages'); return; } $Messages = Gdn::cache()->get('Messages'); if ($Messages === Gdn_Cache::CACHEOP_FAILURE) { $Messages = Gdn::sql()->get('Message', 'Sort')->resultArray(); $Messages = Gdn_DataSet::index($Messages, array('MessageID')); Gdn::cache()->store('Messages', $Messages); } if ($ID === false) { return $Messages; } else { return val($ID, $Messages); } }
/** * * * @param array $IDs * @param bool $SkipCacheQuery * @return array * @throws Exception */ public function getIDs($IDs, $SkipCacheQuery = false) { $DatabaseIDs = $IDs; $Data = []; if (!$SkipCacheQuery) { $Keys = []; // Make keys for cache query foreach ($IDs as $UserID) { if (!$UserID) { continue; } $Keys[] = formatString(self::USERID_KEY, ['UserID' => $UserID]); } // Query cache layer $CacheData = Gdn::cache()->get($Keys); if (!is_array($CacheData)) { $CacheData = []; } foreach ($CacheData as $RealKey => $User) { if ($User === null) { $ResultUserID = trim(strrchr($RealKey, '.'), '.'); } else { $ResultUserID = val('UserID', $User); } $this->setCalculatedFields($User); $Data[$ResultUserID] = $User; } //echo "from cache:\n"; //print_r($Data); $DatabaseIDs = array_diff($DatabaseIDs, array_keys($Data)); unset($CacheData); } // Clean out bogus blank entries $DatabaseIDs = array_diff($DatabaseIDs, [null, '']); // If we are missing any users from cache query, fill em up here if (sizeof($DatabaseIDs)) { $DatabaseData = $this->SQL->whereIn('UserID', $DatabaseIDs)->getWhere('User')->result(DATASET_TYPE_ARRAY); $DatabaseData = Gdn_DataSet::index($DatabaseData, 'UserID'); //echo "from DB:\n"; //print_r($DatabaseData); foreach ($DatabaseIDs as $ID) { if (isset($DatabaseData[$ID])) { $User = $DatabaseData[$ID]; $this->userCache($User, $ID); // Apply calculated fields $this->setCalculatedFields($User); $Data[$ID] = $User; } else { $User = null; $this->userCache($User, $ID); } } } $this->EventArguments['RequestedIDs'] = $IDs; $this->EventArguments['LoadedUsers'] =& $Data; $this->fireEvent('AfterGetIDs'); return $Data; }
/** * Do code checks on an uploaded addon. * * @param int $AddonID Addon to check. * @param bool|false $SaveVersionID Whether to save the version id. * @throws Exception Addon not found. */ public function check($AddonID, $SaveVersionID = false) { $this->permission('Addons.Addon.Manage'); if ($SaveVersionID !== false) { // Get the version data. $Version = $this->AddonModel->SQL->getWhere('AddonVersion', array('AddonVersionID' => $SaveVersionID))->firstRow(DATASET_TYPE_ARRAY); $this->AddonModel->save($Version); $this->Form->setValidationResults($this->AddonModel->validationResults()); } $Addon = $this->AddonModel->getID($AddonID, false, ['GetVersions' => true]); $AddonTypes = Gdn::sql()->get('AddonType')->resultArray(); $AddonTypes = Gdn_DataSet::index($AddonTypes, 'AddonTypeID'); if (!$Addon) { throw notFoundException('Addon'); } // Get the data for the most recent version of the addon. $upload = new Gdn_Upload(); // Also used per version below. $Path = $upload->copyLocal($Addon['File']); $AddonData = arrayTranslate((array) $Addon, array('AddonID', 'AddonKey', 'Name', 'Type', 'Description', 'Requirements', 'Checked')); try { $FileAddonData = UpdateModel::analyzeAddon($Path); if ($FileAddonData) { $AddonData = array_merge($AddonData, arrayTranslate($FileAddonData, array('AddonKey' => 'File_AddonKey', 'Name' => 'File_Name', 'File_Type', 'Description' => 'File_Description', 'Requirements' => 'File_Requirements', 'Checked' => 'File_Checked'))); $AddonData['File_Type'] = valr($FileAddonData['AddonTypeID'] . '.Label', $AddonTypes, 'Unknown'); } $upload->delete($Path); } catch (Exception $Ex) { $AddonData['File_Error'] = $Ex->getMessage(); } $this->setData('Addon', $AddonData); // Go through the versions and make sure we get the versions to check out. $Versions = array(); foreach ($Addon['Versions'] as $Version) { $Version = $Version; $Path = $upload->copyLocal($Version['File']); try { $VersionData = arrayTranslate((array) $Version, array('AddonVersionID', 'Version', 'AddonKey', 'Name', 'MD5', 'FileSize', 'Checked')); $FileVersionData = UpdateModel::analyzeAddon($Path); $FileVersionData = arrayTranslate($FileVersionData, array('Version' => 'File_Version', 'AddonKey' => 'File_AddonKey', 'Name' => 'File_Name', 'MD5' => 'File_MD5', 'FileSize' => 'File_FileSize', 'Checked' => 'File_Checked')); $upload->delete($Path); } catch (Exception $Ex) { $FileVersionData = array('File_Error' => $Ex->getMessage()); } $Versions[] = array_merge($VersionData, $FileVersionData); } $this->setData('Versions', $Versions); $this->addModule('AddonHelpModule'); $this->render(); }
/** * Joins attachments to data * * <code> * <?php * $AttachmentModel->JoinAttachments($Discussion, $Comments); * ?> * </code> * * @param $Data - Data to which to attach comments * @param $Data2 - Optional set of Data to which to attach comments * */ public function joinAttachments(&$Data, &$Data2 = null) { if ($Data == null) { return; } // Gather the Ids. $ForeignIDs = array(); self::gatherIDs($Data, $ForeignIDs); if ($Data2) { self::gatherIDs($Data2, $ForeignIDs); } // Get the attachments. $Attachments = $this->getWhere(array('ForeignID' => array_keys($ForeignIDs)), 'DateInserted', 'desc')->resultArray(); $Attachments = Gdn_DataSet::index($Attachments, 'ForeignID', array('Unique' => false)); // Join the attachments. $this->joinAttachmentsTo($Data, $Attachments); if ($Data2) { $this->joinAttachmentsTo($Data2, $Attachments); } }
/** * Add the tag input to the discussion form. * * @param Gdn_Controller $Sender */ public function postController_afterDiscussionFormOptions_handler($Sender) { if (in_array($Sender->RequestMethod, array('discussion', 'editdiscussion', 'question'))) { // Setup, get most popular tags $TagModel = TagModel::instance(); $Tags = $TagModel->getWhere(array('Type' => array_keys($TagModel->defaultTypes())), 'CountDiscussions', 'desc', c('Plugins.Tagging.ShowLimit', 50))->Result(DATASET_TYPE_ARRAY); $TagsHtml = count($Tags) ? '' : t('No tags have been created yet.'); $Tags = Gdn_DataSet::index($Tags, 'FullName'); ksort($Tags); // The tags must be fetched. if ($Sender->Request->isPostBack()) { $tag_ids = TagModel::SplitTags($Sender->Form->getFormValue('Tags')); $tags = TagModel::instance()->getWhere(array('TagID' => $tag_ids))->resultArray(); $tags = array_column($tags, 'TagID', 'FullName'); } else { // The tags should be set on the data. $tags = array_column($Sender->data('Tags', array()), 'FullName', 'TagID'); $xtags = $Sender->data('XTags', array()); foreach (TagModel::instance()->defaultTypes() as $key => $row) { if (isset($xtags[$key])) { $xtags2 = array_column($xtags[$key], 'FullName', 'TagID'); foreach ($xtags2 as $id => $name) { $tags[$id] = $name; } } } } echo '<div class="Form-Tags P">'; // Tag text box echo $Sender->Form->label('Tags', 'Tags'); echo $Sender->Form->textBox('Tags', array('data-tags' => json_encode($tags))); // Available tags echo wrap(Anchor(t('Show popular tags'), '#'), 'span', array('class' => 'ShowTags')); foreach ($Tags as $Tag) { $TagsHtml .= anchor(htmlspecialchars($Tag['FullName']), '#', 'AvailableTag', array('data-name' => $Tag['Name'], 'data-id' => $Tag['TagID'])) . ' '; } echo wrap($TagsHtml, 'div', array('class' => 'Hidden AvailableTags')); echo '</div>'; } }
/** * Add another user to the conversation. * * @since 2.0.0 * @access public * * @param int $ConversationID Unique ID of conversation effected. * @param int $UserID Unique ID of current user. */ public function addUserToConversation($ConversationID, $UserID) { if (!is_array($UserID)) { $UserID = array($UserID); } // First define the current users in the conversation $OldContributorData = $this->getRecipients($ConversationID); $OldContributorData = Gdn_DataSet::index($OldContributorData, 'UserID'); $AddedUserIDs = array(); // Get some information about this conversation $ConversationData = $this->SQL->select('LastMessageID')->select('DateUpdated')->select('CountMessages')->from('Conversation')->where('ConversationID', $ConversationID)->get()->firstRow(); // Add the user(s) if they are not already in the conversation foreach ($UserID as $NewUserID) { if (!array_key_exists($NewUserID, $OldContributorData)) { $AddedUserIDs[] = $NewUserID; $this->SQL->insert('UserConversation', array('UserID' => $NewUserID, 'ConversationID' => $ConversationID, 'LastMessageID' => $ConversationData->LastMessageID, 'CountReadMessages' => 0, 'DateConversationUpdated' => $ConversationData->DateUpdated)); } elseif ($OldContributorData[$NewUserID]->Deleted) { $AddedUserIDs[] = $NewUserID; $this->SQL->put('UserConversation', array('Deleted' => 0), array('ConversationID' => $ConversationID, 'UserID' => $NewUserID)); } } if (count($AddedUserIDs) > 0) { $ActivityModel = new ActivityModel(); foreach ($AddedUserIDs as $AddedUserID) { $ActivityModel->queue(array('ActivityType' => 'AddedToConversation', 'NotifyUserID' => $AddedUserID, 'HeadlineFormat' => t('You were added to a conversation.', '{ActivityUserID,User} added you to a <a href="{Url,htmlencode}">conversation</a>.'), 'Route' => '/messages/' . $ConversationID), 'ConversationMessage'); } $ActivityModel->saveQueue(); $this->updateUserUnreadCount($AddedUserIDs); $this->updateParticipantCount($ConversationID); } }
/** * Join the tags to a set of discussions. * * @param $data */ public function joinTags(&$data) { $ids = array(); foreach ($data as $row) { $discussionId = val('DiscussionID', $row); if ($discussionId) { $ids[] = $discussionId; } } // Select the tags. $all_tags = $this->SQL->select('td.DiscussionID, t.TagID, t.Name, t.FullName')->from('TagDiscussion td')->join('Tag t', 't.TagID = td.TagID')->whereIn('td.DiscussionID', $ids)->get()->resultArray(); $all_tags = Gdn_DataSet::index($all_tags, 'DiscussionID', array('Unique' => false)); foreach ($data as &$row) { $discussionId = val('DiscussionID', $row); if (isset($all_tags[$discussionId])) { $tags = $all_tags[$discussionId]; if ($this->StringTags) { $tags = consolidateArrayValuesByKey($tags, 'Name'); setValue('Tags', $row, implode(',', $tags)); } else { foreach ($tags as &$trow) { unset($trow['DiscussionID']); } setValue('Tags', $row, $tags); } } else { if ($this->StringTags) { setValue('Tags', $row, ''); } else { setValue('Tags', $row, array()); } } } }
/** * * * @param type $Activities * @since 2.1 */ public function joinComments(&$Activities) { // Grab all of the activity IDs. $ActivityIDs = array(); foreach ($Activities as $Activity) { if ($ID = val('CommentActivityID', $Activity['Data'])) { // This activity shares its comments with another activity. $ActivityIDs[] = $ID; } else { $ActivityIDs[] = $Activity['ActivityID']; } } $ActivityIDs = array_unique($ActivityIDs); $Comments = $this->getComments($ActivityIDs); $Comments = Gdn_DataSet::index($Comments, array('ActivityID'), array('Unique' => false)); foreach ($Activities as &$Activity) { $ID = val('CommentActivityID', $Activity['Data']); if (!$ID) { $ID = $Activity['ActivityID']; } if (isset($Comments[$ID])) { $Activity['Comments'] = $Comments[$ID]; } else { $Activity['Comments'] = array(); } } }