Advertisement:

Author Topic: Questing regarding hooks  (Read 418 times)

nightfly13

  • Full Member
  • ***
  • Posts: 108
Questing regarding hooks
« on: January 20, 2019, 11:21:36 pm »
Dear all...

With this code in item-post page returns 2 thinks, 1st, the gdpr aceptance terms, and the attributes field:
Code: [Select]
        <!-- PLUGIN HOOKS -->
        <div id="post-hooks">
          <h2><?php _e('Additional details''klass24_pt'); ?></h2>

          <?php ItemForm::plugin_post_item(); ?>
        </div>
      </div>

My question is, is possible to separate them in order to have <b>ItemForm::plugin_gdpr_post_item();</b> and the other <b>ItemForm::plugin_attributes_post_item();</b>, and not the same for both????

if i can separate them i can maniputade each one and change the position on the page....

Best Regards
Nightfly

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Questing regarding hooks
« Reply #1 on: January 21, 2019, 06:26:39 am »
Cria os teus próprios hooks.

Exemplo:

Code: [Select]
nome_da_minha_função(){ conteúdo da função}
Fora das chavetas adicionas o gancho:

Code: [Select]
osc_add_hook('nome_do_meu_novo_gancho', 'nome_da_minha_função');
Depois no ficheiro item-post.php chamas o teu hook onde queres:

Code: [Select]
osc_run_hook('nome_do_meu_novo_gancho');
Nota: podes fazer isso em qualquer plugin existente alterando apenas o nome do hook, em vez do nome ser item_form que é o hook por padrão para mostrar na página de publicação:

Code: [Select]
osc_add_hook('item_form', 'nome_da_função_do_teu_plugin');
Mudas desta forma:

Code: [Select]
osc_add_hook('nome_do_meu_novo_gancho', 'nome_da_função_do_teu_plugin');
No ficheiro item-post.php chamas o teu hook onde queres:

Code: [Select]
osc_run_hook('nome_do_meu_novo_gancho');

nightfly13

  • Full Member
  • ***
  • Posts: 108
Re: Questing regarding hooks
« Reply #2 on: January 21, 2019, 11:12:52 am »
Obrigado… Vou tentar…

Cumprimentos

nightfly13

  • Full Member
  • ***
  • Posts: 108
Re: Questing regarding hooks
« Reply #3 on: January 22, 2019, 12:19:10 am »
Dear all...

On this peace of code if i run_osc_run_hook('item_form'); i have allways the categories id   echo "Category ID === ", $cat_id;... If i run osc_run_hook('myAttributes'); i have allways the categorie ID empty.... How can i solve this and have categories id when i run run osc_run_hook('myAttributes'); ???????

It's the only think im missing to have the attributes on the place i want...



