Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.2 kB
0
Indexable
Never
    /**
     * return string
     * @param $fields, $actual string, $project , $customFields
     * set product values when shortcodes found
     */
    public function replaceShortCodeProductValue($fields, $string, $project, $customFields)
    {
        $commonFields = $this->projectFields($customFields);

        while (strpos($string, '[') !== false) {
            $replacePos = strpos($string, "[");
            $explodeStart = explode("[[[", $string);

            //check if formatting product is followed
            if (count($explodeStart) <= 1) {
                return $string;
            }
            $explodeEnd = explode("]]]", $explodeStart[1]);
            $productString = [];
            //copy shortcodes
            $copyString = $explodeEnd[0];

            //setting values
            for ($i = 0; $i < count($project->products); $i++) {
                $productValue = $project->products[$i];
                $stringVal[$i] = $copyString;

                if ($i === 0 && (substr($stringVal[$i], 0, 1) === "\n")) {
                    $stringVal[$i] = substr($stringVal[$i], 1);
                }

                $product = $this->getProductMailValue($productValue);

                if (!empty($customFields)) {
                    foreach ($customFields as $customField) {
                        $stringVal[$i] = $this->replaceProductStringValue($fields, $stringVal[$i], $product, $commonFields, $customField);
                    }
                } else {
                    $stringVal[$i] = $this->replaceProductStringValue($fields, $stringVal[$i], $product, $commonFields);
                }

                $productString[$i] = $stringVal[$i];
            }

            //paste repeated text product block
            $copyStringValue = $this->cleanJson(join($productString), $fields);
            $string = substr_replace($string, $copyStringValue, $replacePos, 0);

            // remove block code [[[ ]]]
            $blockOpen = strpos($string, "[");
            $blockClose = strpos($string, "]") + 2;
            $string = substr_replace($string, '', $blockOpen, ($blockClose - $blockOpen + 1)); 
        }
        

        // //set last field product value in string
        // $productValue = $project->products[count($project->products) - 1];
        // $last_product = $this->getProductMailValue($productValue);

        // if (!empty($customFields)) {
        //     foreach ($customFields as $customField) {
        //         $string = $this->replaceProductStringValue($fields, $string, $last_product, $commonFields, $customField);
        //     }
        // } else {
        //     $string = $this->replaceProductStringValue($fields, $string, $last_product, $commonFields);
        // }

        //remove brackets
        // if (count($project->products) === 1) {
        //     $string = str_replace("[[["."\n", "", $string);
        // }
        // $string = str_replace("]]]"."\n", "", $string);
        // $string = str_replace("[", "", $string);
        // $string = str_replace("]", "", $string);

        return $string;
    }