| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\QueuedJobs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Core\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\ORM\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Monolog\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * A set of utility functions used by the queued jobs module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license http://silverstripe.org/bsd-license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author Marcus Nyeholt <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class QJUtils | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Quote up a filter of the form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * array ("ParentID =" => 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param array $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param string $join | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function dbQuote($filter = array(), $join = " AND ") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $quoteChar = defined(DB::class . '::USE_ANSI_SQL') && DB::USE_ANSI_SQL  ? '"' : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $string = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $sep = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         foreach ($filter as $field => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             // first break the field up into its two components | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $operator = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             if (is_string($field)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 list($field, $operator) = explode(' ', trim($field)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $value = $this->recursiveQuote($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             if (strpos($field, '.')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 list($tb, $fl) = explode('.', $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 $string .= $sep . $quoteChar . $tb . $quoteChar . '.' . $quoteChar . $fl . $quoteChar . " $operator " . $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 if (is_numeric($field)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     $string .= $sep . $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     $string .= $sep . $quoteChar . $field . $quoteChar . " $operator " . $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $sep = $join; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param mixed $val | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected function recursiveQuote($val) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (is_array($val)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $return = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             foreach ($val as $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $return[] = $this->recursiveQuote($v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return '('.implode(',', $return).')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } elseif (is_null($val)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $val = 'NULL'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } elseif (is_int($val)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $val = (int) $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } elseif (is_double($val)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $val = (double) $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } elseif (is_float($val)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $val = (float) $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $val = "'" . Convert::raw2sql($val) . "'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |     public function ajaxResponse($message, $status) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         return Convert::raw2json(array( | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |             'message' => $message, | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |             'status' => $status, | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         )); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |