Back to PhocacartAttribute class

Method checkRequiredAttributes

public static
checkRequiredAttributes
(mixed $id, mixed $attributes)

Method checkRequiredAttributes - Source code

/*
    public static function getType($id) {

        switch((int)$id) {
            case 4:
                $type = 2;//Multiple Value - handle array
            break;

            case 1:
            case 2:
            case 3:
            default:
                $type = 1;//Single Value - handle string
            break;
        }
        return $type;
    }*/
public static function checkRequiredAttributes($id, $attributes)
{
    // PHOCARTATTRIBUTE ATTRIBUTETYPE
    // Covert all attribute values from strings to array
    if (!empty($attributes)) {
        foreach ($attributes as $k => $v) {
            if (!is_array($v)) {
                $attributes[$k] = array(0 => $v);
            }
        }
    }
    // $attributes - attributes sent per form when adding product to cart
    // $requiredAttributes - all required attributes for selected product
    // Get all required attributes for this product
    // Or required options of not required attributes (specific case for text, textarea, gift, etc. attributes where only one option can be required, not whole attribute)
    $requiredAttributes = PhocacartAttribute::getAllRequiredAttributesByProduct($id);
    $msgA = array();
    $passAll = true;
    $passAttribute = array();
    if (!empty($requiredAttributes)) {
        foreach ($requiredAttributes as $k2 => $v2) {
            if (isset($v2['id']) && $v2['id'] > 0) {
                if (!empty($attributes)) {
                    foreach ($attributes as $k3 => $v3) {
                        if (isset($k3) && (int) $k3 == (int) $v2['id']) {
                            $passAttribute[$k3] = 0;
                            if (!empty($v3)) {
                                foreach ($v3 as $k4 => $v4) {
                                    if (isset($v2['type']) && ($v2['type'] == 7 || $v2['type'] == 8 || $v2['type'] == 9 || $v2['type'] == 10 || $v2['type'] == 11 || $v2['type'] == 12 || $v2['type'] == 20)) {
                                        // -------------------------------------
                                        // ATTRIBUTE TYPE = TEXT, TEXTAREA, GIFT
                                        // -------------------------------------
                                        // 1) FIRST test required options (not required attribute)
                                        // required options in attributes which are not required
                                        // because there can be required whole attribute but only one option
                                        if ($v2['required_type'] == 2 && !empty($v2['options'])) {
                                            // Is current option required - is current option ID included in required option field?
                                            if (in_array($k4, $v2['options'])) {
                                                if (isset($v4['ovalue']) && urldecode($v4['ovalue'] != '')) {
                                                    // Order product - we found value in order of products - OK
                                                    $passAttribute[$k3] = 1;
                                                    //break 2;
                                                } else {
                                                    if (!is_array($v4) && urldecode($v4 != '')) {
                                                        // Order product - we found value in order of products - OK
                                                        $passAttribute[$k3] = 1;
                                                        //break 2;
                                                    } else {
                                                        $passAll = false;
                                                        break 2;
                                                    }
                                                }
                                            } else {
                                                // It is not in required field, set is as OK (can be overriden in loop by other option for this attribute)
                                                $passAttribute[$k3] = 1;
                                            }
                                        } else {
                                            // 2) SECOND test required attribute
                                            // There is reverse testing to select or checkbox
                                            // In select or checkbox we can wait for some of the option will be selected
                                            // but by text all input text fields in one attribute must be required
                                            if (isset($v4['ovalue']) && urldecode($v4['ovalue'] != '')) {
                                                // Order product - we found value in order of products - OK
                                                $passAttribute[$k3] = 1;
                                                //break 2;
                                            } else {
                                                if (!is_array($v4) && urldecode($v4 != '')) {
                                                    // Order product - we found value in order of products - OK
                                                    $passAttribute[$k3] = 1;
                                                    //break 2;
                                                } else {
                                                    $passAll = false;
                                                    break 2;
                                                }
                                            }
                                        }
                                    } else {
                                        // ---------------------------------
                                        // ATTRIBUTE TYPE = CHECKBOX, SELECT
                                        // ---------------------------------
                                        if (isset($v4['oid']) && $v4['oid'] > 0) {
                                            // Order product - we found value in order of products - OK
                                            $passAttribute[$k3] = 1;
                                            break 2;
                                        } else {
                                            if (!is_array($v4) && (int) $v4 > 0) {
                                                // Add to cart - we found value when adding product to cart - OK
                                                $passAttribute[$k3] = 1;
                                                break 2;
                                            }
                                        }
                                    }
                                    // possible break 3;
                                }
                            }
                        }
                    }
                } else {
                    $msgA[] = 'No FORM ATTRIBUTE found';
                }
            } else {
                $msgA[] = 'No ID found of REQUIRED ATTRIBUTE';
            }
            // Summarization of passed values
            $aId = (int) $v2['id'];
            // ID of attribute
            if (isset($passAttribute[$aId]) && $passAttribute[$aId] == 1) {
                // this required attribute is OK
            } else {
                // we didn't found any information - any passed information about this required attribute
                $passAll = false;
            }
        }
    }
    if (!empty($msgA)) {
        //$u = PhocacartUser::getUserInfo();
        //PhocacartLog::add(3, implode(' ', $msgA), $id, 'IP: '. $u['ip'].', User ID: '.$u['id'] . ', User Name: '.$u['username']);
    }
    return $passAll;
}