Question

Ok, trying to process a script, both PHP and JavaScript, where I am moving a particular content type NODE from one reference to another. This is the structure:

I have a PROJECT
Inside each PROJECT are PAGES
Inside each PAGE are CALLOUTS
and Inside each CALLOUT are PRODUCTS.

What I want to do is take a PRODUCT from one CALLOUT to another CALLOUT. I am able to merge these, but now what I want to do is delete the first instance. An example:

I have PRODUCT AAG-794200 that is on PAGE 6 CALLOUT A. I am merging that PRODUCT with PAGE 6 CALLOUT B.

I can get the product to merge, but now I need to remove it from CALLOUT A. Here is my code:

$merge = explode(',', $merge); //Merge SKUs
$mpages = explode(',', $mpages); //Merge Pages
$mcallouts = explode(',', $mcallouts); //Merge Callouts
$mcallout_nid = explode(',', $mcallout_nid);  //Merge Current callout

$length = count($merge);
$e = 0;
while ($e < $length) {
    //Where is the SKU going to?
    $to_callout_letter = strtoupper($mcallouts[$e]);
    $to_page_num = $mpages[$e];
    $sku = $merge[$e];
    $from_callout = $mcallout_nid[$e];

    //Where is the SKU coming from?     
    $other_callout = node_load($from_callout);

    //Need page ID of current callout for project purposes
    $page_nid = $other_callout->field_page[0]['nid'];
    $page = node_load($page_nid);
    //Need the project NID
    $project_nid = $page->field_project[0]['nid'];

    //We need to get the NID of the page we are going to
    $page_nid = db_query('SELECT * FROM content_type_page WHERE field_page_order_value = "%d" and field_project_nid = "%d" ORDER BY vid DESC LIMIT 1', $to_page_num, $project_nid);

    $page_nid_res = db_fetch_array($page_nid);
    $to_page_nid = $page_nid_res['nid'];

    //We need to get the NID of the callout here
    $co_nid = db_query('SELECT * FROM content_type_callout WHERE field_identifier_value = "%s" and field_page_nid = "%d"', $to_callout_letter, $to_page_nid);
    $co_nid_res = db_fetch_array($co_nid);
    $to_callout_letter_nid = $co_nid_res['nid'];


    //Load the present callout the SKU resides on
    $f_callout = node_load($from_callout);

    $callout = node_load($to_callout_letter_nid);
    $long = count($f_callout->field_skus);
    $deletecallout = array();
    foreach($f_callout->field_skus as $skus) {
        $s = 0;
        while ($s < $long) {
            if($skus['nid'] == $sku) {
                $callout->field_skus[] = $skus;
                $s++;
            }
            else {
                $deletecallout[] = $skus;
                $s++;
            }
        }
    }


    foreach($other_callout->field_images as $old_image) {
        $callout->field_images[] = $old_image;
    }
    foreach($other_callout->field_line_art as $old_image) {
        $callout->field_line_art[] = $old_image;
    }
    foreach($other_callout->field_swatches as $old_image) {
        $callout->field_swatches[] = $old_image;
    }

    $callout->field_copy_text[0]['value'] .= $other_callout->field_copy_text[0]['value'];
    $callout->field_notes[0]['value'] .= $other_callout->field_notes[0]['value'];
    $callout->field_image_notes[0]['value'] .= $other_callout->field_image_notes[0]['value'];
    $callout->field_status[0]['value'] = 'In Process';
    node_save($callout);

This causes the PRODUCTS to MERGE, but not delete the original.

Thanks for any help. I know it's something simple, and it will be a palm-to-face moment.

Was it helpful?

Solution

I was actually able to solve this myself. @Chris - The brace ended after node_save(callout); I must have missed that when I copied and pasted. However, here is the code I ended up using:

$merge = explode(',', $merge); //Merge SKUs
$mpages = explode(',', $mpages); //Merge Pages
$mcallouts = explode(',', $mcallouts); //Merge Callouts
$mcallout_nid = explode(',', $mcallout_nid);  //Merge Current callout
if($merge[0] !== '0') {
    //Store NIDs of Old Callouts to the proper SKU
    $oc_sku = array();
    $oc_sku_e = count($merge);
    $oc_sku_ee = 0;
    while ($oc_sku_ee < $oc_sku_e) {
        $curr_sku = $merge[$oc_sku_ee];
        $curr_oldco = $mcallout_nid[$oc_sku_ee];
        $oc_sku[$curr_sku] = $curr_oldco;
        $oc_sku_ee++;
    }

    //Convert page numbers to page_nids
    $pc = count($mpages); //How many pages are we getting
    $pc_e = 0;
    while($pc_e < $pc) {
        $nid = $mpages[$pc_e];
        $sql = db_query('SELECT * FROM content_type_page WHERE field_page_order_value = "%d" AND field_project_nid = "%d" ORDER BY vid DESC LIMIT 1', $nid, $project_nid);
        $res = db_fetch_array($sql);
        if($res) {
            $npage_arr[] = $res['nid'];
        } else {  //If there is no page, we need to create it here.
            $node = new StdClass();
            $node->type = 'page';
            $node->title = 'Page ' . $nid . ' of ' . $project->title;
            $node->field_project[0]['nid'] = $project_nid;
            $node->field_page_order[0]['value'] = $nid;
            $node = node_submit($node);
            node_save($node);
            $npage_arr[] = $node->nid;
        }
    $pc_e++;
    }


    // Convert callout letters to callout_nids
    $coc = count($mcallouts);
    $coc_e = 0;
    while($coc_e < $coc) {
        $cnid = strtoupper($mcallouts[$coc_e]);
        $pnid = $npage_arr[$coc_e];
        $page_node = node_load($pnid);
        $sql = db_query('SELECT * FROM content_type_callout WHERE field_identifier_value = "%s" AND field_page_nid = "%d" ORDER BY vid DESC LIMIT 1', $cnid, $pnid);
        $res = db_fetch_array($sql);
        if($res) {
            $cpage_arr[] = $res['nid'];
        } else { //If there is no callout that exists, we need to make it here.
            $callout_node = new stdClass();
            $callout_node->type = 'callout';
            $callout_node->field_page[0]['nid'] = $pnid;
            $callout_node->field_identifier[0]['value'] = $cnid;
            $callout_node->field_sequence[0]['value'] = 0;
            $callout_node->title = "Callout ".$callout." on page ".$page_node->field_page_order[0]['value'];
            $callout_node->field_project[0]['nid'] = $project->nid;
            $callout_node->field_wholesaler[0]['value'] = $project->field_wholesaler[0]['value'];
            $callout_node->field_skus = array();

            $callout_node->status = 1;
            $callout_node->uid = 1;             
            $callout_node->revision = true;
            $callout_node = node_submit($callout_node);
            node_save($callout_node);
            $cpage_arr[] = $callout_node->nid;
        }
    $coc_e++;
    }

    //Now we need to assign the skus to the appropriate callout for processing
    $coc2 = count($cpage_arr);
    $coc_e2 = 0;
    while($coc_e2 < $coc2) {
        $co = $cpage_arr[$coc_e2];
        if($co !== '0') {
            $sku = $merge[$coc_e2];
            $m_arr[$co][] = $sku;
        }
    $coc_e2++;
    }

    //we need a way to centrally store all NID's of SKUs to the callouts they belong to
    $oc_arr = array();
    $oc = count($mcallout_nid);
    $oc_e = 0;
    while($oc_e < $oc) {
        $f_callout = $mcallout_nid[$oc_e];
        $former_callout = node_load($f_callout);
        foreach($former_callout->field_skus as $key=>$skus) {
            $oc_arr[] = $skus;
        }
        $oc_e++;
    }

    //Now we are processing the Pages/Callouts/SKUs to save
    $pc_e2 = 0;
    foreach($m_arr as $key=>$values) {
        $callout = node_load($key);
        foreach($values as $value) {
            $oc = count($oc_arr);
            $oc_e = 0;
            while($oc_e < $oc) {
                $skus = $oc_arr[$oc_e];
                if($value == $skus['nid']) {
                    $callout->field_skus[] = $skus;
                    //$nid = $oc_sku[$value];
                    $old_callout_info[] = $oc_sku[$value];
                    $oc_e = $oc;
                }
                else {
                    $oc_e++;
                }
            }
        }

        foreach($old_callout_info as $nid) {
/*          $nid = $oc_sku[$value]; */
            $former_callout = node_load($nid);
            foreach($former_callout->field_images as $old_image) {
            $callout->field_images[] = $old_image;
            }
            foreach($former_callout->field_line_art as $old_image) {
                $callout->field_line_art[] = $old_image;
            }
            foreach($former_callout->field_swatches as $old_image) {
                $callout->field_swatches[] = $old_image;
            }
            $callout->field_copy_text[0]['value'] .= $former_callout->field_copy_text[0]['value'];
        }
        $callout->field_notes[0]['value'] .= $former_callout->field_notes[0]['value'];
        $callout->field_image_notes[0]['value'] .= $former_callout->field_image_notes[0]['value'];
        $callout->field_logos = $former_callout->field_logos;
        $callout->field_affiliations = $former_callout->field_affiliations;
        $callout->field_graphics = $former_callout->field_graphics;
        $callout->revision = 1;



        $callout->field_status[0]['value'] = 'inprocess';
        node_save($callout);
        $pc_e2++;
    }
    }

I realize this can probably be simplified in a way, but as for now, this works perfectly considering what I'm trying to do. No complaints from the client so far. Thanks for taking a look Drupal Community.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top