public static function displayLabel($idx = null) { return keyval($idx, static::getLabels()); }
/** * Extract a value from an array and return it as an indexed array * This is really just a shortcut function for keyval(null, $val, $array) * @see keyval() */ function value($val, $array) { return keyval(null, $val, $array); }
/** This relies on the m-m definitions in the static $load_many_to_many array * Much more complicated than just saving one to many, or deleting many to many.... * The daya should be an array that contains keys to the relationship matching * the relationship names defined in the object. * * Like if this class has many-to-many relationships with items and * children, it should have those relationships defined in the class, and * also defined in the static::$load_many_to_many variable, with the key * names of $load_many_to_many the same as the relationship names */ public function saveM2MRelations($data = []) { //pkdebug("Saving Here Data:", $data); if (empty(static::$load_many_to_many) || !array_intersect(array_keys(static::$load_many_to_many), array_keys($data))) { return true; #Nothing to do } foreach (static::$load_many_to_many as $relName => $definition) { if (!in_array($relName, array_keys($data))) { continue; } #Nothing here, keep looking $othermodel = keyval('other_model', $definition); if (!class_exists($othermodel) || !is_a($othermodel, self::class, true)) { throw new Exception("No found other class was defined for [{$relName}]"); } #Have the 'other' class - now find Pivot Class or Table $pivotmodel = keyval('pivot_model', $definition); if (!class_exists($pivotmodel) || !is_a($pivotmodel, self::class, true)) { $pivotmodel = null; $pivottable = keyval('pivot_table', $definition); if (!Schema::hasTable($pivottable)) { #Can't do anything throw new Exception("Niether a valid pivot class nor tabe was defined for [{$relName}]"); } } $mykey = keyval('my_key', $definition, Str::snake(getBaseName(static::class)) . '_id'); $otherkey = keyval('other_key', $definition, Str::snake(getBaseName($othermodel)) . '_id'); #Here's where the easy part ends. $arr = $data[$relName]; if (!is_arrayish($arr) || !count($arr)) { #Delete it all! $deleteAll = true; } else { #We have an array of data $otherobjs = $this->{$relName}; if (!is_arrayish($otherobjs)) { pkdebug("unexpected for [{$relName}], other objs are:", $otherobjs); continue; } $mycurrentotherobjkeys = []; foreach ($otherobjs as $otherobj) { if (!is_a($otherobj, $othermodel, true)) { #Again, something seriously wrong pkdebug("For [{$relName}], other model is [{$othermodel}], but otherobj:", $otherobj); continue; } $otherobjkey = $otherobj->getKey(); $mycurrentotherobjkeys[] = "{$otherobjkey}"; } #Great - we have a list of otherobj keys our model pointed to, we have a new #submitted list of other obj keys - let's go! #But gotta clean up the keys in case some are 3 & some are '3'! #Just make them all strings? //pkdebug("Array is:", $arr); $newarr = []; foreach ($arr as $el) { $newarr[] = "{$el}"; } $addIds = array_diff($newarr, $mycurrentotherobjkeys); $idsToDelete = array_diff($mycurrentotherobjkeys, $newarr); } $thiskeyval = $this->getKey(); $fresh = []; if ($pivotmodel) { if (!empty($deleteAll)) { $pivotmodel::where($mykey, $thiskeyval)->delete(); } else { $pivotmodel::where($mykey, $thiskeyval)->whereIn($otherkey, $idsToDelete)->delete(); $fresh[$mykey] = $thiskeyval; foreach ($addIds as $addId) { $fresh[$otherkey] = $addId; //pkdebug("Adding", $fresh); $pivotmodel::create($fresh); } } } else { if (Schema::hasTable($pivottable)) { #Gotta try it with flat table if (!empty($deleteAll)) { DB::table($pivottable)->where($mykey, $thiskeyval)->delete(); } else { DB::table($pivottable)->where($mykey, $thiskeyval)->whereIn($otherkey, $idsToDelete)->delete(); $fresh[$mykey] = $thiskeyval; foreach ($addIds as $addId) { $fresh[$otherkey] = $addId; DB::table($pivottable)->insert($fresh); } } } } } }
/** * Creates an array checkboxes with values - can return multiple values in array * When POSTing, will be a sparse array, so take array_values, and save array as JSON? * @param string $name - the base name of input set - but will be POSTed * as an array "$name[0], $name[1], etc * @param array $list - array of $values($keys) => $labels * @param array|scalar $values - the array of current values. If scalar, converted to array. * @param array $options * @param scalar|null $unset - the value if none of the options are selected */ public function multiselect($name, $list = [], $values = null, $options = [], $unset = null) { $values = $this->getValueAttribute($name, $values); $wrapperclass = keyval('wrapperclass', $options, ' form-control '); $allclass = keyval('allclass', $options); unset($options['wrapperclass']); unset($options['allclass']); $out = "\n<div class='multiselect {$wrapperclass} {$allclass} '>\n"; $out .= "\n<input type='hidden' name='{$name}' value='{$unset}' />\n"; foreach ($list as $key => $label) { $checked = in_array_equivalent($key, $values) ? true : false; $options['id'] = $name . '_' . $key; $out .= "\n<div class='pk-checkbox {$allclass}'>"; $out .= "<label class='multiselect-label {$allclass} '>"; //$out .= $this->checkbox($name."[$i]",$key,$checked,$options); $options['class'] = keyval('class', $options) . " {$allclass} "; $out .= $this->checkbox($name . "[]", $key, $checked, $options); $out .= "{$label}</label>\n"; $out .= "\n</div>"; } $out .= "\n</div>\n"; return $out; }
public function __call($method, $args = []) { $name = removeEndStr($method, 'Tfrm'); if (!$name) { $this->result = call_user_func_array([$this->item, $method], $args); return $this->executeActionSet(); } $actionarr = keyval($name, $this->transforms, $this->donothingaction); $actionarr['args'] = $args; $actionarr['name'] = $name; $this->actionset[] = $actionarr; return $this; }