1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How create a list of existing categories?

Discussion in 'XenForo Development Discussions' started by Zephyr, Mar 14, 2013.

  1. Zephyr

    Zephyr Well-Known Member

    Hello, i would like know how create a list of existing categories for admin options please.

    Can you help me please ?
    It's for remplace this option:

    [​IMG]

    Thanks :)
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    I don't have access to a development environment of work, but I've found an option that has a node-listing in a drop down. Checkout Options > Spam Management > Spam Cleaner Thread Action and look to see how XF has done that.
     
  3. cclaerhout

    cclaerhout Well-Known Member

    If I have correctly understood your question, I think I've done it in the ToogleMe addon:

    Check this class Sedo_ToggleME_Option_Factory and this part of the code:
    PHP:
        public static function render_nodes(XenForo_View $view$fieldPrefix, array $preparedOption$canEdit)
        {
            
    $preparedOption['formatParams'] = XenForo_Model::create('Sedo_ToggleME_Model_GetNodes')->getNodesOptions($preparedOption['option_value']);
            return 
    XenForo_ViewAdmin_Helper_Option::renderOptionTemplateInternal('option_list_option_checkbox'$view$fieldPrefix$preparedOption$canEdit);
        }
    Then check this class Sedo_ToggleME_Model_GetNodes (all of it)

    Your XenForo option must use a callback. See in the addon or just check this part in the install xml:
    Code:
        <option option_id="toggleME_Usergroups_Forumhome" edit_format="callback" data_type="array" can_backup="1">
          <default_value>a:4:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";}</default_value>
          <edit_format_params>Sedo_ToggleME_Option_Factory::render_usergroups</edit_format_params>
          <sub_options>*</sub_options>
          <relation group_id="toggleME" display_order="200"/>
        </option>
    
     
  4. Zephyr

    Zephyr Well-Known Member

    Thank you, it's good :)

    [​IMG]

    But how can I make it to display as dropdown please ? :)

    Edit: j'ai essayé ça mais j'ai une erreur:
    [​IMG]

    I would like the user to be able to select only one result in a dropdown.
     
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    change the template used within the callback to be option_list_option_select, and you should be good.
     
  6. Zephyr

    Zephyr Well-Known Member

    Thanks, it's ok:
    [​IMG]


    But I have this errors:

    Template Errors: option_list_option_select

    1. htmlspecialchars() expects parameter 1 to be string, array givenin /home/zephyr62/www/XenCreaDemo/Forum/library/XenForo/Template/Abstract.php(265) : eval()'d code, line 9:
      8: ';
      9: $__output .= XenForo_Template_Helper_Admin::selectUnit(htmlspecialchars($preparedOption['title']), htmlspecialchars($fieldPrefix) . '[' . htmlspecialchars($preparedOption['option_id']) . ']', htmlspecialchars($preparedOption['option_value']), $__compilerVar1, array(
      10: 'hint' => htmlspecialchars($preparedOption['hint']),
     
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    Change your data type from "Array" to string (or integer).
     
  8. Zephyr

    Zephyr Well-Known Member

    String or Integer =>
    An exception occurred: in_array() expects parameter 2 to be array, string given in /home/zephyr62/www/XenCreaDemo/Forum/library/XenCrea/Colorize/Model/GetNodes.php on line 14

    :S
     
  9. Jeremy

    Jeremy XenForo Moderator Staff Member

    I don't have enouhg information to help you. I need to see XenCrea_Colorize_Model_GetNodes to help you out there. But without looking, its most likely you need to get rid of an in_array() and just pull in the options.
     
  10. Zephyr

    Zephyr Well-Known Member

    Oups sorry ^^

    HTML:
    <?php
     
    class XenCrea_Colorize_Model_GetNodes extends XenForo_Model
    {
        public function getNodesOptions($selectedNodesIds)
        {
     
            $nodes = array();
            foreach ($this->getDbNodes() AS $node)
            {
                $nodes[] = array(
                'label' => $node['title'],
                'value' => $node['node_id'],
                'selected' => in_array($node['node_id'], $selectedNodesIds)
                );
            }
     
            return $nodes;
        }
     
        public function getDbNodes()
        {
     
            return $this->_getDb()->fetchAll('
            SELECT node_id, title, node_type_id
            FROM xf_node
            WHERE node_type_id = ?
            ORDER BY node_id
            ', 'Category');
     
        }
    }
    I have remplaced:
    'selected' => in_array($node['node_id'], $selectedNodesIds)

    by
    'selected' => $selectedNodesIds

    But no work :S
     
  11. Jeremy

    Jeremy XenForo Moderator Staff Member

    You most likely want:
    PHP:
    'selected' => ($node['node_id'] == $selectedNodesIds)
     
  12. cclaerhout

    cclaerhout Well-Known Member

    There are many things you need to modify if you want a list and according to the type of list you want, but are you sure you really want that? I mean do you want to use a list with a single selection (string) or with several selections (array) ?

    If it's a single selection, XenForo has a pre-made template for this
    1) Do what King Kovifor told you
    PHP:
        public function getNodesOptions($selectedNodesId)
        {
     
            
    $nodes = array();
            foreach (
    $this->getDbNodes() AS $node)
            {
                
    $nodes[] = array(
                
    'label' => $node['title'],
                
    'value' => $node['node_id'],
                
    'selected' => ($node['node_id'] == $selectedNodesId)
                );
            }
     
            return 
    $nodes;
        }
    2) Change the template rendering name in this code
    PHP:
            return XenForo_ViewAdmin_Helper_Option::renderOptionTemplateInternal('option_list_option_select'$view$fieldPrefix$preparedOption$canEdit);
    The template name is here "option_list_option_select"

    3) Edit the option in XenOptions, change array to boolean (forum id will be booleans) and delete the sub-array "*"

    It will work

    If you want a multi selection list:
    1) Create a new admin template
    For example: "option_list_option_youraddon_multiselect"

    Here's the code Jaxel already used for XenPorta:
    HTML:
    <xen:selectunit label="{$preparedOption.title}" name="{$fieldPrefix}[{$preparedOption.option_id}]" value="{xen:raw $preparedOption.option_value}"
    hint="{$preparedOption.hint}" size="10" multiple="true">
        <xen:options source="$formatParams" />
        <xen:explain>{xen:raw $preparedOption.explain}</xen:explain>
        <xen:html>
            <input type="hidden" name="{$listedFieldName}" value="{$preparedOption.option_id}" />
            {xen:raw $editLink}
        </xen:html>
    </xen:selectunit>
    
    2) Change the template rendering name in this code
    PHP:
            return XenForo_ViewAdmin_Helper_Option::renderOptionTemplateInternal('option_list_option_youraddon_multiselect'$view$fieldPrefix$preparedOption$canEdit);
    3) That's all (it's still an array)
     
    CrispinP likes this.
  13. Zephyr

    Zephyr Well-Known Member

    Thanks, I have no errors but I still have problems.

    List is ok, I do not have a null value possible, I need one.

    [​IMG]

    Then I have no errors, but when I agree, it is always the category which has id 1 which is stored in the database (I checked by displaying the variables in my views).

    Finally, do you know where I can add code to load a javascript/jquery and css to my page options please?
    I tried to add it in the description of an option, but no work even though the code seems to load, It's functional (tested in a view).

    Anyway thank you for your help, I would offer you the style (premium style) that will work with the addon when it will be ready in a few days it should please you, It's very colorful, flexible and lovely at the same time! ^^
     
  14. cclaerhout

    cclaerhout Well-Known Member

    Hey, that's a basic array, you just need to add an element in it;)
    I don't know which system you've chosen, but replace "$nodes = array();" with something like this:
    Code:
                $nodes[] = array(
                'label' => 'yourLabel',
                'value' => 'yourBlankValue',
                'selected' => in_array('yourBlankValue', $selectedNodesIds)
                );
    
    Nobody can reply to that question. All depends what you want to do, if the JS must be load on every page on not, etc. Just find the best template hook listeners (or post template listener) to add your own template (after to have cached it) with your JS code or CSS. Or use TMS. It's your decision. If you choose the listener option, try to add a timestamp to your JS file (what I often forgot to do). You can find an example in the ToggleMe addon.

    The listener (simplified for the example):
    PHP:
                case 'page_container_head':
                    
    $viewParams = array(
                        
    'toggleme_js_version' => filemtime("js/sedo/toggleme/toggleME.js")
                    );
     
                    
    $contents .= $template->create('toggleme_page_container_js'$viewParams);
                break;
    The template:
    HTML:
    <xen:require js="js/sedo/toggleme/toggleME.js?{$toggleme_js_version}" />
     
  15. Zephyr

    Zephyr Well-Known Member

    I'm not sure I understood what to do:

    (Fr: Pour être sûr de se comprendre, je cherche juste à rajouter un champ null à ma liste de catégorie pour que par défaut il ne puisse pas y avoir deux fois la même catégorie de sélectionné et ne pas créer de problèmes tout en faisant disparaitre le bug qui définie toujours la catégorie 1, j'arrive à bien dev avec Symfony2 mais avec Zend c'est une torture quand on connais pas les bases et ça me ralentit dans certain webdesign qui fonctionnent avec addon :S N'à tu jamais pensé, eu l'envie ou même le temps de faire des tutoriels pour la communauté fr ? Je ne sait pas si cela t’intéresserait et même si ça aiderais beaucoup de monde, mais en tout cas je serait un fervent lecteur si tu te lancez la dedans ! ^^ )

    HTML:
    <?php
     
    class XenCrea_Colorize_Model_GetNodes extends XenForo_Model
    {
        public function getNodesOptions($selectedNodesId)
        {
     
            $nodes[] = array(
                'label' => $node['title'],
                'value' => 'yourBlankValue',
                'selected' => ('yourBlankValue', $selectedNodesIds)
                );
     
            foreach ($this->getDbNodes() AS $node)
            {
                $nodes[] = array(
                'label' => $node['title'],
                'value' => $node['node_id'],
                'selected' => ($node['node_id'] == $selectedNodesId)
                );
            }
     
            return $nodes;
        }
     
        public function getDbNodes()
        {
     
            return $this->_getDb()->fetchAll('
            SELECT node_id, title, node_type_id
            FROM xf_node
            WHERE node_type_id = ?
            ORDER BY node_id
            ', 'Category');
     
        }
    }
    For the listener I'll watch it once this problem is solved :)
     
  16. cclaerhout

    cclaerhout Well-Known Member

    Mais là il ne s'agit de Zend ou tout autre framework, mais juste d'un simple tableau... Tu utilises "$node['title']" alors que la variable $node n'est pas déclarée... tu n'es pas dans la boucle foreach. Ensuite ce code ne veut pas dire grand chose " ('yourBlankValue', $selectedNodesIds)". C'est supposé être une condition (au format court) à l'image de ce qui est fait plus bas.
     
  17. Zephyr

    Zephyr Well-Known Member

    J'ai réussi à afficher le champs, par-contre je ne sait pas si j'ai fait ça comme il le faillais pour que ça fonctionne avec xenforo, et mon 'selected' m'a l'air incorrecte, je ne sait pas à quoi il sert ni même quoi en faire :

    [​IMG]

    PHP:
        public function getNodesOptions($selectedNodesId)
        {
            
    $bool false;
     
            foreach (
    $this->getDbNodes() AS $node)
            {
                if ( 
    $bool == false )
                {
                    
    $nodes[] = array(
                    
    'label' => 'Disabled',
                    
    'value' => 'null',
                    
    'selected' => ('NoChoice' == $selectedNodesId)
                    );
                  
    $bool true;
                }
     
                
    $nodes[] = array(
                
    'label' => $node['title'],
                
    'value' => $node['node_id'],
                
    'selected' => ($node['node_id'] == $selectedNodesId)
                );
            }
     
            return 
    $nodes;
        }
     
  18. Zephyr

    Zephyr Well-Known Member

    It's good it works now, had to integer and not boolean :)

    I'm looking for .js now.
     
  19. cclaerhout

    cclaerhout Well-Known Member

    Use this, it will be cleaner:
    PHP:
        public function getNodesOptions($selectedNodesId)
        {
           
    $nodes[] = array(
                
    'label' => new XenForo_Phrase('yourAddon_admin_optionName'),
                
    'value' => 0,
                
    'selected' => (== $selectedNodesId)
                );
                   
            foreach (
    $this->getDbNodes() AS $node)
            {
                
    $nodes[] = array(
                    
    'label' => $node['title'],
                    
    'value' => $node['node_id'],
                    
    'selected' => ($node['node_id'] == $selectedNodesId)
                    );
            }
     
            return 
    $nodes;
        }
     
        public function 
    getDbNodes()
        {
     
            return 
    $this->_getDb()->fetchAll('
            SELECT node_id, title, node_type_id
            FROM xf_node
            WHERE node_type_id = ?
            ORDER BY node_id
            '
    'Category');
     
        }
    Et un petit cours sur les tableaux (array) pour éviter tout problème à l'avenir ;)
     

Share This Page