We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 31471
    • 206 Posts
    In the ’v1.31 bugfix’ topic I started a conversation about multiple instances of WLPE, but since it’s not a bugfix I open a new topic for it.
    There it turned out that my first attempt of making it was "a bit" overworked by altering the placeholders.
    It also turned out that there are different needs when using multiple instances.
    My idea was to list photos on the sidebar that may not disappear during the edit of a profile in the content area.
    The other concept - thanks to BobRay - is to have multiple userlists with profile links, so opening a profile from either list must disable the other list.

    I tried to compile these all together so here are my recommendations:

    (I worked with v1.31 snippet, but may work with former versions.)
    First move the service request to the very beginning of the snippet so it doesn’t do anything if it’s not necessary. In the original code find
    	$service = $_REQUEST['service'];
    	if (empty($service) || $service == '')
    	{
    		$service = $_REQUEST['serviceButtonValue'];
    	}


    cut&paste it to the beginning, after the */ section, but before this line: [tt]$type = isset($type) ? $type : ’simple’;[/tt]

    After that continue with this (still before the [tt]$type =[/tt] ... code):
    	$instance = isset($instance) ? $instance:'';
    	$postinstance = $_REQUEST['instance'] ? $_REQUEST['instance'] : '';
    	$shy = isset($shy) ? $shy : 0; // or 1 if you prefer 'shy' as default ;)
    	if ($service && $instance != $postinstance) 
    	{
    		if ($shy) return; // skip this instance: 'shy' mode
    		$service=''; // run this instance without mess
    	}
    
    	if (!function_exists('AddInstance'))
    	{
    		function AddInstance($toTpl,$instance)
    		{
    			$toTpl = str_replace('</form>', '<input type="hidden" name="instance" value="'.$instance.'" /></form>', $toTpl);
    			$toTpl = str_replace('service=', 'instance='.$instance.'&service=', $toTpl);
    			return $toTpl;
    		}
    	}

    With this done we have two new parameters in the snippet call:
    &instance: we can give a discrete name to every appearance of the user datas on the page. It’s not necessary, however. You can combine named instances with a nameless one. So if your site already contains working WLPE without a name, don’t touch it, just add a new instance with this parameter - to say, to the sidebar.
    &shy: (1|0) we can decide if the given instance will hide when another one shows a profile or whatever form. If it’s not set, means not shy - good for a sidebar instance.

    Additionally we have a function that puts the instance’s name in the forms/templates so we can use our original Tpl’s. It’s also possible to use the same Tpl’s in different instances.
    For this to take place I call this function for every Tpl’s loaded. Find this part and replace:
    	if ($regType == 'verify'){$wlpeRegisterTpl = AddInstance($wlpeRegisterVerifyTpl, $instance);}else{$wlpeRegisterTpl = AddInstance($wlpeRegisterInstantTpl, $instance);}
    	
    	$displayLoginFormTpl = AddInstance(isset($loginFormTpl) ? $wlpe->Template($loginFormTpl) : $wlpeDefaultFormTpl, $instance);
    	$displaySuccessTpl = AddInstance(isset($successTpl) ? $wlpe->Template($successTpl) : $wlpeDefaultSuccessTpl, $instance);
    	$displayRegisterTpl = AddInstance(isset($registerTpl) ? $wlpe->Template($registerTpl) : $wlpeRegisterTpl, $instance);
    	$displayRegSuccessTpl = AddInstance(isset($registerSuccessTpl) ? $wlpe->Template($registerSuccessTpl) : $wlpeDefaultFormTpl, $instance);
    	$displayProfileTpl = AddInstance(isset($profileTpl) ? $wlpe->Template($profileTpl) : $wlpeProfileTpl, $instance);
    	$displayViewProfileTpl = AddInstance(isset($viewProfileTpl) ? $wlpe->Template($viewProfileTpl) : $wlpeViewProfileTpl, $instance);
    	$displayUsersOuterTpl = AddInstance(isset($usersOuterTpl) ? $wlpe->Template($usersOuterTpl) : $wlpeUsersOuterTpl, $instance);
    	$displayUsersTpl = AddInstance(isset($usersTpl) ? $wlpe->Template($usersTpl) : $wlpeUsersTpl, $instance);
    	$displayManageOuterTpl = AddInstance(isset($manageOuterTpl) ? $wlpe->Template($manageOuterTpl) : $wlpeUsersOuterTpl, $instance);
    	$displayManageTpl = AddInstance(isset($manageTpl) ? $wlpe->Template($manageTpl) : $wlpeManageTpl, $instance);
    	$displayManageProfileTpl = AddInstance(isset($manageProfileTpl) ? $wlpe->Template($manageProfileTpl) : $wlpeManageProfileTpl, $instance);
    	$displayManageDeleteTpl = AddInstance(isset($manageDeleteTpl) ? $wlpe->Template($manageDeleteTpl) : $wlpeManageDeleteTpl, $instance);
    	$displayProfileDeleteTpl = AddInstance(isset($profileDeleteTpl) ? $wlpe->Template($profileDeleteTpl) : $wlpeProfileDeleteTpl, $instance);
    	$displayActivateTpl = AddInstance(isset($activateTpl) ? $wlpe->Template($activateTpl) : $wlpeActivateTpl, $instance);
    	$displayResetTpl = AddInstance(isset($resetTpl) ? $wlpe->Template($resetTpl) : $wlpeResetTpl, $instance);
    	$notifyTpl = AddInstance(isset($notifyTpl) ? $wlpe->Template($notifyTpl) : $wlpeNotifyTpl, $instance);
    	$notifySubject = isset($notifySubject) ? $notifySubject : 'New Web User for '.$modx->config['site_name'].'.';
    	$messageTpl = isset($messageTpl) ? $wlpe->Template($messageTpl) : $wlpeMessageTpl;
    	$tosChunk = isset($tosChunk) ? $wlpe->Template($tosChunk) : $wlpeTos;
    	$modx->setPlaceholder('tos', $tosChunk);
    	$loadJquery = isset($loadJquery) ? $loadJquery : false;
    	$customJs = isset($customJs) ? $customJs : '';

    I tested with three instances. Two in the content area was shy but only one of them had an instance name. The third one was not shy and sit in the sidebar.
    Please test it! I attach the altered snippet code.
      • 3749
      • 24,544 Posts
      A few comments:

      This seems more complicated than necessary but I don’t really understand all the cases where an instance is necessary so maybe this is the best way to go.

      It might make more sense to put the function in the wlpe class file and $instanceID would then be added to the member variables in the class. The constructor can initialize the variable and it would be available to the snippet as $classname->instanceID.

      Also, I would change this:

      $postinstance = $_REQUEST['instance'] ? $_REQUEST['instance'] : '';


      to this:

      $requestInstance = $_REQUEST['instance'] ? $_REQUEST['instance'] : '';


        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 31471
        • 206 Posts
        	if ($service && $instance != $postinstance) 
        	{
        		if ($shy) return; // skip this instance
        		$service=''; // run this instance without mess
        	}

        What can be any simpler than this?  rolleyes

        The rest of the code is automatic templating, so that the user (manager) can use her/his original templates or the default ones.
        For the function to run in the wlpe class I like that idea and will make it. (But as a band-aid it’s easier to patch the snippet only.)

        Thanks your advice!

        EDIT: I moved the function to the wlpe class and also renamed $postinstance. Both the snippet and the class are in the zip.
          • 31471
          • 206 Posts
          I found one problem with this solution:
          If you use the &usersList parameter in your snippet call, the ’outerTplChunk’ and the ’userTplChunk’ templates will be taken in the class instead of the snippet. This means the ’AddInstance’ function (which is called from the snippet) can not add the necessary identification to these templates.
          When you call only one list you can workaround by adding the templates outside the &usersList parameter (&manageTpl=`ManageTpl` &manageOuterTpl=`ManageOuterTpl`) and using ’default’ in &usersList. In this case the class takes the instanced templates.

          -----------------------

          But what if we want to get more than one lists via ’&usersList’ each lists using different templates? The above solution is only OK for the use of the same template in all lists.
          So we must tell the class the actual instancename and let it add that to the templates.

          To every ViewAllUsers function call we must add the $instance variable. This matches one
          [tt]$userpage = $wlpe->ViewAllUsers($displayManageTpl, $displayManageOuterTpl, $usersList, $instance);[/tt]

          and three

          [tt]$manageUsersPage = $wlpe->ViewAllUsers($displayManageTpl, $displayManageOuterTpl, $usersList, $instance);[/tt]

          lines in the snippet.

          Now it’s the class’s turn to add the instance ID to the templates in use.
          Line 1038: [tt]function ViewAllUsers($userTemplate, $outerTemplate, $listUsers, $instance)[/tt]
          to get the ID in this function.

          Line 1066: [tt]$listOuterTemplate = $this->AddInstance($this->Template($listOuterTemplate),$instance);[/tt]

          and

          Line 1077: [tt]$listTemplate = $this->AddInstance($this->Template($listTemplate),$instance);[/tt]

          I also modified the ’AddInstance’ function to change the template only if it gets an instance ID:
          	function AddInstance($toTpl,$instance='')
          	{
          		if($instance) {
          			$toTpl = str_replace('</form>', '<input type="hidden" name="instance" value="'.$instance.'" /></form>', $toTpl);
          			$toTpl = str_replace('service=', 'instance='.$instance.'&service=', $toTpl);
          		}
          		return $toTpl;
          	}

          at the end of the class. (Note:"instance" is for the previously posted solutions! The attached version contains "wlpeID" instead!!!)

          The modified snippet and class are attached to this post.
          Note that in the attached version I changed parameternames &instance to &id and &shy to &hide! And the templates get ?wlpeID="..." instead of ?instance="...".