Ning Developer Network

Ning Developer Admin

Adding a Feature While on Standard Code

Featuring Advanced Member Search

Meta

Source Code Access RequiredNo
Skills RequiredPHP, XML, HTTP

Introduction

There are many reasons that you may want to stick with standard (centralized) code. You may want the updates as soon as they arrive, or you may simply wish to tinker without worrying about code mergers. You can indeed still write PHP code for your social network while staying on the standard code.

This tutorial builds on the Advanced Member Search tutorial. It describes how to shoe-horn this functionality into a social network running the standard code. This same technique should work for many features in other tutorials, but it will not work for all of them. Some features are simply not available unless your feature is implemented as part of the Ning Web Widget Framework. Furthermore, this technique involves cramming all of the templates and models into a single PHP file. This means it will obviously quickly become a nightmare for more complex features.

Prerequisites

I strongly recommend that you run through the Advanced Member Search tutorial before attempting this tutorial.

Getting Started

To get started on this tutorial we'll need to create a PHP file in which to implement this feature. We'll also need to import a few libraries in order to access functions provided by the social network.

  1. Create a single php file named memberSearch.php in the root directory of your social network. This is where we'll be pasting and hacking our code.
  2. Paste the following into the top of this file. This loads some useful libraries for accessing many of the features available to WWF widgets. You can use this same snippet of code for other features you wish to implement as well.
    <?php
    //include some stuff that we need
    define('NF_APP_BASE',$_SERVER['DOCUMENT_ROOT']);
    require 'WWF/bot.php';
    require NF_APP_BASE . '/lib/XG_App.php';
    require_once NF_APP_BASE . '/lib/XG_Query.php';
    call_user_func(array(W_Cache::getClass('app'),'loadWidgets'));
    $user = User::loadOrCreate(XN_Profile::current());

Adding the controller

For the next phase we'll need to copy in the contents of the controller. In this case we have only one controller so it'll be pretty easy, but if multiple controllers exist on the feature that you're attempting to implement you may have to get creative with some switching code.

  1. Copy and paste into your file the entire contents of the /widgets/memberSearch/controllers/IndexController.php.
  2. We're not going to be able to inherit from the base class, so remove the extends W_Controller from the class declaration.
  3. We need to manually instantiate the class and execute the action_index() function. Add this code above the class declaration to accomplish this.
    //create a new instance of this class
    $page = new MemberSearch_IndexController();
    $page->action_index();
  4. The render() function is no longer accessible to us. We need to hack our own implementation. Luckily we only make two calls so this will be an easy replacement. Near line 92 replace $this->render("searchResults"); with $this->renderSearchResults(); and $this->render("searchForm"); with $this->renderSearchForm();.

Adding in the templates

We're almost there! Next we simply have to create those render methods we called in the last step. We'll implement these as class functions and paste in the template contents.

  1. Create a function public function renderSearchForm() and paste the contents of the /widgets/memberSearch/templates/index/searchForm.php file into this function. You may need to clean up extraneous begin and end PHP tags.
  2. Repeat for /widgets/memberSearch/templates/index/searchResults.php
  3. Save the file and check it our with your web browser. It can be found at http://networkname.ning.com/memberSearch.php. This is when we check for errors.
  4. Notice that there's an error!
    error message
    Don't worry it appears that our sidebar function is simply not accessible. Let's remove it.
  5. Search the code for the two places where we call xg_sidebar($this);. You should find one in each template method.  Comment both of them out.
  6. Check the feature again. It looks like everything works now!

Hooking it all up

So now you have a member search that you can access directly. What if you want to add a tab to access it? No problem. You can use the JavaScript technique outlined here.

Conclusion

This development technique is far from graceful and has many limitations. Be sure to test your feature our throughly as it's hard to predict what will not work when implemented this way. Once your testing is complete enjoy your feature while still receiving automatic updates!

Lastly, if you ran into a hitch or just don't want to bother going through this tutorial, click here to download a copy of the final code that results from this tutorial in a zip file.

Last updated by Ning Developer Admin May 9.

We're Hiring

We are looking for talented and passionate individuals to join our growing team.

Visit our engineering jobs and see if Ning is right for you.

© 2008   Created by Ning Developer Admin

Badges  |  Report an Issue  |  Privacy  |  Terms of Service