| Total Complexity | 24 |
| Total Lines | 220 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | class SalesForceDefaultContactField extends DataObject |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * contact fields that should be created by default... |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | private static $defaults_records = []; |
||
|
1 ignored issue
–
show
|
|||
| 15 | |||
| 16 | private static $site_wide_fields_to_send = []; |
||
|
1 ignored issue
–
show
|
|||
| 17 | |||
| 18 | private static $site_wide_filter_values = []; |
||
|
1 ignored issue
–
show
|
|||
| 19 | |||
| 20 | /** |
||
| 21 | * Needs to link to a many-many relationship (SalesforceDefaultContactFields) |
||
| 22 | * @param array $array fields to send |
||
| 23 | * |
||
| 24 | * @return CheckboxSetField |
||
| 25 | */ |
||
| 26 | public static function select_field( |
||
| 27 | $fieldName = 'SalesforceDefaultContactFields', |
||
| 28 | $title = 'Default Fields' |
||
| 29 | ) |
||
| 30 | { |
||
| 31 | $htmlArray = []; |
||
| 32 | foreach($array as $field => $value) { |
||
| 33 | $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')'; |
||
| 34 | } |
||
| 35 | |||
| 36 | return CheckboxSetField::create( |
||
| 37 | $fieldName, |
||
| 38 | $title, |
||
| 39 | SalesForceDefaultContactField::get()->map->toArray() |
||
| 40 | ) |
||
| 41 | ->setDescription(' |
||
| 42 | These default fields are always sent to Salesforce, you can |
||
| 43 | <a href="/admin/salesforceadmin">Edit or Add to the List</a> as required. |
||
| 44 | Please change with care. |
||
| 45 | ') |
||
| 46 | ; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * @param array|DataList|null $array fields to send |
||
| 52 | * @param string $title fields to send |
||
| 53 | * |
||
| 54 | * @return LiteralField |
||
| 55 | */ |
||
| 56 | public static function fields_to_send_field( |
||
| 57 | $mixed = null, |
||
| 58 | $title = 'Fields Added' |
||
| 59 | ) |
||
| 60 | { |
||
| 61 | $array = self::get_fields_to_send($mixed); |
||
| 62 | $htmlArray = []; |
||
| 63 | foreach($array as $field => $value) { |
||
| 64 | $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')'; |
||
| 65 | } |
||
| 66 | if(!count($htmlArray) == 0) { |
||
| 67 | $htmlArray[] = 'none'; |
||
| 68 | } |
||
| 69 | return LiteralField::create( |
||
| 70 | 'FieldsToSendToSalesforce', |
||
| 71 | '<h2>'.$title.'</h2><p>- '.implode('</p><p>', $htmlArray).'</p>' |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | /** |
||
| 75 | * |
||
| 76 | * @param array|DataList|null $array fields to send |
||
| 77 | * @param string $title fields to send |
||
| 78 | * |
||
| 79 | * @return LiteralField |
||
| 80 | */ |
||
| 81 | public static function fields_to_filter_field( |
||
| 82 | $mixed = null, |
||
| 83 | $title = 'Filter Fields' |
||
| 84 | ) |
||
| 85 | { |
||
| 86 | $array = self::get_fields_to_send($mixed); |
||
| 87 | $htmlArray = []; |
||
| 88 | foreach($array as $field => $value) { |
||
| 89 | $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')'; |
||
| 90 | } |
||
| 91 | if(!count($htmlArray) == 0) { |
||
| 92 | $htmlArray[] = 'none'; |
||
| 93 | } |
||
| 94 | return LiteralField::create( |
||
| 95 | 'FieldsToFilterForSalesforce', |
||
| 96 | '<h2>'.$title.'</h2><p>- '.implode('</p><p>', $htmlArray).'</p>' |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * |
||
| 102 | * @param array|DataList|null $mixed fields to send |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public static function get_fields_to_send($mixed = null) |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * |
||
| 118 | * @param array|DataList|null $mixed fields to send |
||
| 119 | * |
||
| 120 | * @return array|DataList|null |
||
| 121 | */ |
||
| 122 | public static function get_fields_for_filter($mixed = null) |
||
| 123 | { |
||
| 124 | $array = self::mixed_to_array($mixed); |
||
| 125 | |||
| 126 | return array_merge( |
||
| 127 | Config::inst()->get('SalesForceDefaultContactField', 'site_wide_filter_values'), |
||
| 128 | $array |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | protected static function mixed_to_array($mixed = null) |
||
| 133 | { |
||
| 134 | if($mixed === null) { |
||
| 135 | $array = []; |
||
| 136 | } |
||
| 137 | elseif($mixed instanceof DataList) { |
||
| 138 | $array = []; |
||
| 139 | foreach($mixed as $object) { |
||
| 140 | $array[trim($object->Field)] = $object->BetterValue(); |
||
| 141 | } |
||
| 142 | } elseif(! is_array($array)) { |
||
| 143 | user_error('Variable '.vardump($mixed).'Should be an array'); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $array; |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Singular name for CMS |
||
| 152 | * @var string |
||
| 153 | */ |
||
| 154 | private static $singular_name = 'Default Contact Field'; |
||
|
1 ignored issue
–
show
|
|||
| 155 | |||
| 156 | /** |
||
| 157 | * Plural name for CMS |
||
| 158 | * @var string |
||
| 159 | */ |
||
| 160 | private static $plural_name = 'Default Contact Fields'; |
||
|
1 ignored issue
–
show
|
|||
| 161 | |||
| 162 | /** |
||
| 163 | * |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | private static $db = [ |
||
|
1 ignored issue
–
show
|
|||
| 167 | 'Key' => 'Varchar', |
||
| 168 | 'Value' => 'Varchar' |
||
| 169 | ]; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Defines summary fields commonly used in table columns |
||
| 173 | * as a quick overview of the data for this dataobject |
||
| 174 | * @var array |
||
| 175 | */ |
||
| 176 | private static $summary_fields = [ |
||
|
1 ignored issue
–
show
|
|||
| 177 | 'Key' => 'Field Name', |
||
| 178 | 'Value' => 'Field Value' |
||
| 179 | ]; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getTitle() |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | public function requireDefaultRecords() |
||
| 192 | { |
||
| 193 | foreach($this->Config()->get('default_records') as $key => $value) { |
||
| 194 | $filter = [ |
||
| 195 | 'Key' => $key |
||
| 196 | ]; |
||
| 197 | |||
| 198 | $obj = SalesForceDefaultContactField::get()->filter($filter)->first(); |
||
| 199 | if(! $obj) { |
||
| 200 | $obj->create($filter); |
||
| 201 | $obj->Value = $value; |
||
| 202 | $obj->write(); |
||
| 203 | } |
||
| 204 | |||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public function BetterValue() |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 232 |