Code: [Select]
// SHOW DATA ON ITEM FORM
function atr_post_form($cat_id = NULL, $item_id = NULL) {
//$cat_id = 190;
//if ($cat_id < 0) {
// return true;
//}

  echo "Category ID === ", $cat_id;

  $attributes = ModelATR::newInstance()->getAttributes(1, $cat_id);

  if(count($attributes) > 0) {
    $html = '<div class="atr-form atr-theme-' . osc_current_web_theme() . '" id="atr-form">';

    foreach($attributes as $a) {
      $required = ($a['b_required'] == 1 ? 'required' : '');
      $name = atr_name($a['locales']);

      $html .= '<div class="control-group atr-type-' . strtolower($a['s_type']) . '" id="atr-' . ($a['s_identifier'] <> '' ? $a['s_identifier'] : 'id' . $a['pk_i_id']) . '">';
      $html .= '<label class="control-label" for="atr' . $a['pk_i_id'] . '">' . ($name <> '' ? $name : __('New attribute', 'attributes')) . '</label>';
      $html .= '<div class="controls">';

      $item_atr = ModelATR::newInstance()->getItemAttributeRaw($item_id, $a['pk_i_id']);
      $item_atr['fk_i_attribute_value_id'] = (isset($item_atr['fk_i_attribute_value_id']) ? $item_atr['fk_i_attribute_value_id'] : '');
      $item_atr['s_value'] = (isset($item_atr['s_value']) ? $item_atr['s_value'] : '');

      if($a['s_type'] == 'SELECT') {
        if(count($a['values']) == 0) {
          $required = '';
        }

        $html .= '<input type="hidden" id="atr_' . $a['pk_i_id'] . '" name="atr_' . $a['pk_i_id'] . '" value="' . $item_atr['fk_i_attribute_value_id'] . '">';

        if($item_atr['fk_i_attribute_value_id'] > 0) {
          // We have selected ID already
          $hierarchy = atr_attribute_value_hierarchy($item_atr['fk_i_attribute_value_id']);
          $data = atr_attribute_value_siblings($item_atr['fk_i_attribute_value_id']);

          if(count($data) > 0) {
            $j = 0;

            foreach($data as $d) {
              $selected_val = $d['selected_id'];
              $siblings = $d['siblings'];

              if(count($siblings) > 0) {
                $html .= '<select data-level="' . ($j + 1) . '" data-atr-id="' . $a['pk_i_id'] . '" data-val-id="' . @$hierarchy[$j-1] . '" ' . ($j == 0 ? $required : '') . ' ' . ($j == 0 ? 'id="select_' . $a['pk_i_id'] . '"' : '') . '>';
                $html .= '<option value="">' . __('Select value ...', 'attributes') . '</option>';

                foreach($siblings as $s) {
                  $html .= '<option value="' . $s['pk_i_id'] . '" ' . ($s['pk_i_id'] == $selected_val ? 'selected="selected"' : '') . '>' . atr_name($s['locales']) . '</option>';
                }

                $html .= '</select>';
              }

              $j++;
            }

            // we have parents, build now children
            $children = ModelATR::newInstance()->getAttributeValuesByParent($item_atr['fk_i_attribute_id'], $item_atr['fk_i_attribute_value_id']);
           
            if(count($children) > 0) {
              $html .= '<select data-level="' . ($j + 1) . '" data-atr-id="' . $a['pk_i_id'] . '" data-val-id="' . $item_atr['fk_i_attribute_value_id'] . '">';
              $html .= '<option value="">' . __('Select value ...', 'attributes') . '</option>';

              foreach($children as $c) {
                $html .= '<option value="' . $c['pk_i_id'] . '">' . atr_name($c['locales']) . '</option>';
              }

              $html .= '</select>';
            }
          }

        } else {
          // Generate first select only
          $html .= '<select data-level="1" data-atr-id="' . $a['pk_i_id'] . '" data-val-id="" ' . $required . ' id="select_' . $a['pk_i_id'] . '">';
          $html .= '<option value="">' . __('Select value ...', 'attributes') . '</option>';
       
          if(count($a['values']) > 0) {
            foreach($a['values'] as $v) {
              $html .= '<option value="' . $v['pk_i_id'] . '">' . atr_name($v['locales']) . '</option>';
            }
          }

          $html .= '</select>';
        }

      } else if($a['s_type'] == 'CHECKBOX' || $a['s_type'] == 'RADIO') {
        if(count($a['values']) > 0) {
          $html .= '<ul class="atr-ul atr-ul-' . strtolower($a['s_type']) . '">';

          foreach($a['values'] as $v) {
            $idc = 'atr_' . $a['pk_i_id'] . '_' . $v['pk_i_id'];
            $item_atr_val = ModelATR::newInstance()->getItemAttributeRaw($item_id, $a['pk_i_id'], $v['pk_i_id']);
            $checked = (@$item_atr_val['s_value'] == 1 ? 'checked' : '');

            $html .= '<li><div class="atr-input-box atr-' . strtolower($a['s_type']) . '"><input type="' . strtolower($a['s_type']) . '" id="' . $idc . '" name="' . $idc . '" ' . $checked . '/> <label for="' . $idc . '">' . atr_name($v['locales']) . '</div></label></li>';
          }
 
          $html .= '</ul>';
        }

      } else if ($a['s_type'] == 'TEXT' || $a['s_type'] == 'PHONE' || $a['s_type'] == 'EMAIL' || $a['s_type'] == 'URL') {
        $input_type = strtolower($a['s_type'] == 'PHONE' ? 'TEL' : $a['s_type']);
        $html .= '<input type="' . $input_type . '" id="atr_' . $a['pk_i_id'] . '" name="atr_' . $a['pk_i_id'] . '" placeholder="' . atr_input_placeholder($a['s_type']) . '" value="' . $item_atr['s_value'] . '" ' . $required . '/>';

      } else if ($a['s_type'] == 'TEXTAREA') {
        $html .= '<textarea id="atr_' . $a['pk_i_id'] . '" name="atr_' . $a['pk_i_id'] . '" ' . $required . ' placeholder="' . atr_input_placeholder($a['s_type']) . '">' . $item_atr['s_value'] . '</textarea>';

      } else if ($a['s_type'] == 'DATE') {
        $html .= '<input type="date" id="atr_' . $a['pk_i_id'] . '" name="atr_' . $a['pk_i_id'] . '" value="' . $item_atr['s_value'] . '" ' . $required . '/>';

      } else if ($a['s_type'] == 'DATERANGE') {
        $range = explode('|', $item_atr['s_value']);

        $html .= '<input type="date" id="atr_' . $a['pk_i_id'] . '_start" name="atr_' . $a['pk_i_id'] . '_start" value="' . @$range[0] . '" ' . $required . '/>';
        $html .= '<span class="atr-date-del">-</span>';
        $html .= '<input type="date" id="atr_' . $a['pk_i_id'] . '_end" name="atr_' . $a['pk_i_id'] . '_end" value="' . @$range[1] . '" ' . $required . '/>';

      }

      $html .= '</div>';
      $html .= '</div>';
    }

    $html .= '</div>';
  }

  echo $html;
}

