| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * this can be linked to pages / other objects using many_many relationships | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * so that you can send default record values to Salesforce | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class SalesForceDefaultContactField extends DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Needs to link to a many-many relationship (SalesforceDefaultContactFields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param  array $array fields to send | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @return CheckboxSetField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public static function select_field( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $fieldName = 'SalesforceDefaultContactFields', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $title = 'Default Fields' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $htmlArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         foreach($array as $field => $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return CheckboxSetField::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $fieldName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             SalesForceDefaultContactField::get()->map->toArray() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ->setDescription(' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 These default fields are always sent to Salesforce, you can | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 <a href="/admin/salesforceadmin">Edit or Add to the List</a> as required. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 Please change with care. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param  array $array fields to send | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return LiteralField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public static function field_showing_fields_to_send($array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $htmlArray = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         foreach($array as $field => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return LiteralField::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'FieldsToSendToSalesforce', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             '<h2>Fields Added:</h2><p>'.implode('</p><p>', $htmlArray).'</p>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Singular name for CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     private static $singular_name = 'Default Contact Field'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Plural name for CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     private static $plural_name = 'Default Contact Fields'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private static $db = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         'Key' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         'Value' => 'Varchar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Defines summary fields commonly used in table columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * as a quick overview of the data for this dataobject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private static $summary_fields = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         'Key' => 'Field Name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         'Value' => 'Field Value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     public function getTitle() | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return $this->Key . ' - '.$this->Value; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * contact fields that should be created by default... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     private static $defaults_records = []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function requireDefaultRecords() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         foreach($this->Config()->get('default_records') as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $filter = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 'Key' => $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $obj = SalesForceDefaultContactField::get()->filter($filter)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             if(! $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $obj->create($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $obj->Value = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $obj->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     public function BetterValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         if(strtolower($this->Value) === 'true') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if(strtolower($this->Value) === 'false') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         if(floatval($this->Value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             return floatval($this->Value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if(intval($this->Value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             return intval($this->Value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |  |