Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Help with an extension

merfedmerfed New
edited October 2009 in Vanilla 1.0 Help
I'm trying to make an extension and I'm completely new to this. My goal is to create a thread "description" area. That will show under the thread name in the thread listing. How would I go about do this? Pointing me in the right direction would be great.

Comments

  • At the moment this is what I have. I can't get it to actually create the database yet.

    // Definitions $Context->Dictionary["TopicDesc"] = "Topic Abstract"; $Context->Dictionary["ErrInstallExtension"] = "LOL CAN'T SEE SHIT CAPN'"; // Install if (!array_key_exists('TOPICDESC', $Configuration)) { $DropTopicDesc = 'DROP TABLE IF EXISTS `' . $Context->Configuration['DATABASE_TABLE_PREFIX'] . 'TopicDesc`'; $CreateTopicDesc = 'CREATE TABLE `' . $Context->Configuration['DATABASE_TABLE_PREFIX'] . 'TopicDesc` (' . '`Desc` varchar(255) NOT NULL,' . '`DiscussionID int NOT NULL)'; $errors = 0; $connection = $Context->Database->GetConnection(); if (!mysql_query($DropTopicDesc, $connection)) $errors++; if (!mysql_query($CreateTopicDesc, $connection)) $errors++; if ($errors > 0) { $Context->WarningCollector->Add($Context->GetDefinition('ErrInstallExtension')); } else { AddConfigurationSetting($Context, 'TOPICDESC', '1'); } } // Database $DatabaseTables['TopicDesc'] = 'TopicDesc'; $DatabaseColumns['TopicDesc']['Desc'] = 'Desc'; $DatabaseColumns['TopicDesc']['DiscussionID'] = 'DiscussionID'; // Delegates $Context->AddToDelegate("DiscussionForm", "DiscussionForm_PreCommentRender", "AddTopicAbstract"); $Context->AddToDelegate("DiscussionForm", "PostSaveDiscussion", "SaveTopicDesc"); // Save Topic Desc function SaveTopicDesc(&$DiscussionForm) { global $Context; if( $Discussion = &$DiscussionForm->DelegateParameters['ResultDiscussion'] ) { foreach ($TopicDesc as &$TopicDesc) { if ($TopicDesc > "") { $query = "INSERT INTO `".$Context->Configuration['DATABASE_TABLE_PREFIX']."TopicDesc` (`Desc`, `DiscussionID`) VALUES ('".FormatStringForDatabaseInput($TopicDesc)."', '".$DiscussionForm->DelegateParameters['ResultDiscussion']->DiscussionID."');"; $result = $Context->Database->Execute($query,'','','An error occured adding a topic description.'); } } } } // Get Topic Desc function GetTopicDesc($DiscussionID) { global $Context; //$query = "SELECT Desc FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."TopicDesc` as t LEFT JOIN `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` as d ON t.DiscussionID = d.DiscussionID WHERE t.DiscussionID = '".$DiscussionID."' AND Active = 1;"; //$result = $Context->Database->Execute($query,'','','An error occured getting Topic Desc for this discussions.'); $TopicDescValue = ""; while (@$row = $Context->Database->GetRow($result)) { $TopicDescValue.= $row['Desc'] . ", "; } return $TopicDescValue; } // Add to Discussion Form function AddTopicAbstract(&$DiscussionForm) { global $Context; $TopicDescList = GetTopicDesc($DiscussionForm->DiscussionID); echo '<li> <label for="txtDesc">'.$Context->GetDefinition('TopicDesc').'</label> <input id="txtDesc" type="text" name="TopicDesc" class="DiscussionBox" value="'.$TopicDescList.'" /> </li>'; }
  • MarkMark Vanilla Staff
    If you're creating a one-to-one relationship, I'd consider just adding your field to the discussion table directly. It will save you some work when the discussion is being saved. Otherwise you're definitely on the right track. What do you mean "can't create database yet"? Your table isn't being created properly?
  • For whatever reason, when it attempted to make the database it failed. I got it to work by changing the name of the Desc column to something different.
  • This is what I got, and now I'm stumped. I can't figure out how to write the value to the database, and to pull it from said database. I'm not a familiar with php as I'd like, so any help would be great.

    <?php /* Extension Name: Topic Descriptions Extension Url: none Description: Allows topic descriptions to be set Version: 1.0 Author: merfed Author Url: none */ // Definitions $Context->Dictionary["TopicDesc"] = "Topic Abstract"; $Context->Dictionary["ErrInstallExtension"] = "LOL CAN'T SEE SHIT CAPN'"; // Install if (!array_key_exists('TOPICDESC', $Configuration)) { $DropTopicDesc = 'DROP TABLE IF EXISTS `' . $Context->Configuration['DATABASE_TABLE_PREFIX'] . 'TopicDesc`'; $CreateTopicDesc = 'CREATE TABLE `' . $Context->Configuration['DATABASE_TABLE_PREFIX'] . 'TopicDesc` (' . '`ThreadDesc` varchar(255) NOT NULL,' . '`DiscussionID` int NOT NULL)'; $errors = 0; $connection = $Context->Database->GetConnection(); if (!mysql_query($DropTopicDesc, $connection)) $errors++; if (!mysql_query($CreateTopicDesc, $connection)) $errors++; if ($errors > 0) { $Context->WarningCollector->Add($Context->GetDefinition('ErrInstallExtension')); } else { AddConfigurationSetting($Context, 'TOPICDESC', '1'); } } // Database $DatabaseTables['TopicDesc'] = 'TopicDesc'; $DatabaseColumns['TopicDesc']['ThreadDesc'] = 'ThreadDesc'; $DatabaseColumns['TopicDesc']['DiscussionID'] = 'DiscussionID'; // Delegates $Context->AddToDelegate("DiscussionForm", "DiscussionForm_PreCommentRender", "AddTopicAbstract"); $Context->AddToDelegate("DiscussionForm", "PostSaveDiscussion", "SaveTopicDesc"); // Save Topic Desc function SaveTopicDesc(&$DiscussionForm) { global $Context; if ($Discussion = &$DiscussionForm->DelegateParameters['ResultDiscussion']) { if (!isset($TopicDesc)) { return; } if ($TopicDesc > "") { $query = "INSERT INTO `".$Context->Configuration['DATABASE_TABLE_PREFIX']."TopicDesc` (`ThreadDesc`, `DiscussionID`) VALUES ('".FormatStringForDatabaseInput($TopicDesc)."', '".$DiscussionForm->DelegateParameters['ResultDiscussio']->DiscussionID."');"; $result = $Context->Database->Execute($query,'','','An error occured adding a topic description.'); } } } // Get Topic Desc function GetTopicDesc($DiscussionID) { global $Context; /* New Database Pull Code $dbPullThreads = $Context->ObjectFactory->NewContextObject($Context, 'SqlBuilder'); $dbPullThreads->SetMainTable('User', 'User'); $dbPullThreads->AddSelect('CountDiscussions', 'User'); $dbPullThreads->AddWhere('User', 'UserID', '', $Context->Session->User->UserID, '=', 'AND', '', 0); $PullDiscussions = $Context->Database->Select($dbPullThreads, 'Points', 'PullUserDetails', 'An error occured while attempting to retrieve Discussion Count'); while($row = $Context->Database->GetRow($PullDiscussions)) { if(!isset($row['CountDiscussions'])) continue; $Points_Count_Threads = $row['CountDiscussions']; } $query = "SELECT Desc FROM `".$Context->Configuration['DATABASE_TABLE_PREFIX']."TopicDesc` as t LEFT JOIN `".$Context->Configuration['DATABASE_TABLE_PREFIX']."Discussion` as d ON t.DiscussionID = d.DiscussionID WHERE t.DiscussionID = '".$DiscussionID."' AND Active = 1;"; $result = $Context->Database->Execute($query,'','','An error occured getting Topic Desc for this discussions.'); $TopicDescValue = ""; while (@$row = $Context->Database->GetRow($result)) { $TopicDescValue.= $row['Desc'] . ", "; } return $TopicDescValue; } // Add to Discussion Form function AddTopicAbstract(&$DiscussionForm) { global $Context; $TopicDesc = GetTopicDesc($DiscussionForm->DiscussionID); echo '<li> <label for="txtDesc">'.$Context->GetDefinition('TopicDesc').'</label> <input id="txtDesc" type="text" name="TopicDesc" class="DiscussionBox" value="'.$TopicDesc.'" /> </li>'; } ?>
  • MarkMark Vanilla Staff
    Again, I think you're overcomplicating things. You can add your description field to the discussion table directly, and then you don't have to worry about joining to a separate table to get your values.
Sign In or Register to comment.