osc_add_hook('myAttributes', 'atr_post_form');

function atr_edit_form($cat_id, $item_id = NULL) {
  atr_post_form($cat_id, $item_id);
}


osc_add_hook('item_form', 'atr_post_form');
osc_add_hook('item_edit', 'atr_edit_form');


Best Regards
Nightfly

WEBmods

  • Hero Member
  • *****
  • Posts: 872
Re: Questing regarding hooks
« Reply #4 on: January 22, 2019, 12:32:24 am »
Hello,

Hook 'myAttributes' is run when the item post/edit page loads while hook 'item_form' loads by Javascript when a category is selected. You would need to call your hook not with PHP but with JS.

Example:

Code: [Select]
$("#catId").change(function(){
    var cat_id = $(this).val();
    var url = '<?php echo osc_base_url(true); ?>';
    var result = '';

    if(cat_id != '') {
        $.ajax({
            type: "POST",
            url: url,
            data: 'page=ajax&action=runhook&hook=myAttributes&catId=' + cat_id, // Note the "hook=myAttributes".
            dataType: 'html',
            success: function(data){
                $("#myAttributes").html(data); // Selector for the div you want to place attributes in.
            }
        });
    }
});

Regards.

nightfly13

  • Full Member
  • ***
  • Posts: 108
Re: Questing regarding hooks
« Reply #5 on: January 22, 2019, 02:38:28 pm »
Thanks allot for your help...

Sory for the question, in wich file i should apply your lines??? Theme function.php, plugin function php???

And on the end to call the hook myAttributes is enought <?php myAttibutes (); ?> or osc_run_hook ('myAttributes'); ????

Best regards
Nightfly

WEBmods

  • Hero Member
  • *****
  • Posts: 872
Re: Questing regarding hooks
« Reply #6 on: January 25, 2019, 01:51:07 am »
I'm not sure but I think that JS must be put directly in item-post.php.

Regards.