• Add price to model

      As of version 1.6.x, the ad price has been added to the classified ad template: attribute "price type double.

      If you had already overloaded the classified ad type to add the "price" :

      1. Delete the "price" declaration in your overload
      2. If your "price" type was a string instead of double, run the following script to migrate the "price" attribute from string to double for your existing ads. Set handle to true to make script effective.
      let handle = false;
      
      let success = 0;
      let failure = 0;
      
      let logger = Ametys.getLogger("org.ametys.plugins.classifiedads.migrate.price", { "filename": "migration", "level": "info" });
      
      Repository.query("//element(*, ametys:content)[@ametys-internal:contentType = 'org.ametys.plugins.classified.ads.Content.ads']").forEach(
          content =>
          {
              if (handle)
              {
                  Content.migrate(
                      content,
                      [_migratePrice],
                      false /* old versions are still compatible */,
                      null /* no tag */,
                      false /* not verbose */,
                      true /* Synchronize with live workspace */
                  );
              }
              else
              {
                  _migratePrice(content);
              }
          }
      );
      
      logger.info(`${success} ads have been migrated`); 
      if (failure > 0)
      {
          logger.warn(`Migration of ${failure} ads failed`); 
      }
      
      function _migratePrice(content)
      {
          if (content.getNode().hasProperty("ametys:price"))
          {
              if (content.getNode().getProperty("ametys:price").getType() == 1)
              {
                  let val = content.getNode().getProperty("ametys:price").getString();
                  let asDouble = parseFloat(val.replaceAll(",", "."));
                  if (Number.isNaN(asDouble))
                  {
                      logger.warn(`Price "${val}" can not be parse to double for content ${content.getTitle()} (${content.getId()})`);
                      failure++;
                  }
                  else
                  {
                      logger.debug(`Price "${val}" migrated to "${asDouble}" on content ${content.getTitle()} (${content.getId()})`);
                      if (handle)
                      {
                          content.getNode().getProperty("ametys:price").remove();
                          content.setValue("price", asDouble);
                      }
                      success++;
                  }
              }
          }
      }
Back to top

Classified Ads