Landing Page which contains product section

A landing page that applies certain rules for displaying the items and predefined set of fields (Custom Record) which display on the default landing page template. Products will be shown based on conditions, if one checkbox is checked it will display items on the landing page, a grouping of items is done based on the primary category of the item, sample custom record is shown below and landing page also

Custom Record

Sample Landing Page



Router

define(
    'CD.ps_landingPage.pslanding.Router', [
        'CD.ps_landingPage.pslandingPage.View', 'CD.ps_landingPage.pslandingPage.Model', 'Profile.Model'
    ],
    function(
        CDps_landingPagepslandingPageView, CDps_landingPagepslandingPageModel, ProfileModel
    ) {
        'use strict';
        return Backbone.Router.extend({
            routes: {
                'landingpage/:id': 'productlanding'
            }
            ,
            initialize: function(application) {
                this.application = application;
            },
            productlanding: function(id) {
                var application = this.application;
                var listid = id
                var model = new CDps_landingPagepslandingPageModel();


                var url_options = _.parseUrlOptions(Backbone.history.fragment);

                var pagno = url_options.page - 1 || 0
                var itemsperpage = 45
                var itemfrm = 0 + (itemsperpage * pagno)
                var itemto = itemsperpage + (itemsperpage * pagno)

                model.fetch({ data: { listid: listid, itemfrm: itemfrm, itemto: itemto } }).done(function(result) {
                    var url_base = window.location.origin;
                    var profileModel = ProfileModel.getInstance();
  
                    var view = new CDps_landingPagepslandingPageView({ application: application, result: result })
                    view.showContent();
                });

            }
        });
    });

View

define('CD.ps_landingPage.pslandingPage.View', [
'cd_ps_landingpage_pslandingpage.tpl', 'Backbone', 'Profile.Model', 'GlobalViews.Pagination.View', 'SC.Configuration', 'Backbone.CollectionView', 'CD.ps_landingPage.row', 'cd_ps_landingpage_row.tpl'
], function(
cd_ps_landingpage_pslandingpage_tpl, Backbone, ProfileModel, GlobalViewsPaginationView, Configuration, BackboneCollectionView, CDps_landingPagerow, cd_ps_landingpage_row_tpl
) {
'use strict';
// @class CD.ps_landingPage.pslandingPage.View @extends Backbone.View
return Backbone.View.extend({
    template: cd_ps_landingpage_pslandingpage_tpl
        ,
    title: _('Valley Vet Supply').translate(),
    initialize: function(options) {
            console.log('options',options)
            this.application = options.application
            this.result = options.result[0]
            var url_options = _.parseUrlOptions(Backbone.history.fragment);
            var self = this
            var needsgrouping = this.result.group[0].contains == "" ? false : true
            var quoteper = Number(options.result[0].quotestar) * 20
            this.result.quotestar = quoteper;
            var showdropship = this.result.showdropship == 'T' ? true : false
            var manufilter = this.result.manuft != '' ? true : false
            var groupfilter = this.result.groupname != '' ? true : false
            var items = this.result.items
            var itemdisplay = []
            this.totalproducts = this.result.productlist.length

            const url1 = "//www.shopperapproved.com/widgets/group2.0/20842.js";
            $.getScript(url1)
                .done(function(script, textStatus) {})
                .fail(function(jqxhr, settings, exception) {
                    $("div.log").text("Triggered ajaxError handler.");
                });

            itemdisplay = this.result.productlist
            var itemarry = []
            var grouped_list
            var groupinglist = itemdisplay
            var grpfiltlist = []
            var grpfilteredlist = []
            if (needsgrouping) {
                _.each(this.result.group, function(line) {
                    grouped_list = _.groupBy(groupinglist, function(num) {
                        return num.primarycat == line.contains
                    });
                    groupinglist = _.filter(groupinglist, function(list) {
                        return list.primarycat != line.contains;
                    });
                    if (grouped_list.true) {
                        itemarry.push({ "item": grouped_list.true, "groupname": line.subtitle })
                    }
                })
                itemarry.push({ "item": grouped_list.false, "groupname": "", "name": "products" })
            } else {
                itemarry.push({ "item": groupinglist, "groupname": "", "name": "products" })
            }
            this.itemarry = itemarry
            this.result.itemdisplay = itemarry
        }

        //@method getContext @return CD.ps_landingPage.pslandingPage.View.Context
        ,
    childViews: {
        'GlobalViews.Pagination': function() {
            var self = this
            return new GlobalViewsPaginationView(_.extend({
                totalPages: (self.result.totalproducts / 45)
            }, Configuration.defaultPaginationSettings));
        },
        'proditems.Collection': function() {
            return new BackboneCollectionView({
                collection: this.newprod,
                childView: CDps_landingPagerow,
                cellTemplate: cd_ps_landingpage_row_tpl,
                viewsPerRow: 3,
                childViewOptions: {
                    application: this.application
                }
            });
        }
    },
    getContext: function getContext() {
        var baseUrl = SC.ENVIRONMENT.baseUrl;
        baseUrl = baseUrl.split('.com')[0] + '.com'
        var showquickink = this.result.showquicklink == 'T' ? true : false
        var showarticle = this.result.article[0].article == "" ? false : true
        var showquote = this.result.quote == "" ? false : true
        var showbanner = this.result.pagebanner == "" ? false : true
        var showitems = this.result.productlist ? true : false
        return {
            message: this.message,
            page: this.result,
            baseUrl: baseUrl,
            showquickink: showquickink,
            showarticle: showarticle,
            showquote: showquote,
            showbanner: showbanner,
            showitems: showitems
        };
    }
});
});

