*

mwindey

  • *****
  • 474 posts
I am currently working on a new function so that when placing a new listing, the advertiser has the option to also enter a URL at the bottom of the description form without using TinyMCE. For this I have an extra field in t_item_description table with name s_url. which saves the link to execute later on the item page. So far so good. Now when a user does an edit item the url disappears in the form and when sending the s_url in the database returns to NULL instead of the updated url. I provide the custom code in DB, item-post.php, item.php, modelEPS and functions. Any idea how I retrieve the url in item post/edit and replenish the table s_url after editing?

Item-post:
Code: [Select]
<section class="info">
     <h2><?php _e('Listing description and attributes''epsilon'); ?> <i class="show-tip fas fa-question-circle"></i></h2>

     <div class="in">
         <div class="row ttle">
             <label for="title[<?php echo osc_current_user_locale(); ?>]"><?php _e('Title''epsilon'); ?> <span class="req">*</span></label>
             <div class="input-box">
                 <?php ItemForm::title_input('title'osc_current_user_locale(), osc_esc_html(eps_post_item_title())); ?>
             </div>
         </div>

         <div class="row dsc">
             <label for="description[<?php echo osc_current_user_locale(); ?>]"><?php _e('Description''epsilon'); ?> <span class="req">*</span></label>
             <div class="td-wrap d1 input-box">
                 <?php ItemForm::description_textarea('description'osc_current_user_locale(), osc_esc_html(eps_post_item_description())); ?>
             </div>
         </div>

         <?php
         
// Initialize $modelEPS if it is not already initialized
         
if (!isset($modelEPS)) {
             
$modelEPS = new ModelEPS();
         }

         
// Get the URL of the item if it already exists
         
$url_from_database $modelEPS->get_item_description_url(osc_item_id(), osc_current_user_locale());

         
// Check if a URL has been submitted via the form
         
if(isset($_POST['url'])) {
             
$url $_POST['url'];
             
// Update the URL in the database
             
if(!$modelEPS->update_item_description_url(osc_item_id(), $urlosc_current_user_locale())) {
                 
// Handle any errors when updating the URL
                 // This could, for example, display an error message to the user
             
}
         } else {
             
// If no URL has been submitted, use the existing URL
             
$url $url_from_database;
         }
         
?>


         <div class="row url">
             <label for="url"><?php _e('URL''epsilon'); ?></label>
             <div class="input-box">
                 <input type="text" id="url" name="url" value="<?php echo $url?>" placeholder="Start with http:// or https://">
             </div>
         </div>

         <?php osc_run_hook('item_publish_description'); ?>

         <div id="post-hooks" class="hooks-block"><?php if($edit) { ItemForm::plugin_edit_item(); } else { ItemForm::plugin_post_item(); } ?></div>

         <?php osc_run_hook('item_publish_hook'); ?>
     </div>

     <div class="tip">
         <i class="fas fa-times close-tip"></i>
         <p><strong><?php _e('Additional description''epsilon'); ?></strong></p>
         <p><?php _e('Title is used in search so it is recommended to use keywords of your listing in title.''epsilon'); ?></p>
         <p><?php _e('Detail description will also provide customers with all the information they need and reduce need to contact you.''epsilon'); ?></p>
     </div>
</section>

<section class="buttons-block">
     <div class="row captcha"><?php osc_run_hook('invisible_recaptcha'); ?></div>

     <button type="submit" class="btn"><?php _e('Submit''epsilon'); ?></button>
     <?php osc_run_hook('item_publish_buttons'); ?>
     <?php osc_run_hook('fill_item_description_url_hook'); ?>
</section>

<?php osc_run_hook('item_publish_after'); ?>
</form>
</div>
</div>

Item.php part that is used to retrieve on item page:
Code: [Select]
<?php echo osc_item_description(); ?>
</div>
<?php ?>
<!-- Show URL if it exists -->
<?php
$modelEPS 
= new ModelEPS();
$item_id osc_item_id();
$locale_code osc_current_user_locale(); // Assuming you want to use the current user's locale
$url $modelEPS->get_item_description_url($item_id$locale_code);
if (!empty(
$url)) {
     echo 
'<div class="item-url">' __('URL:''epsilon') . ' <a href="' $url '" target="_blank">' $url '</a></div>';
}
?>

