/** * Update record styles to match exhibit CSS. For example, if `styles` on * the parent exhibit is: * * .tag1 { * fill-color: #111111; * } * .tag2 { * stroke-color: #222222; * } * * And `array('tag1', 'tag2')` is passed, `fill_color` should be set to * '#111111' and `stroke_color` to '#222222'. * * @param array $tags An array of tags to pull. */ public function pullStyles($tags) { // If the record is new, pull the `all` tag. if (!$this->exists()) { $tags = array_merge($tags, array('all')); } // Parse the stylesheet. $css = nl_readCSS($this->getExhibit()->styles); // Gather style columns. $valid = nl_getStyles(); // Walk CSS rules. foreach ($css as $selector => $rules) { // If the tag is being pulled. if (in_array($selector, $tags)) { // Walk valid CSS rules. foreach ($rules as $prop => $val) { // Is the property valid? if (in_array($prop, $valid)) { // Set value if not `auto` or `none`. if ($val != 'auto' && $val != 'none') { $this->{$prop} = $val; } else { if ($val == 'none') { $this->{$prop} = null; } } } } } } }
/** * Update the exhibit stylesheet with values from a specific record. For * example, if `styles` is: * * .tag { * fill-color: #111111; * stroke-color: #222222; * } * * And the record is tagged with `tag` has a `fill_color` of `#333333` and * a `stroke_color` of `#444444`, the stylesheet would be updated to: * * .tag { * fill-color: #333333; * stroke-color: #444444; * } * * @param NeatlineRecord $record The record to update from. */ public function pullStyles($record) { // Parse the stylesheet. $css = nl_readCSS($this->styles); // Explode record tags. $tags = nl_explode($record->tags); // Gather style columns. $valid = nl_getStyles(); foreach ($css as $selector => $rules) { // Is the record tagged with the selector? if (in_array($selector, $tags) || $selector == 'all') { // Scan valid rule definitions. foreach ($rules as $prop => $val) { // Is the property valid? if (in_array($prop, $valid)) { // Get the record value. $value = !is_null($record->{$prop}) ? $record->{$prop} : 'none'; // Update the CSS. $css[$selector][$prop] = $value; } } } } // Recompile the stylesheet. $this->styles = nl_writeCSS($css); }