SS Model

// CD.ps_landingPage.pslandingPage.js
// Load all your starter dependencies in backend for your extension here
// ----------------

define('CD.ps_landingPage.pslandingPage', [
    'CD.ps_landingPage.pslandingPage.ServiceController', 'SC.Model', 'Utils', 'underscore'
], function(
    pslandingPageServiceController, SCModel, Utils, _
) {
    'use strict';
    return SCModel.extend({
        getdetail: function(details) {
            try {
                var list_id = JSON.stringify(details.listid);
                var parsedid = JSON.parse(list_id);
                var itemfrm = details.itemfrm;
                var itemto = details.itemto;
                var pageSearch = nlapiSearchRecord("customrecord_product_section_page", null,
                    [
                        ["custrecord_ps_url_fragment", "is", parsedid]
                    ],
                    [
                        new nlobjSearchColumn("custrecord_ps_page_header"),
                        new nlobjSearchColumn("custrecord_ps_page_banner"),
                        new nlobjSearchColumn("custrecord_ps_page_banner_url"),
                        new nlobjSearchColumn("custrecord_ps_drop_ship"),
                        new nlobjSearchColumn("custrecord_ps_item_ids"),
                        new nlobjSearchColumn("custrecord_ps_manufact_name"),
                        new nlobjSearchColumn("custrecord_ps_quote"),
                        new nlobjSearchColumn("custrecord_quote_star_review"),
                        new nlobjSearchColumn("custrecord_ps_quote_by"),
                        new nlobjSearchColumn("custrecord_ps_page_subtitle"),
                        new nlobjSearchColumn("custrecord_ps_show_quicklink"),
                        new nlobjSearchColumn("custrecord_ps_swatches"),
                        new nlobjSearchColumn("custrecord_ps_url_fragment"),
                        new nlobjSearchColumn("custrecord_ps_group_name")
                    ]
                );
                var itemids, manuids, primids
                if (pageSearch) {
                    for (var i = 0; i < pageSearch.length; i++) {
                        var result = pageSearch[i];
                        var itemlist = result.getValue("custrecord_ps_item_ids")
                        itemids = itemlist.length > 0 ? itemlist.split(",") : [];
                        var manulist = result.getValue("custrecord_ps_manufact_name")
                        manuids = manulist.length > 0 ? manulist.split(",") : [];
                        var primlist = result.getValue("custrecord_ps_group_name") != '' ? result.getValue("custrecord_ps_group_name") : []
                        console.log("sample", primlist)
                        primids = primlist.length > 0 ? primlist.split(",") : [];
                        var showchild = result.getValue("custrecord_ps_swatches")
                        var dropship = result.getValue('custrecord_ps_drop_ship')
                        var showdropship = dropship == 'T' ? true : false
                    }
                }

                if (itemids.length > 0 || manuids.length > 0 || primids.length > 0) {
                    console.log('insideif')
                    var filter = [
                        ["isonline", "is", "T"], "AND"
                    ]
                    var matfilter = [
                        ["matrixchild", "is", "T"], "AND", ["isonline", "is", "T"]
                    ]
                    if (itemids.length > 0) {
                        filter.push(["internalid", "anyof", itemids])
                    }
                    if (manuids.length > 0) {
                        if (itemids.length > 0) {
                            filter.push("OR", ["custitemmanuf_name", "anyof", manuids])
                            matfilter.push("AND", ["custitemmanuf_name", "anyof", manuids])
                        } else {
                            filter.push(["custitemmanuf_name", "anyof", manuids])
                            matfilter.push(["custitemmanuf_name", "anyof", manuids])
                        }
                    }
                    if (primids.length > 0) {
                        if (manuids.length > 0 || itemids.length > 0) {

                            filter.push("OR", [
                                ["commercecategoryisprimary", "is", "T"], "AND", ["commercecategoryid", "anyof", primids]
                            ])
                            matfilter.push("OR", [
                                ["parent.commercecategoryisprimary", "is", "T"], "AND", ["parent.commercecategoryid", "anyof", primids]
                            ])
                        } else {
                            filter.push([
                                ["commercecategoryisprimary", "is", "T"], "AND", ["commercecategoryid", "anyof", primids]
                            ])
                            matfilter.push([
                                ["parent.commercecategoryisprimary", "is", "T"], "AND", ["parent.commercecategoryid", "anyof", primids]
                            ])
                        }

                    }
                    nlapiLogExecution('DEBUG', 'filterss', JSON.stringify(filter));
                    var itemSearch = nlapiSearchRecord("item", null,
                        filter,
                        [
                            new nlobjSearchColumn("custitem_vv_guid", null, "GROUP"),
                            new nlobjSearchColumn("internalid", null, "GROUP").setSort(false),
                            new nlobjSearchColumn("displayname", null, "GROUP"),
                            new nlobjSearchColumn("salesdescription", null, "GROUP"),
                            new nlobjSearchColumn("custitemcat_itemidnumber", null, "GROUP"),
                            new nlobjSearchColumn("custitemmanuf_name", null, "GROUP"),
                            new nlobjSearchColumn("urlcomponent", null, "GROUP"),
                            new nlobjSearchColumn("onlinecustomerprice", null, "GROUP"),
                            new nlobjSearchColumn("formulatext", null, "GROUP").setFormula("case when ({commercecategoryisprimary} = 'T') then {commercecategoryname} else 'None' end "),
                            new nlobjSearchColumn("commercecategoryisprimary", null, "MAX"),
                            new nlobjSearchColumn("isdropshipitem", null, "GROUP"),
                            new nlobjSearchColumn("type", null, "GROUP"),
                        ]
                    );
                    var produtitemlist = []
                    if (itemSearch) {
                        for (var i = 0; i < itemSearch.length; i++) {
                            var result = itemSearch[i];
                            produtitemlist.push({
                                id: result.getValue("internalid", null, "GROUP"),
                                subtitle: result.getValue("displayname", null, "GROUP"),
                                guid: result.getValue("custitem_vv_guid", null, "GROUP"),
                                manufact: result.getText("custitemmanuf_name", null, "GROUP"),
                                price: result.getValue("onlinecustomerprice", null, "GROUP"),
                                url: result.getValue("urlcomponent", null, "GROUP"),
                                primarycat: result.getValue("formulatext", null, "GROUP"),
                                isdropshipitem: result.getValue("isdropshipitem", null, "GROUP") == 'T' ? true : false
                            });
                        }
                    }

                    if (showchild == "T" && (manuids.length > 0 || primids.length > 0)) {
                        var itemchildSearch = nlapiSearchRecord("item", null,
                            matfilter,
                            [
                                new nlobjSearchColumn("custitem_vv_guid", null, "GROUP"),
                                new nlobjSearchColumn("internalid", null, "GROUP").setSort(false),
                                new nlobjSearchColumn("displayname", null, "GROUP"),
                                new nlobjSearchColumn("salesdescription", null, "GROUP"),
                                new nlobjSearchColumn("custitemcat_itemidnumber", null, "GROUP"),
                                new nlobjSearchColumn("custitemmanuf_name", null, "GROUP"),
                                new nlobjSearchColumn("urlcomponent", "parent", "GROUP"),
                                new nlobjSearchColumn("onlinecustomerprice", null, "GROUP"),
                                new nlobjSearchColumn("formulatext", null, "GROUP").setFormula("case when ({parent.commercecategoryisprimary} = 'T') then {parent.commercecategoryname} else 'None' end "),
                                new nlobjSearchColumn("commercecategoryisprimary", "parent", "MAX"),
                                new nlobjSearchColumn("isdropshipitem", null, "GROUP"),
                                new nlobjSearchColumn("internalid", "parent", "GROUP"),
                            ]
                        );
                        if (itemchildSearch) {
                            for (var i = 0; i < itemchildSearch.length; i++) {
                                var result = itemchildSearch[i];
                                produtitemlist.push({
                                    id: result.getValue("internalid", null, "GROUP"),
                                    subtitle: result.getValue("displayname", null, "GROUP"),
                                    guid: result.getValue("custitem_vv_guid", null, "GROUP"),
                                    manufact: result.getText("custitemmanuf_name", null, "GROUP"),
                                    price: result.getValue("onlinecustomerprice", null, "GROUP"),
                                    url: result.getValue("urlcomponent", "parent", "GROUP"),
                                    primarycat: result.getValue("formulatext", "parent", "GROUP"),
                                    isdropshipitem: result.getValue("isdropshipitem", null, "GROUP") == 'T' ? true : false,
                                    parentid: result.getValue("internalid", 'parent', "GROUP"),
                                });
                            }
                        }
                    }
                }

                var uniqueArray = this.removeDuplicates(produtitemlist, "id");

                produtitemlist = uniqueArray
                var self = this
                if (!showdropship) {
                    produtitemlist = _.filter(produtitemlist, function(list) {
                        return list.isdropshipitem != true;
                    });
                }
                var totalproducts = produtitemlist.length
                produtitemlist = produtitemlist.slice(itemfrm, itemto)
                // console.log('newprod',this.newprod)
                _.each(produtitemlist, function(line) {
                    nlapiLogExecution('DEBUG', 'url', JSON.stringify(line));
                    var lineid = line.id
                    if(line.parentid){
                        lineid=line.parentid
                    }
                    line.imgurl = self.getItemImageUrl(lineid)
                })

                var article = nlapiSearchRecord("customrecord_product_section_page", null,
                    [
                        ["custrecord_ps_url_fragment", "is", parsedid]
                    ],
                    [new nlobjSearchColumn("custrecord_ps_article_name", "CUSTRECORD_PS_ARTICLE_LINK", null),
                        new nlobjSearchColumn("custrecord_ps_article_url", "CUSTRECORD_PS_ARTICLE_LINK", null)
                    ]
                );
                var articlelist = []
                if (article) {
                    for (var i = 0; i < article.length; i++) {
                        var result = article[i];
                        articlelist.push({
                            article: result.getValue("custrecord_ps_article_name", "CUSTRECORD_PS_ARTICLE_LINK", null),
                            url: result.getValue("custrecord_ps_article_url", "CUSTRECORD_PS_ARTICLE_LINK", null),
                        });
                    }
                }
                var group = nlapiSearchRecord("customrecord_product_section_page", null,
                    [
                        ["custrecord_ps_url_fragment", "is", parsedid]
                    ],
                    [new nlobjSearchColumn("custrecord_ps_grouping_subtitle", "CUSTRECORD_PS_GROUPING_LINK", null),
                        new nlobjSearchColumn("custrecord_ps_grouping_contains", "CUSTRECORD_PS_GROUPING_LINK", null)
                    ]
                );
                var groupinglist = []
                if (group) {
                    for (var i = 0; i < group.length; i++) {
                        var result = group[i];
                        groupinglist.push({
                            subtitle: result.getValue("custrecord_ps_grouping_subtitle", "CUSTRECORD_PS_GROUPING_LINK", null),
                            contains: result.getText("custrecord_ps_grouping_contains", "CUSTRECORD_PS_GROUPING_LINK", null),
                        });
                    }
                }
                var pageSearchdetails = []
                if (pageSearch) {
                    for (var i = 0; i < pageSearch.length; i++) {
                        var result = pageSearch[i];
                        pageSearchdetails.push({
                            pageheader: result.getValue("custrecord_ps_page_header"),
                            pagebanner: result.getText("custrecord_ps_page_banner"),
                            bannerurl: result.getValue("custrecord_ps_page_banner_url"),
                            showdropship: result.getValue("custrecord_ps_drop_ship"),
                            items: result.getValue("custrecord_ps_item_ids"),
                            manuft: result.getText("custrecord_ps_manufact_name"),
                            quote: result.getValue("custrecord_ps_quote"),
                            quotestar: result.getValue("custrecord_quote_star_review"),
                            quoteby: result.getValue("custrecord_ps_quote_by"),
                            subtitle: result.getValue("custrecord_ps_page_subtitle"),
                            showquicklink: result.getValue("custrecord_ps_show_quicklink"),
                            showchild: result.getValue("custrecord_ps_swatches"),
                            url: result.getValue("custrecord_ps_url_fragment"),
                            groupname: result.getValue("custrecord_ps_group_name"),
                            article: articlelist || null,
                            group: groupinglist || null,
                            productlist: produtitemlist || null,
                            totalproducts: totalproducts,
                            uniqueArray: uniqueArray
                        });
                    }
                }
                nlapiLogExecution('DEBUG', 'pageSearchdetails', pageSearchdetails);
                return pageSearchdetails
            } catch (e) {
                console.log(e);
            }

        },
        removeDuplicates: function(originalArray, prop) {
            var newArray = [];
            var grouped = _.groupBy(originalArray, function(num) { return num.id; })
            var filteredlist = []
            var newlist = _.each(grouped, function(num) {
                if (num.length > 1) {
                    _.each(num, function(line) {
                        if (line.primarycat != "None") {
                            filteredlist.push(line)
                        }
                    })
                } else {
                    filteredlist.push(num[0])
                }
            })
            newArray = filteredlist
            var newArrayfilterd = [];
            var lookupfilterd = {};
            for (var i in newArray) {
                if (newArray[i] != null) {
                    lookupfilterd[newArray[i].id] = newArray[i];
                }
            }
            for (i in lookupfilterd) {
                if (lookupfilterd[i] != null) {
                    newArrayfilterd.push(lookupfilterd[i]);
                }
            }
            return newArrayfilterd;
        },
        getItemImageUrl: function(item) {
            var type = nlapiLookupField('item', item, 'recordType')
            var itemrecord = nlapiLoadRecord(type, item)
            var images = [];
            var imageIds = [];
            var url;
            var totalLines = itemrecord.getLineItemCount('itemimages');
            for (var i = 0; i <= totalLines; i++) {
                var imageName = itemrecord.getLineItemValue('itemimages', 'name', i)
                images.push(imageName);
                imageIds[imageName] = itemrecord.getLineItemValue('itemimages', 'nkey', i)
            }
            images = _.filter(images, function(num) { return num != null; });
            if (images.length > 0) {

                var fileObj = nlapiLoadFile(imageIds[images[0]])

                url = fileObj.getURL();

                url = 'https://system.netsuite.com' + url;
            }

            return url;
        }

    });

});

Service Controller

define("CD.ps_landingPage.pslandingPage.ServiceController", ["ServiceController", "CD.ps_landingPage.pslandingPage"], function(
    ServiceController, CDps_landingPagepslandingPage
) {
    "use strict";

    return ServiceController.extend({
        name: "CD.ps_landingPage.pslandingPage.ServiceController",

        // The values in this object are the validation needed for the current service.
        options: {
            common: {}
        },

        get: function get() {
            try {
                var listid = this.request.getParameter('listid');
                var itemfrm = this.request.getParameter('itemfrm');
                var itemto = this.request.getParameter('itemto');
                var result
                if (listid) {
                    result = CDps_landingPagepslandingPage.getdetail({ listid: listid, itemfrm: itemfrm, itemto: itemto })
                }
                return JSON.stringify(result)
            } catch (e) {
                console.warn('Service.ss::' + e.name, e);
                this.sendError(e);
            }
        },

        post: function post() {
            // not implemented
        },

        put: function put() {
            // not implemented
        },

        delete: function() {
            // not implemented
        }
    });
});

Leave a comment

Your email address will not be published. Required fields are marked *