</div>
<div class="location">

ModelEPS:
Code: [Select]
// Method to retrieve the URL for an item for a given language
     public function get_item_description_url($item_id, $locale_code) {
         // First check if the item_id and locale_code are valid
         if ($item_id <= 0 || empty($locale_code)) {
             return '';
         }

         // Get the URL of the item description for the specified language
         $result = $this->dao->select('s_extra_url') // Changed from 's_url' to 's_extra_url'
             ->from($this->getTable_item_description())
             ->where('fk_i_item_id', $item_id)
             ->where('fk_c_locale_code', $locale_code)
             ->get();

         // Check if there is a result
         if ($result->numRows() > 0) {
             $row = $result->row();
             return $row['s_extra_url']; // Changed from 's_url' to 's_extra_url'
         } else {
             return '';
         }
     }

     // Method to update the URL of the item description
     public function update_item_description_url($item_id, $url, $locale_code) {
         if ($item_id <= 0 || empty($url) || empty($locale_code)) {
             return false;
         }

         // Update the URL in the database
         $data = array('s_extra_url' => $url); // Changed from 's_url' to 's_extra_url'
         $where = array('fk_i_item_id' => $item_id, 'fk_c_locale_code' => $locale_code);
         return $this->dao->update($this->getTable_item_description(), $data, $where);
     }

Functions.php:

Code: [Select]
// FUNCTION TO ADD URL TO DATABASE
function fill_item_description_url_hook($item) {
     // Initialize $modelEPS if it is not already initialized
     if (!isset($modelEPS)) {
         $modelEPS = new ModelEPS();
     }

     // Check if a URL has been submitted via the form
     if(isset($_POST['url'])) {
         $url = $_POST['url'];
         // Update the URL in the database
         if(!$modelEPS->update_item_description_url($item['fk_i_item_id'], $url, osc_current_user_locale())) {
             // Handle any errors when updating the URL
             // This could, for example, display an error message to the user
         }
     }
}

// Add hook to call the function after an item is posted or edited
osc_add_hook('posted_item', 'fill_item_description_url_hook', 10);
osc_add_hook('edited_item', 'fill_item_description_url_hook', 10);

DB:
Code: [Select]
ALTER TABLE `oc_t_item_description`
ADD COLUMN `s_extra_url` VARCHAR(255) NULL;

Finally add a validation in item-post at the end starting from zip
Code: [Select]
    url: {
      required: function(element) {
        return $(element).val().trim() !== '';
      },
      url: true,
      customURL: {
        depends: function(element) {
          return $(element).val().trim() !== '';
        },
        method: function(value, element) {
          var regex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|ftp:\/\/www\.|ftp:\/\/)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(\/\S*)?$/;
          return this.optional(element) || regex.test(value);
        },
        message: "Please enter a valid URL"
      }
    }
  },
« Last Edit: May 08, 2024, 06:35:53 PM by mwindey »

*

MB Themes

Re: Retrieve Extra field s_url (experimental) in item post item/edit
« Reply #1 on: May 07, 2024, 07:50:44 AM »
I woukd not place it to item description but to item table instead.
Also use field name that will not colide in fiture with osclass fields (ie s_my_url1).
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

*

mwindey

  • *****
  • 474 posts
Re: Retrieve Extra field s_url (experimental) in item post item/edit
« Reply #2 on: May 07, 2024, 02:47:34 PM »
@MB Themes

I still used the item_description table, but added a check for local code now and changed s_url to s_extra_url so that future osclass releases will probably not use the table.... All is tested and ready to copy / paste :-)

*

MB Themes

Re: Retrieve Extra field s_url (experimental) in item post item/edit
« Reply #3 on: May 08, 2024, 05:56:36 PM »
Glad to hear that  ;)
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots